Almaatla commited on
Commit
9c48c76
·
verified ·
1 Parent(s): 120f3d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -5
app.py CHANGED
@@ -163,11 +163,81 @@ async def get_proxy():
163
  async callAgent(model, messages, temperature = 0.7) { await this.rateLimitWait(); const startTime = Date.now(); console.log("Calling Agent:", model); try { const response = await fetch(this.apiUrl + 'chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}` }, body: JSON.stringify({ model: model, messages: messages, temperature: temperature }) }); const endTime = Date.now(); this.callTimestamps.push(endTime); console.log(`API call took ${endTime - startTime} ms`); if (!response.ok) { const errorData = await response.json().catch(() => ({ error: { message: response.statusText } })); console.error("API Error:", errorData); throw new Error(errorData.error?.message || `API failed: ${response.status}`); } const data = await response.json(); if (!data.choices || !data.choices[0]?.message) throw new Error("Invalid API response structure"); console.log("API Response received."); return data.choices[0].message.content; } catch (error) { this.callTimestamps.push(Date.now()); console.error('Error calling agent:', error); throw error; } }
164
  setMaxCallsPerMinute(value) { const parsedValue = parseInt(value, 10); if (!isNaN(parsedValue) && parsedValue > 0) { console.log(`Max calls/min set to: ${parsedValue}`); this.maxCallsPerMinute = parsedValue; return true; } console.warn(`Invalid max calls/min: ${value}`); return false; }
165
  }
166
- class ConversationalAgentClient extends BaseAgentClient {
167
- constructor(apiKey, apiUrl = 'https://llm.synapse.thalescloud.io/v1/') { super(apiKey, apiUrl); }
168
- async call(model, userPrompt, systemPrompt, conversationHistory = [], temperature = 0.7) { const messages = [{ role: 'system', content: systemPrompt }, ...conversationHistory, { role: 'user', content: userPrompt }]; const assistantResponse = await super.callAgent(model, messages, temperature); const updatedHistory = [...conversationHistory, { role: 'user', content: userPrompt }, { role: 'assistant', content: assistantResponse }]; return { response: assistantResponse, history: updatedHistory }; }
169
- async callWithCodeContext(model, userPrompt, systemPrompt, selectedCodeVersionsData = [], conversationHistory = [], temperature = 0.7) { let codeContext = ""; let fullSystemPrompt = systemPrompt || ""; if (selectedCodeVersionsData && selectedCodeVersionsData.length > 0) { codeContext = "Code context (chronological):\n\n"; selectedCodeVersionsData.forEach((versionData, index) => { if (versionData && typeof versionData.code === 'string') codeContext += `--- Part ${index + 1} (${versionData.version || '?'}) ---\n${versionData.code}\n\n`; else console.warn(`Invalid context version data at index ${index}`); }); codeContext += "-------- end context ---\n\nUser request based on context:\n\n"; } const fullPrompt = codeContext + userPrompt; const messages = [{ role: 'system', content: fullSystemPrompt }, ...conversationHistory, { role: 'user', content: fullPrompt }]; const assistantResponse = await super.callAgent(model, messages, temperature); const updatedHistory = [...conversationHistory, { role: 'user', content: fullPrompt }, { role: 'assistant', content: assistantResponse }]; return { response: assistantResponse, history: updatedHistory }; }
170
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  </script>
172
  </body>
173
  </html>
 
163
  async callAgent(model, messages, temperature = 0.7) { await this.rateLimitWait(); const startTime = Date.now(); console.log("Calling Agent:", model); try { const response = await fetch(this.apiUrl + 'chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}` }, body: JSON.stringify({ model: model, messages: messages, temperature: temperature }) }); const endTime = Date.now(); this.callTimestamps.push(endTime); console.log(`API call took ${endTime - startTime} ms`); if (!response.ok) { const errorData = await response.json().catch(() => ({ error: { message: response.statusText } })); console.error("API Error:", errorData); throw new Error(errorData.error?.message || `API failed: ${response.status}`); } const data = await response.json(); if (!data.choices || !data.choices[0]?.message) throw new Error("Invalid API response structure"); console.log("API Response received."); return data.choices[0].message.content; } catch (error) { this.callTimestamps.push(Date.now()); console.error('Error calling agent:', error); throw error; } }
164
  setMaxCallsPerMinute(value) { const parsedValue = parseInt(value, 10); if (!isNaN(parsedValue) && parsedValue > 0) { console.log(`Max calls/min set to: ${parsedValue}`); this.maxCallsPerMinute = parsedValue; return true; } console.warn(`Invalid max calls/min: ${value}`); return false; }
165
  }
166
+ class ConversationalAgentClient extends BaseAgentClient {
167
+ constructor(apiKey, apiUrl = 'https://llm.synapse.thalescloud.io/v1/') {
168
+ super(apiKey, apiUrl);
169
+ }
170
+
171
+ async call(model, userPrompt, systemPrompt, conversationHistory = [], temperature = 0.7) {
172
+ const messages = [
173
+ { role: 'system', content: systemPrompt },
174
+ ...conversationHistory,
175
+ { role: 'user', content: userPrompt }
176
+ ];
177
+
178
+ const assistantResponse = await super.callAgent(model, messages, temperature);
179
+
180
+ const updatedHistory = [
181
+ ...conversationHistory,
182
+ { role: 'user', content: userPrompt },
183
+ { role: 'assistant', content: assistantResponse }
184
+ ];
185
+
186
+ return {
187
+ response: assistantResponse,
188
+ history: updatedHistory
189
+ };
190
+ }
191
+
192
+ async callWithCodeContext(
193
+ model,
194
+ userPrompt,
195
+ systemPrompt,
196
+ selectedCodeVersionsData = [],
197
+ conversationHistory = [],
198
+ temperature = 0.7
199
+ ) {
200
+ let codeContext = "";
201
+ let fullSystemPrompt = systemPrompt || "";
202
+
203
+ if (selectedCodeVersionsData && selectedCodeVersionsData.length > 0) {
204
+ codeContext = "Code context (chronological):\n\n";
205
+
206
+ selectedCodeVersionsData.forEach((versionData, index) => {
207
+ if (versionData && typeof versionData.code === 'string') {
208
+ codeContext += `--- Part ${index + 1} (${versionData.version || '?'}) ---\n`;
209
+ codeContext += `${versionData.code}\n\n`;
210
+ } else {
211
+ console.warn(`Invalid context version data at index ${index}`);
212
+ }
213
+ });
214
+
215
+ codeContext += "-------- end context ---\n\nUser request based on context:\n\n";
216
+ }
217
+
218
+ const fullPrompt = codeContext + userPrompt;
219
+
220
+ const messages = [
221
+ { role: 'system', content: fullSystemPrompt },
222
+ ...conversationHistory,
223
+ { role: 'user', content: fullPrompt }
224
+ ];
225
+
226
+ const assistantResponse = await super.callAgent(model, messages, temperature);
227
+
228
+ const updatedHistory = [
229
+ ...conversationHistory,
230
+ { role: 'user', content: fullPrompt },
231
+ { role: 'assistant', content: assistantResponse }
232
+ ];
233
+
234
+ return {
235
+ response: assistantResponse,
236
+ history: updatedHistory
237
+ };
238
+ }
239
+ }
240
+
241
  </script>
242
  </body>
243
  </html>