Niansuh commited on
Commit
e2aea7d
·
verified ·
1 Parent(s): 09f58ca

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +42 -42
main.py CHANGED
@@ -328,56 +328,56 @@ class ChatRequest(BaseModel):
328
  user: Optional[str] = None
329
 
330
  @app.post("/v1/chat/completions", dependencies=[Depends(rate_limiter_per_ip)])
331
- async def chat_completions(request: ChatRequest, req: Request, api_key: str = Depends(get_api_key)):
332
  client_ip = req.client.host
333
  # Redact user messages only for logging purposes
334
  redacted_messages = [{"role": msg.role, "content": "[redacted]"} for msg in request.messages]
335
 
336
  logger.info(f"Received chat completions request from API key: {api_key} | IP: {client_ip} | Model: {request.model} | Messages: {redacted_messages}")
337
 
338
- try:
339
- # Validate that the requested model is available
340
- if request.model not in Blackbox.models and request.model not in Blackbox.model_aliases:
341
- logger.warning(f"Attempt to use unavailable model: {request.model} from IP: {client_ip}")
342
- raise HTTPException(status_code=400, detail="Requested model is not available.")
343
-
344
- # Process the request with actual message content, but don't log it
345
- response_content = await Blackbox.create_completion(
346
- model=request.model,
347
- messages=[{"role": msg.role, "content": msg.content} for msg in request.messages], # Actual message content used here
348
- )
349
-
350
- logger.info(f"Completed response generation for API key: {api_key} | IP: {client_ip}")
351
- return {
352
- "id": f"chatcmpl-{uuid.uuid4()}",
353
- "object": "chat.completion",
354
- "created": int(datetime.now().timestamp()),
355
- "model": request.model,
356
- "choices": [
357
- {
358
- "message": {
359
- "role": "assistant",
360
- "content": response_content
361
- },
362
- "finish_reason": "stop",
363
- "index": 0
 
 
 
 
 
 
364
  }
365
- ],
366
- "usage": {
367
- "prompt_tokens": sum(len(msg.content.split()) for msg in request.messages),
368
- "completion_tokens": len(response_content.split()),
369
- "total_tokens": sum(len(msg.content.split()) for msg in request.messages) + len(response_content.split())
370
  }
371
- } # Closing the dictionary here
372
- except ModelNotWorkingException as e:
373
- logger.warning(f"Model not working: {e} | IP: {client_ip}")
374
- raise HTTPException(status_code=503, detail=str(e))
375
- except HTTPException as he:
376
- logger.warning(f"HTTPException: {he.detail} | IP: {client_ip}")
377
- raise he
378
- except Exception as e:
379
- logger.exception(f"An unexpected error occurred while processing the chat completions request from IP: {client_ip}.")
380
- raise HTTPException(status_code=500, detail=str(e))
381
 
382
 
383
  # Endpoint: GET /v1/models
 
328
  user: Optional[str] = None
329
 
330
  @app.post("/v1/chat/completions", dependencies=[Depends(rate_limiter_per_ip)])
331
+ async def chat_completions(request: ChatRequest, req: Request, api_key: str = Depends(get_api_key)): # Make sure this function is async
332
  client_ip = req.client.host
333
  # Redact user messages only for logging purposes
334
  redacted_messages = [{"role": msg.role, "content": "[redacted]"} for msg in request.messages]
335
 
336
  logger.info(f"Received chat completions request from API key: {api_key} | IP: {client_ip} | Model: {request.model} | Messages: {redacted_messages}")
337
 
338
+ try:
339
+ # Validate that the requested model is available
340
+ if request.model not in Blackbox.models and request.model not in Blackbox.model_aliases:
341
+ logger.warning(f"Attempt to use unavailable model: {request.model} from IP: {client_ip}")
342
+ raise HTTPException(status_code=400, detail="Requested model is not available.")
343
+
344
+ # Process the request with actual message content, but don't log it
345
+ response_content = await Blackbox.create_completion( # Correct usage of await inside async function
346
+ model=request.model,
347
+ messages=[{"role": msg.role, "content": msg.content} for msg in request.messages], # Actual message content used here
348
+ )
349
+
350
+ logger.info(f"Completed response generation for API key: {api_key} | IP: {client_ip}")
351
+ return {
352
+ "id": f"chatcmpl-{uuid.uuid4()}",
353
+ "object": "chat.completion",
354
+ "created": int(datetime.now().timestamp()),
355
+ "model": request.model,
356
+ "choices": [
357
+ {
358
+ "message": {
359
+ "role": "assistant",
360
+ "content": response_content
361
+ },
362
+ "finish_reason": "stop",
363
+ "index": 0
364
+ }
365
+ ],
366
+ "usage": {
367
+ "prompt_tokens": sum(len(msg.content.split()) for msg in request.messages),
368
+ "completion_tokens": len(response_content.split()),
369
+ "total_tokens": sum(len(msg.content.split()) for msg in request.messages) + len(response_content.split())
370
  }
 
 
 
 
 
371
  }
372
+ except ModelNotWorkingException as e:
373
+ logger.warning(f"Model not working: {e} | IP: {client_ip}")
374
+ raise HTTPException(status_code=503, detail=str(e))
375
+ except HTTPException as he:
376
+ logger.warning(f"HTTPException: {he.detail} | IP: {client_ip}")
377
+ raise he
378
+ except Exception as e:
379
+ logger.exception(f"An unexpected error occurred while processing the chat completions request from IP: {client_ip}.")
380
+ raise HTTPException(status_code=500, detail=str(e))
 
381
 
382
 
383
  # Endpoint: GET /v1/models