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

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +43 -46
main.py CHANGED
@@ -335,53 +335,50 @@ async def chat_completions(request: ChatRequest, req: Request, api_key: str = De
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.logger.info(f"Completed response generation for API key: {api_key} | IP: {client_ip}")"
354
- return {
355
- "id": f"chatcmpl-{uuid.uuid4()}",
356
- "object": "chat.completion",
357
- "created": int(datetime.now().timestamp()),
358
- "model": request.model,
359
- "choices": [
360
- {
361
- "message": {
362
- "role": "assistant",
363
- "content": response_content
364
- },
365
- "finish_reason": "stop",
366
- "index": 0
 
 
 
367
  }
368
- ],
369
- "usage": {
370
- "prompt_tokens": sum(len(msg.content.split()) for msg in request.messages),
371
- "completion_tokens": len(response_content.split()),
372
- "total_tokens": sum(len(msg.content.split()) for msg in request.messages) + len(response_content.split())
373
- }
374
- } # This closing curly brace was missing
375
-
376
- except ModelNotWorkingException as e:
377
- logger.warning(f"Model not working: {e} | IP: {client_ip}")
378
- raise HTTPException(status_code=503, detail=str(e))
379
- except HTTPException as he:
380
- logger.warning(f"HTTPException: {he.detail} | IP: {client_ip}")
381
- raise he
382
- except Exception as e:
383
- logger.exception(f"An unexpected error occurred while processing the chat completions request from IP: {client_ip}.")
384
- raise HTTPException(status_code=500, detail=str(e))
385
 
386
  # Endpoint: GET /v1/models
387
  @app.get("/v1/models", dependencies=[Depends(rate_limiter_per_ip)])
 
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
384
  @app.get("/v1/models", dependencies=[Depends(rate_limiter_per_ip)])