Update api/utils.py
Browse files- api/utils.py +20 -8
api/utils.py
CHANGED
|
@@ -141,6 +141,7 @@ async def process_streaming_response(request: ChatRequest):
|
|
| 141 |
"imageGenerationMode": False, # Added this line
|
| 142 |
}
|
| 143 |
|
|
|
|
| 144 |
async with httpx.AsyncClient() as client:
|
| 145 |
try:
|
| 146 |
async with client.stream(
|
|
@@ -166,15 +167,26 @@ async def process_streaming_response(request: ChatRequest):
|
|
| 166 |
if not content:
|
| 167 |
continue # Skip if content is empty after removal
|
| 168 |
cleaned_content = strip_model_prefix(content, model_prefix)
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
-
yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
|
| 177 |
-
yield "data: [DONE]\n\n"
|
| 178 |
except httpx.HTTPStatusError as e:
|
| 179 |
logger.error(f"HTTP error occurred for Request ID {request_id}: {e}")
|
| 180 |
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
|
@@ -283,7 +295,7 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
| 283 |
|
| 284 |
cleaned_full_response = strip_model_prefix(full_response, model_prefix)
|
| 285 |
|
| 286 |
-
# Append the advertisement text
|
| 287 |
if ADVERTISEMENT_TEXT:
|
| 288 |
cleaned_full_response += "\n\n" + ADVERTISEMENT_TEXT
|
| 289 |
|
|
|
|
| 141 |
"imageGenerationMode": False, # Added this line
|
| 142 |
}
|
| 143 |
|
| 144 |
+
response_chunks = []
|
| 145 |
async with httpx.AsyncClient() as client:
|
| 146 |
try:
|
| 147 |
async with client.stream(
|
|
|
|
| 167 |
if not content:
|
| 168 |
continue # Skip if content is empty after removal
|
| 169 |
cleaned_content = strip_model_prefix(content, model_prefix)
|
| 170 |
+
response_chunks.append(create_chat_completion_data(cleaned_content, request.model, timestamp))
|
| 171 |
+
|
| 172 |
+
# At the very end, add the advertisement text once
|
| 173 |
+
if ADVERTISEMENT_TEXT:
|
| 174 |
+
# If there are chunks already, update the last one with the advertisement text.
|
| 175 |
+
if response_chunks:
|
| 176 |
+
last_chunk = response_chunks[-1]
|
| 177 |
+
last_chunk["choices"][0]["delta"]["content"] += "\n\n" + ADVERTISEMENT_TEXT
|
| 178 |
+
else:
|
| 179 |
+
# If no chunks are returned, just create an empty response with the ad.
|
| 180 |
+
response_chunks.append(create_chat_completion_data(ADVERTISEMENT_TEXT, request.model, timestamp))
|
| 181 |
|
| 182 |
+
# Finalize the response
|
| 183 |
+
response_chunks.append(create_chat_completion_data('', request.model, timestamp, 'stop'))
|
| 184 |
+
response_chunks.append({"data": "[DONE]\n\n"})
|
| 185 |
+
|
| 186 |
+
# Yield each chunk as part of the stream response
|
| 187 |
+
for chunk in response_chunks:
|
| 188 |
+
yield f"data: {json.dumps(chunk)}\n\n"
|
| 189 |
|
|
|
|
|
|
|
| 190 |
except httpx.HTTPStatusError as e:
|
| 191 |
logger.error(f"HTTP error occurred for Request ID {request_id}: {e}")
|
| 192 |
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
|
|
|
| 295 |
|
| 296 |
cleaned_full_response = strip_model_prefix(full_response, model_prefix)
|
| 297 |
|
| 298 |
+
# Append the advertisement text only once at the end
|
| 299 |
if ADVERTISEMENT_TEXT:
|
| 300 |
cleaned_full_response += "\n\n" + ADVERTISEMENT_TEXT
|
| 301 |
|