yasirme commited on
Commit
e608215
·
verified ·
1 Parent(s): af9a3da

Update application/static/js/components/initialize.js

Browse files
application/static/js/components/initialize.js CHANGED
@@ -1,112 +1,112 @@
1
- import requests from "./request.js";
2
- class Initialize{
3
- constructor(uiManager){
4
- this.convId;
5
- this.convTitle;
6
- this.uiManager = uiManager;
7
- this.systemPrompt = `your response syntax should be: ***for heading*** \n ** for sub heading **`;
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
- this.uiManager.prevChatsCont.innerHTML = '';
16
- await this.fetchConvs();
17
- document.querySelectorAll('.prevChat').forEach((elem)=>{
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=null;
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
- alert('error while fetching conversations')
41
- return
42
- }
43
- let data = await response.json();
44
- this.convTitle = data['title']
45
- data = data['messages']
46
- for(let i=0;i<data.length;i++){
47
- const dict = data[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(`an error occured ${error}`)
58
- console.log(error)
59
- }
60
- }
61
- async fetchConvs(){
62
- try {
63
- const response = await requests.request('GET','/convs',{"Content-Type": "application/json"},null,false);
64
- if(!response.ok){
65
- alert('error while fetching conversations')
66
- return
67
- }
68
- const data = await response.json();
69
- for(let i=0;i<data.length;i++){
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(`an error ocuured ${error}`)
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
+ import requests from "./request.js";
2
+ class Initialize{
3
+ constructor(uiManager){
4
+ this.convId;
5
+ this.convTitle;
6
+ this.uiManager = uiManager;
7
+ this.systemPrompt = `your response syntax should be: ***for heading*** \n ** for sub heading **`;
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
+ this.uiManager.prevChatsCont.innerHTML = '';
16
+ await this.fetchConvs();
17
+ document.querySelectorAll('.prevChat').forEach((elem)=>{
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
+ alert('error while fetching conversations')
41
+ return
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(`an error occured ${error}`)
58
+ console.log(error)
59
+ }
60
+ }
61
+ async fetchConvs(){
62
+ try {
63
+ const response = await requests.request('GET','/convs',{"Content-Type": "application/json"},null,false);
64
+ if(!response.ok){
65
+ alert('error while fetching conversations')
66
+ return
67
+ }
68
+ const data = await response.json();
69
+ for(let i=0;i<data.length;i++){
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(`an error ocuured ${error}`)
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