Update api/utils.py
Browse files- api/utils.py +19 -56
api/utils.py
CHANGED
|
@@ -12,86 +12,49 @@ from api.providers import AmigoChat
|
|
| 12 |
|
| 13 |
logger = setup_logger(__name__)
|
| 14 |
|
| 15 |
-
async def process_streaming_response(request: ChatRequest):
|
| 16 |
logger.info("Processing streaming response with AmigoChat")
|
| 17 |
messages = [msg.dict() for msg in request.messages]
|
| 18 |
|
| 19 |
try:
|
| 20 |
-
async
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
chunk = {
|
| 28 |
-
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 29 |
-
"object": "chat.completion.chunk",
|
| 30 |
-
"created": timestamp,
|
| 31 |
-
"model": request.model,
|
| 32 |
-
"choices": [
|
| 33 |
-
{
|
| 34 |
-
"index": 0,
|
| 35 |
-
"delta": {"content": content},
|
| 36 |
-
"finish_reason": None,
|
| 37 |
-
}
|
| 38 |
-
],
|
| 39 |
-
}
|
| 40 |
-
yield f"data: {json.dumps(chunk)}\n\n"
|
| 41 |
-
|
| 42 |
-
# Indicate the end of the stream
|
| 43 |
-
end_chunk = {
|
| 44 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 45 |
"object": "chat.completion.chunk",
|
| 46 |
-
"created":
|
| 47 |
"model": request.model,
|
| 48 |
"choices": [
|
| 49 |
{
|
| 50 |
"index": 0,
|
| 51 |
-
"delta": {},
|
| 52 |
-
"finish_reason":
|
| 53 |
}
|
| 54 |
],
|
| 55 |
}
|
| 56 |
-
yield f"data: {json.dumps(
|
| 57 |
-
yield "data: [DONE]\n\n"
|
| 58 |
-
|
| 59 |
-
return event_generator()
|
| 60 |
-
|
| 61 |
-
except Exception as e:
|
| 62 |
-
logger.error(f"Error in streaming response: {e}")
|
| 63 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
messages = [msg.dict() for msg in request.messages]
|
| 68 |
-
|
| 69 |
-
try:
|
| 70 |
-
responses = []
|
| 71 |
-
async for content in AmigoChat.generate_response(
|
| 72 |
-
model=request.model,
|
| 73 |
-
messages=messages,
|
| 74 |
-
stream=False
|
| 75 |
-
):
|
| 76 |
-
responses.append(content)
|
| 77 |
-
|
| 78 |
-
full_response = ''.join(responses)
|
| 79 |
-
|
| 80 |
-
return {
|
| 81 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 82 |
-
"object": "chat.completion",
|
| 83 |
"created": int(datetime.now().timestamp()),
|
| 84 |
"model": request.model,
|
| 85 |
"choices": [
|
| 86 |
{
|
| 87 |
"index": 0,
|
| 88 |
-
"
|
| 89 |
"finish_reason": "stop",
|
| 90 |
}
|
| 91 |
],
|
| 92 |
-
"usage": None,
|
| 93 |
}
|
|
|
|
|
|
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
-
logger.error(f"Error in
|
| 97 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 12 |
|
| 13 |
logger = setup_logger(__name__)
|
| 14 |
|
| 15 |
+
async def process_streaming_response(request: ChatRequest) -> AsyncGenerator[str, None]:
|
| 16 |
logger.info("Processing streaming response with AmigoChat")
|
| 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
|
| 24 |
+
):
|
| 25 |
+
timestamp = int(datetime.now().timestamp())
|
| 26 |
+
chunk = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 28 |
"object": "chat.completion.chunk",
|
| 29 |
+
"created": timestamp,
|
| 30 |
"model": request.model,
|
| 31 |
"choices": [
|
| 32 |
{
|
| 33 |
"index": 0,
|
| 34 |
+
"delta": {"content": content},
|
| 35 |
+
"finish_reason": None,
|
| 36 |
}
|
| 37 |
],
|
| 38 |
}
|
| 39 |
+
yield f"data: {json.dumps(chunk)}\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Indicate the end of the stream
|
| 42 |
+
end_chunk = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 44 |
+
"object": "chat.completion.chunk",
|
| 45 |
"created": int(datetime.now().timestamp()),
|
| 46 |
"model": request.model,
|
| 47 |
"choices": [
|
| 48 |
{
|
| 49 |
"index": 0,
|
| 50 |
+
"delta": {},
|
| 51 |
"finish_reason": "stop",
|
| 52 |
}
|
| 53 |
],
|
|
|
|
| 54 |
}
|
| 55 |
+
yield f"data: {json.dumps(end_chunk)}\n\n"
|
| 56 |
+
yield "data: [DONE]\n\n"
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
+
logger.error(f"Error in streaming response: {e}")
|
| 60 |
raise HTTPException(status_code=500, detail=str(e))
|