Update api/utils.py
Browse files- api/utils.py +14 -8
api/utils.py
CHANGED
@@ -1,10 +1,12 @@
|
|
1 |
-
|
|
|
2 |
import json
|
3 |
-
from
|
4 |
import uuid
|
5 |
-
import
|
6 |
|
7 |
import httpx
|
|
|
8 |
from api.config import (
|
9 |
MODEL_MAPPING,
|
10 |
MODEL_ALIASES,
|
@@ -13,7 +15,6 @@ from api.config import (
|
|
13 |
TRENDING_AGENT_MODE,
|
14 |
BASE_URL
|
15 |
)
|
16 |
-
from fastapi import HTTPException
|
17 |
from api.models import ChatRequest
|
18 |
|
19 |
from api.logger import setup_logger
|
@@ -100,23 +101,26 @@ async def process_streaming_response(request: ChatRequest):
|
|
100 |
timeout=100,
|
101 |
) as response:
|
102 |
response.raise_for_status()
|
|
|
103 |
async for line in response.aiter_lines():
|
104 |
timestamp = int(datetime.now().timestamp())
|
105 |
if line:
|
106 |
content = line
|
107 |
if content.startswith("$@$v=undefined-rv1$@$"):
|
108 |
content = content[21:]
|
|
|
109 |
yield f"data: {json.dumps(create_chat_completion_data(content, model, timestamp))}\n\n"
|
110 |
|
111 |
# Indicate the end of the stream
|
112 |
timestamp = int(datetime.now().timestamp())
|
|
|
113 |
yield f"data: {json.dumps(create_chat_completion_data('', model, timestamp, 'stop'))}\n\n"
|
114 |
yield "data: [DONE]\n\n"
|
115 |
except httpx.HTTPStatusError as e:
|
116 |
-
logger.error(f"HTTP error occurred: {e}")
|
117 |
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
118 |
except httpx.RequestError as e:
|
119 |
-
logger.error(f"
|
120 |
raise HTTPException(status_code=500, detail=str(e))
|
121 |
|
122 |
|
@@ -163,13 +167,15 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
163 |
json=json_data,
|
164 |
) as response:
|
165 |
response.raise_for_status()
|
|
|
166 |
async for chunk in response.aiter_text():
|
167 |
full_response += chunk
|
|
|
168 |
except httpx.HTTPStatusError as e:
|
169 |
-
logger.error(f"HTTP error occurred: {e}")
|
170 |
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
171 |
except httpx.RequestError as e:
|
172 |
-
logger.error(f"
|
173 |
raise HTTPException(status_code=500, detail=str(e))
|
174 |
|
175 |
if full_response.startswith("$@$v=undefined-rv1$@$"):
|
|
|
1 |
+
# api/utils.py
|
2 |
+
|
3 |
import json
|
4 |
+
from datetime import datetime
|
5 |
import uuid
|
6 |
+
from typing import Any, Dict, Optional
|
7 |
|
8 |
import httpx
|
9 |
+
from fastapi import HTTPException
|
10 |
from api.config import (
|
11 |
MODEL_MAPPING,
|
12 |
MODEL_ALIASES,
|
|
|
15 |
TRENDING_AGENT_MODE,
|
16 |
BASE_URL
|
17 |
)
|
|
|
18 |
from api.models import ChatRequest
|
19 |
|
20 |
from api.logger import setup_logger
|
|
|
101 |
timeout=100,
|
102 |
) as response:
|
103 |
response.raise_for_status()
|
104 |
+
logger.info(f"Received response status: {response.status_code}")
|
105 |
async for line in response.aiter_lines():
|
106 |
timestamp = int(datetime.now().timestamp())
|
107 |
if line:
|
108 |
content = line
|
109 |
if content.startswith("$@$v=undefined-rv1$@$"):
|
110 |
content = content[21:]
|
111 |
+
logger.debug(f"Streaming content: {content}")
|
112 |
yield f"data: {json.dumps(create_chat_completion_data(content, model, timestamp))}\n\n"
|
113 |
|
114 |
# Indicate the end of the stream
|
115 |
timestamp = int(datetime.now().timestamp())
|
116 |
+
logger.debug("Ending streaming response")
|
117 |
yield f"data: {json.dumps(create_chat_completion_data('', model, timestamp, 'stop'))}\n\n"
|
118 |
yield "data: [DONE]\n\n"
|
119 |
except httpx.HTTPStatusError as e:
|
120 |
+
logger.error(f"HTTP error occurred: {e.response.status_code} - {e.response.text}")
|
121 |
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
122 |
except httpx.RequestError as e:
|
123 |
+
logger.error(f"Request error occurred: {str(e)}")
|
124 |
raise HTTPException(status_code=500, detail=str(e))
|
125 |
|
126 |
|
|
|
167 |
json=json_data,
|
168 |
) as response:
|
169 |
response.raise_for_status()
|
170 |
+
logger.info(f"Received response status: {response.status_code}")
|
171 |
async for chunk in response.aiter_text():
|
172 |
full_response += chunk
|
173 |
+
logger.debug(f"Received chunk: {chunk}")
|
174 |
except httpx.HTTPStatusError as e:
|
175 |
+
logger.error(f"HTTP error occurred: {e.response.status_code} - {e.response.text}")
|
176 |
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
177 |
except httpx.RequestError as e:
|
178 |
+
logger.error(f"Request error occurred: {str(e)}")
|
179 |
raise HTTPException(status_code=500, detail=str(e))
|
180 |
|
181 |
if full_response.startswith("$@$v=undefined-rv1$@$"):
|