Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -104,184 +104,117 @@ async def get_proxy():
|
|
104 |
return HTMLResponse("""
|
105 |
<html>
|
106 |
<body>
|
107 |
-
<h1>Proxy Client (
|
108 |
-
<div style="
|
109 |
-
|
110 |
-
<
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
|
|
|
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
128 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
}
|
148 |
|
149 |
-
// WebSocket
|
150 |
const ws = new WebSocket('wss://' + window.location.host + '/ws');
|
151 |
-
ws.onopen = () => {
|
152 |
-
ws.send(JSON.stringify({ source: 'proxy' }));
|
153 |
-
};
|
154 |
-
|
155 |
ws.onmessage = async e => {
|
156 |
const msg = JSON.parse(e.data);
|
|
|
|
|
157 |
if (msg.destination === 'proxy') {
|
|
|
|
|
|
|
158 |
try {
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
systemPrompt,
|
164 |
-
conversationHistory
|
165 |
-
);
|
166 |
|
167 |
const responseMsg = {
|
168 |
content: llmResponse.response,
|
169 |
source: 'proxy',
|
170 |
-
destination:
|
171 |
};
|
172 |
ws.send(JSON.stringify(responseMsg));
|
173 |
-
|
|
|
174 |
} catch (error) {
|
175 |
-
|
176 |
const errorResponse = {
|
177 |
-
content: `Error
|
178 |
source: 'proxy',
|
179 |
-
destination:
|
180 |
};
|
181 |
ws.send(JSON.stringify(errorResponse));
|
182 |
-
showStatus(`Error: ${error.message}`, 'error');
|
183 |
}
|
184 |
}
|
185 |
};
|
186 |
|
187 |
-
//
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
// --- Include provided client classes here ---
|
194 |
-
// --- API Client Classes --- (Keep existing classes BaseAgentClient, ConversationalAgentClient)
|
195 |
-
class BaseAgentClient {
|
196 |
-
constructor(apiKey, apiUrl = 'https://llm.synapse.thalescloud.io/v1/') { this.apiKey = apiKey; this.apiUrl = apiUrl; this.models = []; this.maxCallsPerMinute = 4; this.callTimestamps = []; }
|
197 |
-
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}`); } }
|
198 |
-
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; } }
|
199 |
-
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]; }
|
200 |
-
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); } }
|
201 |
-
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; } }
|
202 |
-
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; }
|
203 |
-
}
|
204 |
-
class ConversationalAgentClient extends BaseAgentClient {
|
205 |
-
constructor(apiKey, apiUrl = 'https://llm.synapse.thalescloud.io/v1/') {
|
206 |
-
super(apiKey, apiUrl);
|
207 |
-
}
|
208 |
-
|
209 |
-
async call(model, userPrompt, systemPrompt, conversationHistory = [], temperature = 0.7) {
|
210 |
-
const messages = [
|
211 |
-
{ role: 'system', content: systemPrompt },
|
212 |
-
...conversationHistory,
|
213 |
-
{ role: 'user', content: userPrompt }
|
214 |
-
];
|
215 |
-
|
216 |
-
const assistantResponse = await super.callAgent(model, messages, temperature);
|
217 |
-
|
218 |
-
const updatedHistory = [
|
219 |
-
...conversationHistory,
|
220 |
-
{ role: 'user', content: userPrompt },
|
221 |
-
{ role: 'assistant', content: assistantResponse }
|
222 |
-
];
|
223 |
-
|
224 |
-
return {
|
225 |
-
response: assistantResponse,
|
226 |
-
history: updatedHistory
|
227 |
-
};
|
228 |
-
}
|
229 |
-
|
230 |
-
async callWithCodeContext(
|
231 |
-
model,
|
232 |
-
userPrompt,
|
233 |
-
systemPrompt,
|
234 |
-
selectedCodeVersionsData = [],
|
235 |
-
conversationHistory = [],
|
236 |
-
temperature = 0.7
|
237 |
-
) {
|
238 |
-
let codeContext = "";
|
239 |
-
let fullSystemPrompt = systemPrompt || "";
|
240 |
-
|
241 |
-
if (selectedCodeVersionsData && selectedCodeVersionsData.length > 0) {
|
242 |
-
codeContext = `Code context (chronological):\n\n`;
|
243 |
-
|
244 |
-
selectedCodeVersionsData.forEach((versionData, index) => {
|
245 |
-
if (versionData && typeof versionData.code === 'string') {
|
246 |
-
codeContext += `--- Part ${index + 1} (${versionData.version || '?'}) ---\n`;
|
247 |
-
codeContext += `${versionData.code}\n\n`;
|
248 |
-
} else {
|
249 |
-
console.warn(`Invalid context version data at index ${index}`);
|
250 |
-
}
|
251 |
-
});
|
252 |
-
|
253 |
-
codeContext += `-------- end context ---\n\nUser request based on context:\n\n`;
|
254 |
-
}
|
255 |
-
|
256 |
-
const fullPrompt = codeContext + userPrompt;
|
257 |
-
|
258 |
-
const messages = [
|
259 |
-
{ role: 'system', content: fullSystemPrompt },
|
260 |
-
...conversationHistory,
|
261 |
-
{ role: 'user', content: fullPrompt }
|
262 |
-
];
|
263 |
-
|
264 |
-
const assistantResponse = await super.callAgent(model, messages, temperature);
|
265 |
-
|
266 |
-
const updatedHistory = [
|
267 |
-
...conversationHistory,
|
268 |
-
{ role: 'user', content: fullPrompt },
|
269 |
-
{ role: 'assistant', content: assistantResponse }
|
270 |
-
];
|
271 |
-
|
272 |
-
return {
|
273 |
-
response: assistantResponse,
|
274 |
-
history: updatedHistory
|
275 |
-
};
|
276 |
}
|
277 |
-
}
|
278 |
-
|
279 |
</script>
|
280 |
</body>
|
281 |
</html>
|
282 |
""")
|
283 |
|
284 |
|
|
|
285 |
@app.post("/v1/chat/completions", response_model=ChatCompletionResponse)
|
286 |
async def chat_completions(request: ChatCompletionRequest):
|
287 |
request_id = str(uuid.uuid4())
|
|
|
104 |
return HTMLResponse("""
|
105 |
<html>
|
106 |
<body>
|
107 |
+
<h1>Proxy Client (Message Gateway)</h1>
|
108 |
+
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; height: 80vh;">
|
109 |
+
<!-- Connection Panel -->
|
110 |
+
<div style="border-right: 1px solid #ccc; padding-right: 20px;">
|
111 |
+
<div style="margin-bottom: 20px;">
|
112 |
+
<input type="password" id="apiKey" placeholder="Enter API Key" style="width: 100%;">
|
113 |
+
<button onclick="initializeClient()" style="margin-top: 10px;">Fetch Models</button>
|
114 |
+
</div>
|
115 |
+
<select id="modelSelect" style="width: 100%; margin-bottom: 20px;"></select>
|
116 |
+
<div id="systemStatus" style="color: #666; font-size: 0.9em;"></div>
|
117 |
+
</div>
|
118 |
|
119 |
+
<!-- Message Flow Visualization -->
|
120 |
+
<div style="display: flex; flex-direction: column; height: 100%;">
|
121 |
+
<div id="messageFlow" style="flex: 1; border: 1px solid #eee; padding: 10px; overflow-y: auto; background: #f9f9f9;">
|
122 |
+
<div style="text-align: center; color: #999; margin-bottom: 10px;">Message Flow</div>
|
123 |
+
</div>
|
124 |
+
<div id="detailedStatus" style="color: #666; font-size: 0.9em; margin-top: 10px;"></div>
|
125 |
+
</div>
|
126 |
+
</div>
|
127 |
|
128 |
+
<style>
|
129 |
+
.message-entry {
|
130 |
+
margin: 5px 0;
|
131 |
+
padding: 8px;
|
132 |
+
border-radius: 8px;
|
133 |
+
background: white;
|
134 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
135 |
+
font-family: monospace;
|
136 |
}
|
137 |
+
.incoming { border-left: 4px solid #4CAF50; }
|
138 |
+
.outgoing { border-left: 4px solid #2196F3; }
|
139 |
+
.system { border-left: 4px solid #9C27B0; }
|
140 |
+
.error { border-left: 4px solid #F44336; }
|
141 |
+
.message-header {
|
142 |
+
display: flex;
|
143 |
+
justify-content: space-between;
|
144 |
+
font-size: 0.8em;
|
145 |
+
color: #666;
|
146 |
+
margin-bottom: 4px;
|
147 |
+
}
|
148 |
+
</style>
|
149 |
|
150 |
+
<script>
|
151 |
+
function addMessageEntry(direction, source, destination, content) {
|
152 |
+
const flowDiv = document.getElementById('messageFlow');
|
153 |
+
const timestamp = new Date().toLocaleTimeString();
|
154 |
+
|
155 |
+
const entry = document.createElement('div');
|
156 |
+
entry.className = `message-entry ${direction}`;
|
157 |
+
entry.innerHTML = `
|
158 |
+
<div class="message-header">
|
159 |
+
<span>${source} → ${destination}</span>
|
160 |
+
<span>${timestamp}</span>
|
161 |
+
</div>
|
162 |
+
<div style="white-space: pre-wrap;">${content}</div>
|
163 |
+
`;
|
164 |
+
|
165 |
+
flowDiv.appendChild(entry);
|
166 |
+
flowDiv.scrollTop = flowDiv.scrollHeight;
|
167 |
}
|
168 |
|
169 |
+
// Modified WebSocket handler
|
170 |
const ws = new WebSocket('wss://' + window.location.host + '/ws');
|
|
|
|
|
|
|
|
|
171 |
ws.onmessage = async e => {
|
172 |
const msg = JSON.parse(e.data);
|
173 |
+
|
174 |
+
// Display incoming messages
|
175 |
if (msg.destination === 'proxy') {
|
176 |
+
addMessageEntry('incoming', msg.source, 'proxy', msg.content);
|
177 |
+
document.getElementById('detailedStatus').textContent = `Processing ${msg.source} request...`;
|
178 |
+
|
179 |
try {
|
180 |
+
const llmResponse = await agentClient.call(currentModel, msg.content, systemPrompt, conversationHistory);
|
181 |
+
|
182 |
+
// Display outgoing response
|
183 |
+
addMessageEntry('outgoing', 'proxy', msg.source, llmResponse.response);
|
|
|
|
|
|
|
184 |
|
185 |
const responseMsg = {
|
186 |
content: llmResponse.response,
|
187 |
source: 'proxy',
|
188 |
+
destination: msg.source
|
189 |
};
|
190 |
ws.send(JSON.stringify(responseMsg));
|
191 |
+
|
192 |
+
document.getElementById('detailedStatus').textContent = `Response sent to ${msg.source}`;
|
193 |
} catch (error) {
|
194 |
+
addMessageEntry('error', 'system', 'proxy', `Error: ${error.message}`);
|
195 |
const errorResponse = {
|
196 |
+
content: `Error: ${error.message}`,
|
197 |
source: 'proxy',
|
198 |
+
destination: msg.source
|
199 |
};
|
200 |
ws.send(JSON.stringify(errorResponse));
|
|
|
201 |
}
|
202 |
}
|
203 |
};
|
204 |
|
205 |
+
// Modified model initialization
|
206 |
+
function showStatus(message, type = 'info') {
|
207 |
+
const statusDiv = document.getElementById('systemStatus');
|
208 |
+
statusDiv.innerHTML = `<div style="color: ${type === 'error' ? '#F44336' : '#4CAF50'}">${message}</div>`;
|
209 |
+
addMessageEntry('system', 'system', 'proxy', message);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
}
|
|
|
|
|
211 |
</script>
|
212 |
</body>
|
213 |
</html>
|
214 |
""")
|
215 |
|
216 |
|
217 |
+
|
218 |
@app.post("/v1/chat/completions", response_model=ChatCompletionResponse)
|
219 |
async def chat_completions(request: ChatCompletionRequest):
|
220 |
request_id = str(uuid.uuid4())
|