Almaatla commited on
Commit
120f3d6
·
verified ·
1 Parent(s): 050938e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -8
app.py CHANGED
@@ -66,28 +66,114 @@ async def get_proxy():
66
  return HTMLResponse("""
67
  <html>
68
  <body>
69
- <h1>Proxy Client</h1>
 
 
 
 
 
 
 
 
 
 
 
70
  <script>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  const ws = new WebSocket('wss://' + window.location.host + '/ws');
72
  ws.onopen = () => {
73
  ws.send(JSON.stringify({ source: 'proxy' }));
74
  };
75
- ws.onmessage = e => {
 
76
  const msg = JSON.parse(e.data);
77
  if (msg.destination === 'proxy') {
78
- const response = {
79
- content: "Hello user!",
80
- source: 'proxy',
81
- destination: 'user'
82
- };
83
- ws.send(JSON.stringify(response));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  }
85
  };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  </script>
87
  </body>
88
  </html>
89
  """)
90
 
 
91
  @app.websocket("/ws")
92
  async def websocket_endpoint(websocket: WebSocket):
93
  await manager.connect(websocket)
 
66
  return HTMLResponse("""
67
  <html>
68
  <body>
69
+ <h1>Proxy Client (LLM Gateway)</h1>
70
+ <div style="margin-bottom: 20px;">
71
+ <input type="password" id="apiKey" placeholder="Enter API Key" style="width: 300px;">
72
+ <button onclick="initializeClient()">Fetch Models</button>
73
+ </div>
74
+ <div style="margin-bottom: 20px;">
75
+ <select id="modelSelect" style="width: 300px;">
76
+ <option value="" disabled selected>-- Select Model --</option>
77
+ </select>
78
+ </div>
79
+ <div id="status"></div>
80
+
81
  <script>
82
+ let agentClient = null;
83
+ let currentModel = null;
84
+ const systemPrompt = "You are a helpful AI assistant. Respond concisely and accurately.";
85
+ const conversationHistory = [];
86
+
87
+ function showStatus(message, type = 'info') {
88
+ const statusDiv = document.getElementById('status');
89
+ statusDiv.innerHTML = `<div style="color: ${type === 'error' ? 'red' : 'orange'}">${message}</div>`;
90
+ }
91
+
92
+ function initializeClient() {
93
+ const apiKey = document.getElementById('apiKey').value;
94
+ if (!apiKey) {
95
+ showStatus("Please enter an API key", 'error');
96
+ return;
97
+ }
98
+
99
+ agentClient = new ConversationalAgentClient(apiKey);
100
+ agentClient.populateLLMModels()
101
+ .then(models => {
102
+ agentClient.updateModelSelect('modelSelect', models.find(m => m.includes("gemini-2.5")));
103
+ currentModel = document.getElementById('modelSelect').value;
104
+ showStatus(`Loaded ${models.length} models. Default: ${currentModel}`);
105
+ })
106
+ .catch(error => {
107
+ showStatus(`Error fetching models: ${error.message}`, 'error');
108
+ });
109
+ }
110
+
111
+ // WebSocket setup
112
  const ws = new WebSocket('wss://' + window.location.host + '/ws');
113
  ws.onopen = () => {
114
  ws.send(JSON.stringify({ source: 'proxy' }));
115
  };
116
+
117
+ ws.onmessage = async e => {
118
  const msg = JSON.parse(e.data);
119
  if (msg.destination === 'proxy') {
120
+ try {
121
+ showStatus("Processing user query...");
122
+ const llmResponse = await agentClient.call(
123
+ currentModel,
124
+ msg.content,
125
+ systemPrompt,
126
+ conversationHistory
127
+ );
128
+
129
+ const responseMsg = {
130
+ content: llmResponse.response,
131
+ source: 'proxy',
132
+ destination: 'user'
133
+ };
134
+ ws.send(JSON.stringify(responseMsg));
135
+ showStatus("Response sent successfully");
136
+ } catch (error) {
137
+ console.error("LLM Error:", error);
138
+ const errorResponse = {
139
+ content: `Error processing request: ${error.message}`,
140
+ source: 'proxy',
141
+ destination: 'user'
142
+ };
143
+ ws.send(JSON.stringify(errorResponse));
144
+ showStatus(`Error: ${error.message}`, 'error');
145
+ }
146
  }
147
  };
148
+
149
+ // Model selection change handler
150
+ document.getElementById('modelSelect').addEventListener('change', function() {
151
+ currentModel = this.value;
152
+ showStatus(`Model changed to: ${currentModel}`);
153
+ });
154
+
155
+ // --- Include provided client classes here ---
156
+ // --- API Client Classes --- (Keep existing classes BaseAgentClient, ConversationalAgentClient)
157
+ class BaseAgentClient {
158
+ constructor(apiKey, apiUrl = 'https://llm.synapse.thalescloud.io/v1/') { this.apiKey = apiKey; this.apiUrl = apiUrl; this.models = []; this.maxCallsPerMinute = 4; this.callTimestamps = []; }
159
+ async fetchLLMModels() { if (!this.apiKey) throw new Error("API Key is not set."); console.log("Fetching models from:", this.apiUrl + 'models'); try { const response = await fetch(this.apiUrl + 'models', { method: 'GET', headers: { 'Authorization': `Bearer ${this.apiKey}` } }); if (!response.ok) { const errorText = await response.text(); console.error("Fetch models error response:", errorText); throw new Error(`HTTP error! Status: ${response.status} - ${errorText}`); } const data = await response.json(); console.log("Models fetched:", data.data); const filteredModels = data.data.map(model => model.id).filter(id => !id.toLowerCase().includes('embed') && !id.toLowerCase().includes('image')); return filteredModels; } catch (error) { console.error('Error fetching LLM models:', error); throw new Error(`Failed to fetch models: ${error.message}`); } }
160
+ async populateLLMModels(defaultModel = "gemini-2.5-pro-exp-03-25") { try { const modelList = await this.fetchLLMModels(); const sortedModels = modelList.sort((a, b) => { if (a === defaultModel) return -1; if (b === defaultModel) return 1; return a.localeCompare(b); }); const finalModels = []; if (sortedModels.includes(defaultModel)) { finalModels.push(defaultModel); sortedModels.forEach(model => { if (model !== defaultModel) finalModels.push(model); }); } else { finalModels.push(defaultModel); finalModels.push(...sortedModels); } this.models = finalModels; console.log("Populated models:", this.models); return this.models; } catch (error) { console.error("Error populating models:", error); this.models = [defaultModel]; throw error; } }
161
+ updateModelSelect(elementId = 'modelSelect', selectedModel = null) { const select = document.getElementById(elementId); if (!select) { console.warn(`Element ID ${elementId} not found.`); return; } const currentSelection = selectedModel || select.value || this.models[0]; select.innerHTML = ''; if (this.models.length === 0 || (this.models.length === 1 && this.models[0] === "gemini-2.5-pro-exp-03-25" && !this.apiKey)) { const option = document.createElement('option'); option.value = ""; option.textContent = "-- Fetch models first --"; option.disabled = true; select.appendChild(option); return; } this.models.forEach(model => { const option = document.createElement('option'); option.value = model; option.textContent = model; if (model === currentSelection) option.selected = true; select.appendChild(option); }); if (!select.value && this.models.length > 0) select.value = this.models[0]; }
162
+ async rateLimitWait() { const currentTime = Date.now(); this.callTimestamps = this.callTimestamps.filter(ts => currentTime - ts <= 60000); if (this.callTimestamps.length >= this.maxCallsPerMinute) { const waitTime = 60000 - (currentTime - this.callTimestamps[0]); const waitSeconds = Math.ceil(waitTime / 1000); const waitMessage = `Rate limit (${this.maxCallsPerMinute}/min) reached. Waiting ${waitSeconds}s...`; console.log(waitMessage); showGenerationStatus(waitMessage, 'warn'); await new Promise(resolve => setTimeout(resolve, waitTime + 100)); showGenerationStatus('Resuming after rate limit wait...', 'info'); this.callTimestamps = this.callTimestamps.filter(ts => Date.now() - ts <= 60000); } }
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>
174
  """)
175
 
176
+
177
  @app.websocket("/ws")
178
  async def websocket_endpoint(websocket: WebSocket):
179
  await manager.connect(websocket)