Update api/utils.py
Browse files- api/utils.py +26 -30
api/utils.py
CHANGED
@@ -141,7 +141,6 @@ async def process_streaming_response(request: ChatRequest):
|
|
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(
|
@@ -152,48 +151,45 @@ async def process_streaming_response(request: ChatRequest):
|
|
152 |
timeout=100,
|
153 |
) as response:
|
154 |
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
155 |
async for chunk in response.aiter_text():
|
156 |
-
timestamp = int(datetime.now().timestamp())
|
157 |
if chunk:
|
158 |
content = chunk
|
159 |
if content.startswith("$@$v=undefined-rv1$@$"):
|
160 |
-
content = content[21:]
|
161 |
-
|
|
|
162 |
if BLOCKED_MESSAGE in content:
|
163 |
-
logger.info(
|
164 |
-
f"Blocked message detected in response for Request ID {request_id}."
|
165 |
-
)
|
166 |
content = content.replace(BLOCKED_MESSAGE, '').strip()
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
169 |
cleaned_content = strip_model_prefix(content, model_prefix)
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
|
|
|
|
173 |
if ADVERTISEMENT_TEXT:
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
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))
|
193 |
except httpx.RequestError as e:
|
194 |
-
logger.error(
|
195 |
-
f"Error occurred during request for Request ID {request_id}: {e}"
|
196 |
-
)
|
197 |
raise HTTPException(status_code=500, detail=str(e))
|
198 |
|
199 |
# Process non-streaming response with headers from config.py
|
|
|
141 |
"imageGenerationMode": False, # Added this line
|
142 |
}
|
143 |
|
|
|
144 |
async with httpx.AsyncClient() as client:
|
145 |
try:
|
146 |
async with client.stream(
|
|
|
151 |
timeout=100,
|
152 |
) as response:
|
153 |
response.raise_for_status()
|
154 |
+
|
155 |
+
# Start processing the chunks and yield them one by one
|
156 |
+
timestamp = int(datetime.now().timestamp())
|
157 |
+
response_content = "" # Collect response content
|
158 |
+
|
159 |
async for chunk in response.aiter_text():
|
|
|
160 |
if chunk:
|
161 |
content = chunk
|
162 |
if content.startswith("$@$v=undefined-rv1$@$"):
|
163 |
+
content = content[21:] # Remove unwanted prefix
|
164 |
+
|
165 |
+
# Remove blocked message if present
|
166 |
if BLOCKED_MESSAGE in content:
|
167 |
+
logger.info(f"Blocked message detected in response for Request ID {request_id}.")
|
|
|
|
|
168 |
content = content.replace(BLOCKED_MESSAGE, '').strip()
|
169 |
+
|
170 |
+
if not content:
|
171 |
+
continue # Skip if content is empty after removal
|
172 |
+
|
173 |
+
# Clean up the content
|
174 |
cleaned_content = strip_model_prefix(content, model_prefix)
|
175 |
+
|
176 |
+
# Yield each chunk as soon as it's ready
|
177 |
+
yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
|
178 |
+
response_content += cleaned_content # Collect the full response content
|
179 |
+
|
180 |
+
# After all chunks are processed, add the advertisement text at the end
|
181 |
if ADVERTISEMENT_TEXT:
|
182 |
+
response_content += "\n\n" + ADVERTISEMENT_TEXT
|
183 |
+
yield f"data: {json.dumps(create_chat_completion_data(response_content, request.model, timestamp, 'stop'))}\n\n"
|
184 |
+
|
185 |
+
# Add the final "done" marker
|
186 |
+
yield "data: [DONE]\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
|
188 |
except httpx.HTTPStatusError as e:
|
189 |
logger.error(f"HTTP error occurred for Request ID {request_id}: {e}")
|
190 |
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
191 |
except httpx.RequestError as e:
|
192 |
+
logger.error(f"Error occurred during request for Request ID {request_id}: {e}")
|
|
|
|
|
193 |
raise HTTPException(status_code=500, detail=str(e))
|
194 |
|
195 |
# Process non-streaming response with headers from config.py
|