Update api/utils.py
Browse files- api/utils.py +5 -9
api/utils.py
CHANGED
|
@@ -101,7 +101,6 @@ async def process_streaming_response(request: ChatRequest):
|
|
| 101 |
"imageGenerationMode": False,
|
| 102 |
}
|
| 103 |
|
| 104 |
-
response_content = "" # Store accumulated response content
|
| 105 |
advertisement_added = False # Track if advertisement is already added
|
| 106 |
|
| 107 |
async with httpx.AsyncClient() as client:
|
|
@@ -125,19 +124,16 @@ async def process_streaming_response(request: ChatRequest):
|
|
| 125 |
|
| 126 |
cleaned_content = strip_model_prefix(content, model_prefix)
|
| 127 |
|
| 128 |
-
#
|
| 129 |
-
response_content += cleaned_content
|
| 130 |
-
|
| 131 |
-
# Yield cleaned chunk as part of the stream (do not repeat this later)
|
| 132 |
yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
|
| 133 |
|
| 134 |
-
# Append advertisement
|
| 135 |
if ADVERTISEMENT_TEXT and not advertisement_added:
|
| 136 |
-
|
| 137 |
advertisement_added = True
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
yield f"data: {json.dumps(create_chat_completion_data(
|
| 141 |
yield "data: [DONE]\n\n"
|
| 142 |
|
| 143 |
except httpx.HTTPStatusError as e:
|
|
|
|
| 101 |
"imageGenerationMode": False,
|
| 102 |
}
|
| 103 |
|
|
|
|
| 104 |
advertisement_added = False # Track if advertisement is already added
|
| 105 |
|
| 106 |
async with httpx.AsyncClient() as client:
|
|
|
|
| 124 |
|
| 125 |
cleaned_content = strip_model_prefix(content, model_prefix)
|
| 126 |
|
| 127 |
+
# Yield cleaned chunk as part of the stream
|
|
|
|
|
|
|
|
|
|
| 128 |
yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
|
| 129 |
|
| 130 |
+
# Append advertisement as a separate chunk, if applicable
|
| 131 |
if ADVERTISEMENT_TEXT and not advertisement_added:
|
| 132 |
+
yield f"data: {json.dumps(create_chat_completion_data(ADVERTISEMENT_TEXT, request.model, timestamp))}\n\n"
|
| 133 |
advertisement_added = True
|
| 134 |
|
| 135 |
+
# Yield final chunk indicating the end of the stream
|
| 136 |
+
yield f"data: {json.dumps(create_chat_completion_data('', request.model, timestamp, 'stop'))}\n\n"
|
| 137 |
yield "data: [DONE]\n\n"
|
| 138 |
|
| 139 |
except httpx.HTTPStatusError as e:
|