Update api/utils.py
Browse files- api/utils.py +31 -1
api/utils.py
CHANGED
@@ -17,7 +17,7 @@ async def process_streaming_response(request: ChatRequest) -> AsyncGenerator[str
|
|
17 |
messages = [msg.dict() for msg in request.messages]
|
18 |
|
19 |
try:
|
20 |
-
async for content in AmigoChat.generate_response(
|
21 |
model=request.model,
|
22 |
messages=messages,
|
23 |
stream=True
|
@@ -58,3 +58,33 @@ async def process_streaming_response(request: ChatRequest) -> AsyncGenerator[str
|
|
58 |
except Exception as e:
|
59 |
logger.error(f"Error in streaming response: {e}")
|
60 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
messages = [msg.dict() for msg in request.messages]
|
18 |
|
19 |
try:
|
20 |
+
async for content in await AmigoChat.generate_response(
|
21 |
model=request.model,
|
22 |
messages=messages,
|
23 |
stream=True
|
|
|
58 |
except Exception as e:
|
59 |
logger.error(f"Error in streaming response: {e}")
|
60 |
raise HTTPException(status_code=500, detail=str(e))
|
61 |
+
|
62 |
+
async def process_non_streaming_response(request: ChatRequest):
|
63 |
+
logger.info("Processing non-streaming response with AmigoChat")
|
64 |
+
messages = [msg.dict() for msg in request.messages]
|
65 |
+
|
66 |
+
try:
|
67 |
+
content = await AmigoChat.generate_response(
|
68 |
+
model=request.model,
|
69 |
+
messages=messages,
|
70 |
+
stream=False
|
71 |
+
)
|
72 |
+
|
73 |
+
return {
|
74 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
75 |
+
"object": "chat.completion",
|
76 |
+
"created": int(datetime.now().timestamp()),
|
77 |
+
"model": request.model,
|
78 |
+
"choices": [
|
79 |
+
{
|
80 |
+
"index": 0,
|
81 |
+
"message": {"role": "assistant", "content": content},
|
82 |
+
"finish_reason": "stop",
|
83 |
+
}
|
84 |
+
],
|
85 |
+
"usage": None,
|
86 |
+
}
|
87 |
+
|
88 |
+
except Exception as e:
|
89 |
+
logger.error(f"Error in non-streaming response: {e}")
|
90 |
+
raise HTTPException(status_code=500, detail=str(e))
|