1
1
document . addEventListener ( 'DOMContentLoaded' , function ( ) {
2
- var apiKeyInput = document . getElementById ( 'apikeyInput' ) ;
3
- var agentRole = document . getElementById ( 'agentRole' ) ;
4
- var agentBot = document . getElementById ( 'agentBot' ) ;
5
- var chatBox = document . getElementById ( 'chat-box' ) ;
6
- var endPoint = "https://api.openai.com/v1/chat/completions" ;
7
- var AgentModel = "gpt-3.5-turbo" ;
8
- var AgentEndPoint = "https://api.openai.com/v1/chat/completions" ;
9
- var botModel = document . getElementById ( 'agentModel' ) ;
10
- var botEndPoint = document . getElementById ( 'agentEndPoint' ) ;
11
- botModel . value = AgentModel ;
12
- botEndPoint . value = endPoint ;
2
+ const apiKeyInput = document . getElementById ( 'apikeyInput' ) ;
3
+ const agentRole = document . getElementById ( 'agentRole' ) ;
4
+ const agentBot = document . getElementById ( 'agentBot' ) ;
5
+ const chatBox = document . getElementById ( 'chat-box' ) ;
6
+ const botModel = document . getElementById ( 'agentModel' ) ;
7
+ const botEndPoint = document . getElementById ( 'agentEndPoint' ) ;
13
8
14
- function getCookieValue ( cookieName ) {
15
- var name = cookieName + '=' ;
16
- var decodedCookie = decodeURIComponent ( document . cookie ) ;
17
- var cookieArray = decodedCookie . split ( ';' ) ;
18
- for ( var i = 0 ; i < cookieArray . length ; i ++ ) {
19
- var cookie = cookieArray [ i ] . trim ( ) ;
20
- if ( cookie . indexOf ( name ) === 0 ) {
21
- return cookie . substring ( name . length , cookie . length ) ;
22
- }
23
- }
24
- return '' ;
25
- }
26
-
27
- var agentApiKeyValue = getCookieValue ( 'agent_apikey' ) ;
28
- if ( agentApiKeyValue !== '' ) {
29
- apiKeyInput . value = agentApiKeyValue ;
30
- }
9
+ const DEFAULT_API_ENDPOINT = "https://api.openai.com/v1/chat/completions" ;
10
+ const DEFAULT_MODEL = "gpt-3.5-turbo" ;
31
11
32
- apiKeyInput . addEventListener ( 'input' , function ( ) {
33
- var apiKeyValue = apiKeyInput . value ;
34
- document . cookie = 'agent_apikey=' + encodeURIComponent ( apiKeyValue ) ;
35
- } ) ;
12
+ botModel . value = DEFAULT_MODEL ;
13
+ botEndPoint . value = DEFAULT_API_ENDPOINT ;
36
14
37
- var agentRoleValue = getCookieValue ( 'agent_role' ) ;
38
- if ( agentRoleValue !== '' ) {
39
- agentRole . value = agentRoleValue ;
40
- }
15
+ loadSettings ( ) ;
41
16
42
- agentRole . addEventListener ( 'input' , function ( ) {
43
- var agentRoleValue = agentRole . value ;
44
- document . cookie = 'agent_role=' + encodeURIComponent ( agentRoleValue ) ;
17
+ apiKeyInput . addEventListener ( 'input' , saveApiKey ) ;
18
+ agentRole . addEventListener ( 'input' , saveAgentRole ) ;
19
+ agentBot . addEventListener ( 'change' , updateBotSettings ) ;
20
+ document . getElementById ( 'send-button' ) . addEventListener ( 'click' , sendMessage ) ;
21
+ document . getElementById ( 'user-input' ) . addEventListener ( 'keypress' , function ( event ) {
22
+ if ( event . key === 'Enter' ) sendMessage ( ) ;
45
23
} ) ;
46
24
47
- var agentBotValue = getCookieValue ( 'agent_bot' ) ;
48
- if ( agentBotValue !== '' ) {
49
- agentBot . value = agentBotValue ;
25
+ function loadSettings ( ) {
26
+ apiKeyInput . value = getCookieValue ( 'agent_apikey' ) || '' ;
27
+ agentRole . value = getCookieValue ( 'agent_role' ) || agentRole . placeholder ;
28
+ agentBot . value = getCookieValue ( 'agent_bot' ) || 'chatGPT' ;
29
+ updateBotSettings ( ) ;
50
30
}
51
31
52
- // Event listener for Agent Bot select changes
53
- agentBot . addEventListener ( 'change' , function ( event ) {
54
- var agentBotValue = agentBot . value ;
55
- AgentEndPoint = ( agentBotValue === "Llama" ) ? "http://localhost:1234/v1/chat/completions" : "https://api.openai.com/v1/chat/completions" ;
56
- document . cookie = 'agent_bot=' + encodeURIComponent ( agentBotValue ) ;
57
- botModel . value = "gpt-3.5-turbo" ;
58
- botEndPoint . value = AgentEndPoint ;
59
- } ) ;
32
+ function saveApiKey ( ) {
33
+ setCookie ( 'agent_apikey' , apiKeyInput . value ) ;
34
+ }
60
35
61
- // Event listener for send button click
62
- document . getElementById ( 'send-button' ) . addEventListener ( 'click' , sendMessage ) ;
36
+ function saveAgentRole ( ) {
37
+ setCookie ( 'agent_role' , agentRole . value ) ;
38
+ }
63
39
64
- // Event listener for user input enter key press
65
- document . getElementById ( 'user-input' ) . addEventListener ( 'keypress' , function ( event ) {
66
- if ( event . key === 'Enter' ) {
67
- sendMessage ( ) ;
40
+ function updateBotSettings ( ) {
41
+ const agentBotValue = agentBot . value ;
42
+
43
+ let endPoint = '' ;
44
+ let model = '' ;
45
+
46
+ if ( agentBotValue === "Llama" ) {
47
+ endPoint = "http://localhost:1234/v1/chat/completions" ;
48
+ model = "llama-model" ;
49
+ } else if ( agentBotValue === "chatGPT" ) {
50
+ endPoint = DEFAULT_API_ENDPOINT ;
51
+ model = DEFAULT_MODEL ;
52
+ } else if ( agentBotValue === "Custom" ) {
53
+ endPoint = '' ;
54
+ model = '' ;
68
55
}
69
- } ) ;
56
+
57
+ setCookie ( 'agent_bot' , agentBotValue ) ;
58
+ botModel . value = model ;
59
+ botEndPoint . value = endPoint ;
60
+ }
70
61
71
62
async function sendMessage ( ) {
72
63
const inputField = document . getElementById ( 'user-input' ) ;
73
64
const userMessage = inputField . value . trim ( ) ;
74
65
75
- if ( userMessage === '' ) {
76
- return ;
77
- }
66
+ if ( ! userMessage ) return ;
78
67
79
68
appendMessage ( 'user' , userMessage ) ;
80
69
inputField . value = '' ;
@@ -94,12 +83,14 @@ document.addEventListener('DOMContentLoaded', function() {
94
83
}
95
84
96
85
async function getBotResponse ( userMessage ) {
97
- const apiKey = apiKeyInput . value || 'YOUR_API_KEY' ; // Replace with your OpenAI API key
98
- const endpoint = AgentEndPoint || 'https://api.openai.com/v1/chat/completions' ;
86
+ const apiKey = apiKeyInput . value || 'YOUR_API_KEY' ;
87
+ const endpoint = botEndPoint . value ;
88
+ const model = botModel . value ;
89
+
99
90
const payload = {
100
- model : AgentModel ,
91
+ model : model ,
101
92
messages : [
102
- { role : "system" , content : agentRole . value || "You are a friendly and helpful assistant named Alex. You are knowledgeable in technology, science, and literature. Always be polite and use simple, easy-to-understand language." } ,
93
+ { role : "system" , content : agentRole . value } ,
103
94
{ role : "user" , content : userMessage }
104
95
] ,
105
96
max_tokens : 150 ,
@@ -123,15 +114,29 @@ document.addEventListener('DOMContentLoaded', function() {
123
114
if ( data . choices && data . choices . length > 0 ) {
124
115
return data . choices [ 0 ] . message . content . trim ( ) ;
125
116
} else {
126
- if ( data . error && data . error . message ) {
127
- return '(' + data . error . code + ') ' + data . error . message ;
128
- } else {
129
- return 'Sorry, I am having trouble responding at the moment. Please try again later.' ;
130
- }
117
+ return data . error ? `(${ data . error . code } ) ${ data . error . message } ` : 'Sorry, I am having trouble responding at the moment. Please try again later.' ;
131
118
}
132
119
} catch ( error ) {
133
120
console . error ( 'Error:' , error ) ;
134
121
return 'Sorry, I am having trouble responding at the moment. Please try again later.' ;
135
122
}
136
123
}
137
- } ) ;
124
+
125
+ function getCookieValue ( cookieName ) {
126
+ const name = cookieName + '=' ;
127
+ const decodedCookie = decodeURIComponent ( document . cookie ) ;
128
+ const cookieArray = decodedCookie . split ( ';' ) ;
129
+ for ( const cookie of cookieArray ) {
130
+ let c = cookie . trim ( ) ;
131
+ if ( c . indexOf ( name ) === 0 ) return c . substring ( name . length , c . length ) ;
132
+ }
133
+ return '' ;
134
+ }
135
+
136
+ function setCookie ( cookieName , cookieValue , days = 365 ) {
137
+ const d = new Date ( ) ;
138
+ d . setTime ( d . getTime ( ) + ( days * 24 * 60 * 60 * 1000 ) ) ;
139
+ const expires = `expires=${ d . toUTCString ( ) } ` ;
140
+ document . cookie = `${ cookieName } =${ encodeURIComponent ( cookieValue ) } ;${ expires } ;path=/` ;
141
+ }
142
+ } ) ;
0 commit comments