Niansuh commited on
Commit
d86b151
·
verified ·
1 Parent(s): 094d631

Update api/routes.py

Browse files
Files changed (1) hide show
  1. api/routes.py +16 -6
api/routes.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  import json
2
  from fastapi import APIRouter, Depends, HTTPException, Request, Response
3
  from fastapi.responses import StreamingResponse
@@ -39,17 +41,25 @@ async def chat_completions(
39
  # Validate if the requested model is allowed
40
  allowed_model_ids = [model["id"] for model in ALLOWED_MODELS]
41
  if request.model not in allowed_model_ids:
 
42
  raise HTTPException(
43
  status_code=400,
44
  detail=f"Model {request.model} is not allowed. Allowed models are: {', '.join(allowed_model_ids)}",
45
  )
46
 
47
- if request.stream:
48
- logger.info("Streaming response")
49
- return StreamingResponse(process_streaming_response(request), media_type="text/event-stream")
50
- else:
51
- logger.info("Non-streaming response")
52
- return await process_non_streaming_response(request)
 
 
 
 
 
 
 
53
 
54
  @router.get("/health")
55
  @router.get("/")
 
1
+ # api/routes.py
2
+
3
  import json
4
  from fastapi import APIRouter, Depends, HTTPException, Request, Response
5
  from fastapi.responses import StreamingResponse
 
41
  # Validate if the requested model is allowed
42
  allowed_model_ids = [model["id"] for model in ALLOWED_MODELS]
43
  if request.model not in allowed_model_ids:
44
+ logger.warning(f"Model {request.model} is not allowed.")
45
  raise HTTPException(
46
  status_code=400,
47
  detail=f"Model {request.model} is not allowed. Allowed models are: {', '.join(allowed_model_ids)}",
48
  )
49
 
50
+ try:
51
+ if request.stream:
52
+ logger.info("Streaming response")
53
+ return StreamingResponse(process_streaming_response(request), media_type="text/event-stream")
54
+ else:
55
+ logger.info("Non-streaming response")
56
+ return await process_non_streaming_response(request)
57
+ except HTTPException as e:
58
+ logger.error(f"HTTPException: {e.detail}")
59
+ raise e
60
+ except Exception as e:
61
+ logger.error(f"Unexpected error: {str(e)}")
62
+ raise HTTPException(status_code=500, detail="Internal Server Error")
63
 
64
  @router.get("/health")
65
  @router.get("/")