Spaces:
Sleeping
Sleeping
Update application/static/js/components/initialize.js
Browse files
application/static/js/components/initialize.js
CHANGED
@@ -1,112 +1,119 @@
|
|
|
|
1 |
import requests from "./request.js";
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
this.
|
|
|
6 |
this.uiManager = uiManager;
|
7 |
this.systemPrompt = `your response syntax should be: ###for heading### \n ** for sub heading ** \n *** for text highlight/bold text ***`;
|
8 |
this.model = null;
|
|
|
9 |
}
|
10 |
|
11 |
-
async initialize(model=null){
|
12 |
-
this.convTitle=null;
|
13 |
-
this.convId=null;
|
14 |
this.uiManager.messagesDiv.innerHTML = '';
|
15 |
-
|
16 |
-
await this.
|
17 |
-
|
18 |
-
elem.addEventListener('click', ()=>{
|
19 |
-
this.reInitialize(elem.id)
|
20 |
-
})
|
21 |
-
})
|
22 |
-
if(model!=null){
|
23 |
-
this.model = model;
|
24 |
-
} else{
|
25 |
-
await this.fetchModels()
|
26 |
-
}
|
27 |
}
|
28 |
|
29 |
-
async reInitialize(id){
|
30 |
-
this.convTitle=null;
|
31 |
-
this.convId=id;
|
32 |
this.uiManager.messagesDiv.innerHTML = '';
|
33 |
await this.fetchConv(id);
|
34 |
}
|
35 |
|
36 |
-
async fetchConv(id){
|
37 |
try {
|
38 |
-
const response = await requests.request('POST','/fetch',{"Content-Type": "application/json"},JSON.stringify({"convId": id}),false);
|
39 |
-
if(!response.ok){
|
40 |
-
|
41 |
-
|
42 |
}
|
43 |
const data = await response.json();
|
44 |
this.convTitle = data['title'];
|
45 |
const arr = data['messages'];
|
46 |
-
for(let i=0;i<arr.length;i++){
|
47 |
const dict = arr[i];
|
48 |
-
if(dict['role']=='user'){
|
49 |
this.uiManager.appendUserMsg(dict['content'])
|
50 |
}
|
51 |
-
else if(dict['role']=='assistant'){
|
52 |
this.uiManager.appendAiMsg(dict['content']);
|
53 |
this.uiManager.renderSymbols.renderAll(this.uiManager.aiP);
|
54 |
}
|
55 |
}
|
56 |
} catch (error) {
|
57 |
-
alert(`
|
58 |
-
console.
|
59 |
}
|
60 |
}
|
61 |
-
|
|
|
62 |
try {
|
63 |
-
const response = await requests.request('GET','/convs',{"Content-Type": "application/json"},null,false);
|
64 |
-
if(!response.ok){
|
65 |
-
|
66 |
-
|
67 |
}
|
68 |
const data = await response.json();
|
69 |
-
|
|
|
70 |
const prevChat = document.createElement('div');
|
71 |
const dict = data[i];
|
72 |
prevChat.id = dict['convId'];
|
73 |
prevChat.className = 'prevChat';
|
74 |
prevChat.innerText = dict['title'];
|
75 |
-
this.uiManager.prevChatsCont.appendChild(prevChat)
|
76 |
|
|
|
|
|
|
|
|
|
77 |
}
|
78 |
} catch (error) {
|
79 |
-
alert(`
|
80 |
-
|
81 |
-
}
|
82 |
-
async createConv(){
|
83 |
-
const response = await requests.request('POST', '/create', {"Content-Type": "application/json"},JSON.stringify({"system_prompt": this.systemPrompt}),false)
|
84 |
-
if(!response.ok){
|
85 |
-
alert('error while creating new Conversation')
|
86 |
-
return
|
87 |
}
|
88 |
-
const data = await response.json()
|
89 |
-
this.convId = data['convId']
|
90 |
}
|
91 |
-
async fetchModels(){
|
92 |
-
const response = await requests.request('GET', '/models', {"Content-Type": "application/json"},null,false)
|
93 |
-
if(!response.ok){
|
94 |
-
alert('error while fetching models')
|
95 |
-
return
|
96 |
-
}
|
97 |
-
const data = await response.json();
|
98 |
-
this.model = data[0];
|
99 |
-
this.uiManager.models.innerHTML = '';
|
100 |
-
for(let i=0; i<data.length;i++){
|
101 |
-
const opt = document.createElement('option')
|
102 |
-
opt.innerText = data[i];
|
103 |
-
this.uiManager.models.appendChild(opt)
|
104 |
-
}
|
105 |
-
this.uiManager.models.addEventListener('change', (e)=>{
|
106 |
-
const selected = e.target.value;
|
107 |
-
this.initialize(selected)
|
108 |
-
})
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
}
|
111 |
}
|
112 |
-
export default Initialize
|
|
|
1 |
+
// application/static/js/initialize.js
|
2 |
import requests from "./request.js";
|
3 |
+
|
4 |
+
class Initialize {
|
5 |
+
constructor(uiManager) {
|
6 |
+
this.convId = null; // Initialize to null
|
7 |
+
this.convTitle = null;
|
8 |
this.uiManager = uiManager;
|
9 |
this.systemPrompt = `your response syntax should be: ###for heading### \n ** for sub heading ** \n *** for text highlight/bold text ***`;
|
10 |
this.model = null;
|
11 |
+
this.image_captioning = this.uiManager.image_captioning; // Get from uiManager
|
12 |
}
|
13 |
|
14 |
+
async initialize(model = null) {
|
15 |
+
this.convTitle = null;
|
16 |
+
this.convId = null;
|
17 |
this.uiManager.messagesDiv.innerHTML = '';
|
18 |
+
// No need to clear prevChatsCont here; it's done in fetchConvs
|
19 |
+
await this.fetchModels(model); // Fetch models *first*
|
20 |
+
await this.fetchConvs(); // *Then* fetch conversations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
}
|
22 |
|
23 |
+
async reInitialize(id) {
|
24 |
+
this.convTitle = null;
|
25 |
+
this.convId = id;
|
26 |
this.uiManager.messagesDiv.innerHTML = '';
|
27 |
await this.fetchConv(id);
|
28 |
}
|
29 |
|
30 |
+
async fetchConv(id) {
|
31 |
try {
|
32 |
+
const response = await requests.request('POST', '/fetch', { "Content-Type": "application/json" }, JSON.stringify({ "convId": id }), false);
|
33 |
+
if (!response.ok) {
|
34 |
+
const errorText = await response.text();
|
35 |
+
throw new Error(`Error fetching conversation: ${response.status} - ${errorText}`);
|
36 |
}
|
37 |
const data = await response.json();
|
38 |
this.convTitle = data['title'];
|
39 |
const arr = data['messages'];
|
40 |
+
for (let i = 0; i < arr.length; i++) {
|
41 |
const dict = arr[i];
|
42 |
+
if (dict['role'] == 'user') {
|
43 |
this.uiManager.appendUserMsg(dict['content'])
|
44 |
}
|
45 |
+
else if (dict['role'] == 'assistant') {
|
46 |
this.uiManager.appendAiMsg(dict['content']);
|
47 |
this.uiManager.renderSymbols.renderAll(this.uiManager.aiP);
|
48 |
}
|
49 |
}
|
50 |
} catch (error) {
|
51 |
+
alert(`An error occurred: ${error}`);
|
52 |
+
console.error(error);
|
53 |
}
|
54 |
}
|
55 |
+
|
56 |
+
async fetchConvs() {
|
57 |
try {
|
58 |
+
const response = await requests.request('GET', '/convs', { "Content-Type": "application/json" }, null, false);
|
59 |
+
if (!response.ok) {
|
60 |
+
const errorText = await response.text();
|
61 |
+
throw new Error(`Error fetching conversations: ${response.status} - ${errorText}`);
|
62 |
}
|
63 |
const data = await response.json();
|
64 |
+
this.uiManager.prevChatsCont.innerHTML = ''; // Clear here
|
65 |
+
for (let i = 0; i < data.length; i++) {
|
66 |
const prevChat = document.createElement('div');
|
67 |
const dict = data[i];
|
68 |
prevChat.id = dict['convId'];
|
69 |
prevChat.className = 'prevChat';
|
70 |
prevChat.innerText = dict['title'];
|
71 |
+
this.uiManager.prevChatsCont.appendChild(prevChat);
|
72 |
|
73 |
+
// Add event listener *inside* the loop, after appending
|
74 |
+
prevChat.addEventListener('click', () => {
|
75 |
+
this.reInitialize(prevChat.id);
|
76 |
+
});
|
77 |
}
|
78 |
} catch (error) {
|
79 |
+
alert(`An error occurred: ${error}`);
|
80 |
+
console.error(error);
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
}
|
|
|
|
|
82 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
|
85 |
+
async fetchModels(initialModel) {
|
86 |
+
try {
|
87 |
+
const response = await requests.request('GET', '/models', { "Content-Type": "application/json" }, null, false);
|
88 |
+
if (!response.ok) {
|
89 |
+
const errorText = await response.text();
|
90 |
+
throw new Error(`Error fetching models: ${response.status} - ${errorText}`);
|
91 |
+
}
|
92 |
+
const data = await response.json();
|
93 |
+
this.uiManager.models.innerHTML = ''; // Clear existing options
|
94 |
+
|
95 |
+
for (let i = 0; i < data.length; i++) {
|
96 |
+
const opt = document.createElement('option');
|
97 |
+
opt.innerText = data[i];
|
98 |
+
opt.value = data[i]; // Set the value attribute
|
99 |
+
this.uiManager.models.appendChild(opt);
|
100 |
+
}
|
101 |
+
|
102 |
+
// Set the initial model, or the first one if none provided
|
103 |
+
this.model = initialModel || data[0];
|
104 |
+
this.uiManager.models.value = this.model; // Set selected option
|
105 |
+
|
106 |
+
this.uiManager.models.addEventListener('change', (e) => {
|
107 |
+
const selected = e.target.value;
|
108 |
+
this.model = selected; // Update the model
|
109 |
+
// this.initialize(selected); // Don't re-initialize the entire UI.
|
110 |
+
// Just update this.model. Re-initializing is too disruptive.
|
111 |
+
});
|
112 |
+
|
113 |
+
} catch (error) {
|
114 |
+
alert(`An error occurred: ${error}`);
|
115 |
+
console.error(error);
|
116 |
+
}
|
117 |
}
|
118 |
}
|
119 |
+
export default Initialize
|