Niansuh commited on
Commit
5b8e849
·
verified ·
1 Parent(s): d6b078b

Update api/utils.py

Browse files
Files changed (1) hide show
  1. api/utils.py +2 -3
api/utils.py CHANGED
@@ -148,12 +148,12 @@ async def process_streaming_response(request: ChatRequest):
148
  # Yield the cleaned chunk as part of the stream
149
  yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
150
 
151
- # Ensure that the advertisement is added after the full response is processed
152
  if ADVERTISEMENT_TEXT and not advertisement_added:
153
  response_content += "\n\n" + ADVERTISEMENT_TEXT # Add line break here
154
  advertisement_added = True
155
 
156
- # Yield the final chunk with the advertisement text appended at the end
157
  yield f"data: {json.dumps(create_chat_completion_data(response_content, request.model, timestamp, 'stop'))}\n\n"
158
 
159
  # Add the final "done" marker
@@ -166,7 +166,6 @@ async def process_streaming_response(request: ChatRequest):
166
  logger.error(f"Error occurred during request for Request ID {request_id}: {e}")
167
  raise HTTPException(status_code=500, detail=str(e))
168
 
169
-
170
  # Process non-streaming response with headers from config.py
171
  async def process_non_streaming_response(request: ChatRequest):
172
  request_id = f"chatcmpl-{uuid.uuid4()}"
 
148
  # Yield the cleaned chunk as part of the stream
149
  yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
150
 
151
+ # After processing all chunks, add the advertisement once (at the end)
152
  if ADVERTISEMENT_TEXT and not advertisement_added:
153
  response_content += "\n\n" + ADVERTISEMENT_TEXT # Add line break here
154
  advertisement_added = True
155
 
156
+ # Yield the final chunk with the advertisement text appended at the end (this is the final response)
157
  yield f"data: {json.dumps(create_chat_completion_data(response_content, request.model, timestamp, 'stop'))}\n\n"
158
 
159
  # Add the final "done" marker
 
166
  logger.error(f"Error occurred during request for Request ID {request_id}: {e}")
167
  raise HTTPException(status_code=500, detail=str(e))
168
 
 
169
  # Process non-streaming response with headers from config.py
170
  async def process_non_streaming_response(request: ChatRequest):
171
  request_id = f"chatcmpl-{uuid.uuid4()}"