Almaatla commited on
Commit
f4c8797
·
verified ·
1 Parent(s): 414e28c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -8
app.py CHANGED
@@ -109,7 +109,12 @@ async def get_proxy():
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>
@@ -301,6 +306,16 @@ async def get_proxy():
301
  if (msg.destination === 'proxy') {
302
  addMessageEntry('incoming', msg.source, 'proxy', msg.content);
303
  document.getElementById('detailedStatus').textContent = `Processing ${msg.source} request...`;
 
 
 
 
 
 
 
 
 
 
304
 
305
  try {
306
  const llmResponse = await agentClient.call(currentModel, msg.content, systemPrompt, conversationHistory);
@@ -344,32 +359,45 @@ async def get_proxy():
344
 
345
 
346
  @app.post("/v1/chat/completions", response_model=ChatCompletionResponse)
347
- async def chat_completions(request: ChatCompletionRequest):
 
 
 
 
 
 
 
 
 
 
 
348
  request_id = str(uuid.uuid4())
349
- # Find proxy connection
350
  proxy_ws = next((ws for ws, src in manager.active_connections.items() if src == 'proxy'), None)
351
  if not proxy_ws:
352
  raise HTTPException(503, "Proxy client not connected")
353
- # Get user message
354
  user_message = next((m for m in request.messages if m.role == "user"), None)
355
  if not user_message:
356
  raise HTTPException(400, "No user message found")
357
- # Send to proxy
 
358
  proxy_msg = {
359
  "request_id": request_id,
360
  "content": user_message.content,
361
  "source": "api",
362
  "destination": "proxy",
363
  "model": request.model,
364
- "temperature": request.temperature
 
365
  }
 
366
  await proxy_ws.send_text(json.dumps(proxy_msg))
367
- # Wait for response from proxy
368
  try:
369
  response_content = await manager.wait_for_response(request_id)
370
  except asyncio.TimeoutError:
371
  raise HTTPException(504, "Proxy response timeout")
372
- # Return OpenAI-compatible response
373
  return ChatCompletionResponse(
374
  id=request_id,
375
  created=int(time.time()),
@@ -380,6 +408,7 @@ async def chat_completions(request: ChatCompletionRequest):
380
  )
381
 
382
 
 
383
  @app.websocket("/ws")
384
  async def websocket_endpoint(websocket: WebSocket):
385
  await manager.connect(websocket)
 
109
  <!-- Connection Panel -->
110
  <div style="border-right: 1px solid #ccc; padding-right: 20px;">
111
  <div style="margin-bottom: 20px;">
112
+ <div style="margin-bottom: 20px;">
113
+ <input type="password" id="apiKey" placeholder="LLM API Key" style="width: 100%; margin-bottom: 8px;">
114
+ <input type="password" id="incomingKey" placeholder="Proxy Incoming Key" style="width: 100%;">
115
+ <button onclick="initializeClient()" style="margin-top: 10px;">Connect</button>
116
+ </div>
117
+
118
  <button onclick="initializeClient()" style="margin-top: 10px;">Fetch Models</button>
119
  </div>
120
  <select id="modelSelect" style="width: 100%; margin-bottom: 20px;"></select>
 
306
  if (msg.destination === 'proxy') {
307
  addMessageEntry('incoming', msg.source, 'proxy', msg.content);
308
  document.getElementById('detailedStatus').textContent = `Processing ${msg.source} request...`;
309
+ // check if incoming call has the correct key
310
+ const expected_incoming_key = document.getElementById('incomingKey');
311
+ if (msg.incomingKey != expected_incoming_key){
312
+ return {
313
+ request_id: msg.request_id, // Critical addition
314
+ content: "Error: Incoming Authentication Key incorrect!",
315
+ source: 'proxy',
316
+ destination: msg.source
317
+ };
318
+ }
319
 
320
  try {
321
  const llmResponse = await agentClient.call(currentModel, msg.content, systemPrompt, conversationHistory);
 
359
 
360
 
361
  @app.post("/v1/chat/completions", response_model=ChatCompletionResponse)
362
+ async def chat_completions(
363
+ request: ChatCompletionRequest,
364
+ authorization: str = Header(None) # Add Authorization header
365
+ ):
366
+ # Extract and validate API key
367
+ if not authorization or not authorization.startswith("Bearer "):
368
+ raise HTTPException(
369
+ status_code=status.HTTP_401_UNAUTHORIZED,
370
+ detail="Missing or invalid Authorization header"
371
+ )
372
+ api_key = authorization[7:] # Remove "Bearer " prefix
373
+
374
  request_id = str(uuid.uuid4())
 
375
  proxy_ws = next((ws for ws, src in manager.active_connections.items() if src == 'proxy'), None)
376
  if not proxy_ws:
377
  raise HTTPException(503, "Proxy client not connected")
378
+
379
  user_message = next((m for m in request.messages if m.role == "user"), None)
380
  if not user_message:
381
  raise HTTPException(400, "No user message found")
382
+
383
+ # Add API key to proxy message
384
  proxy_msg = {
385
  "request_id": request_id,
386
  "content": user_message.content,
387
  "source": "api",
388
  "destination": "proxy",
389
  "model": request.model,
390
+ "temperature": request.temperature,
391
+ "incoming_key": api_key # Critical addition
392
  }
393
+
394
  await proxy_ws.send_text(json.dumps(proxy_msg))
395
+
396
  try:
397
  response_content = await manager.wait_for_response(request_id)
398
  except asyncio.TimeoutError:
399
  raise HTTPException(504, "Proxy response timeout")
400
+
401
  return ChatCompletionResponse(
402
  id=request_id,
403
  created=int(time.time()),
 
408
  )
409
 
410
 
411
+
412
  @app.websocket("/ws")
413
  async def websocket_endpoint(websocket: WebSocket):
414
  await manager.connect(websocket)