Update api/utils.py
Browse files- api/utils.py +3 -19
api/utils.py
CHANGED
@@ -3,7 +3,6 @@ import json
|
|
3 |
import uuid
|
4 |
import asyncio
|
5 |
import random
|
6 |
-
import string
|
7 |
from typing import Any, Dict, Optional
|
8 |
|
9 |
import httpx
|
@@ -60,14 +59,6 @@ def message_to_dict(message, model_prefix: Optional[str] = None):
|
|
60 |
}
|
61 |
return {"role": message.role, "content": content}
|
62 |
|
63 |
-
# Function to strip model prefix from content if present
|
64 |
-
def strip_model_prefix(content: str, model_prefix: Optional[str] = None) -> str:
|
65 |
-
"""Remove the model prefix from the response content if present."""
|
66 |
-
if model_prefix and content.startswith(model_prefix):
|
67 |
-
logger.debug(f"Stripping prefix '{model_prefix}' from content.")
|
68 |
-
return content[len(model_prefix):].strip()
|
69 |
-
return content
|
70 |
-
|
71 |
# Process streaming response with headers from config.py
|
72 |
async def process_streaming_response(request: ChatRequest):
|
73 |
logger.info(f"Processing streaming response for Model: {request.model}")
|
@@ -121,11 +112,8 @@ async def process_streaming_response(request: ChatRequest):
|
|
121 |
async for line in response.aiter_lines():
|
122 |
timestamp = int(datetime.now().timestamp())
|
123 |
if line:
|
124 |
-
|
125 |
-
|
126 |
-
content = content[21:]
|
127 |
-
cleaned_content = strip_model_prefix(content, model_prefix)
|
128 |
-
yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
|
129 |
|
130 |
yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
|
131 |
yield "data: [DONE]\n\n"
|
@@ -191,10 +179,6 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
191 |
except httpx.RequestError as e:
|
192 |
logger.error(f"Error occurred during request: {e}")
|
193 |
raise HTTPException(status_code=500, detail=str(e))
|
194 |
-
if full_response.startswith("$@$v=undefined-rv1$@$"):
|
195 |
-
full_response = full_response[21:]
|
196 |
-
|
197 |
-
cleaned_full_response = strip_model_prefix(full_response, model_prefix)
|
198 |
|
199 |
return {
|
200 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
@@ -204,7 +188,7 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
204 |
"choices": [
|
205 |
{
|
206 |
"index": 0,
|
207 |
-
"message": {"role": "assistant", "content":
|
208 |
"finish_reason": "stop",
|
209 |
}
|
210 |
],
|
|
|
3 |
import uuid
|
4 |
import asyncio
|
5 |
import random
|
|
|
6 |
from typing import Any, Dict, Optional
|
7 |
|
8 |
import httpx
|
|
|
59 |
}
|
60 |
return {"role": message.role, "content": content}
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
# Process streaming response with headers from config.py
|
63 |
async def process_streaming_response(request: ChatRequest):
|
64 |
logger.info(f"Processing streaming response for Model: {request.model}")
|
|
|
112 |
async for line in response.aiter_lines():
|
113 |
timestamp = int(datetime.now().timestamp())
|
114 |
if line:
|
115 |
+
# Directly yield each line without additional processing
|
116 |
+
yield f"data: {json.dumps(create_chat_completion_data(line, request.model, timestamp))}\n\n"
|
|
|
|
|
|
|
117 |
|
118 |
yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
|
119 |
yield "data: [DONE]\n\n"
|
|
|
179 |
except httpx.RequestError as e:
|
180 |
logger.error(f"Error occurred during request: {e}")
|
181 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
182 |
|
183 |
return {
|
184 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
|
|
188 |
"choices": [
|
189 |
{
|
190 |
"index": 0,
|
191 |
+
"message": {"role": "assistant", "content": full_response},
|
192 |
"finish_reason": "stop",
|
193 |
}
|
194 |
],
|