Niansuh commited on
Commit
cb310c2
·
verified ·
1 Parent(s): 0eb0c17

Update api/utils.py

Browse files
Files changed (1) hide show
  1. api/utils.py +4 -1
api/utils.py CHANGED
@@ -142,6 +142,8 @@ async def process_streaming_response(request: ChatRequest):
142
  }
143
 
144
  response_content = "" # Variable to hold the full response content
 
 
145
  async with httpx.AsyncClient() as client:
146
  try:
147
  async with client.stream(
@@ -178,8 +180,9 @@ async def process_streaming_response(request: ChatRequest):
178
  yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
179
 
180
  # After all chunks are processed, append the advertisement text at the end if it's not already included
181
- if ADVERTISEMENT_TEXT and ADVERTISEMENT_TEXT not in response_content:
182
  response_content += "\n\n" + ADVERTISEMENT_TEXT
 
183
 
184
  # Now yield the final chunk with the advertisement text appended at the end
185
  yield f"data: {json.dumps(create_chat_completion_data(response_content, request.model, timestamp, 'stop'))}\n\n"
 
142
  }
143
 
144
  response_content = "" # Variable to hold the full response content
145
+ advertisement_added = False # Track if advertisement is added
146
+
147
  async with httpx.AsyncClient() as client:
148
  try:
149
  async with client.stream(
 
180
  yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
181
 
182
  # After all chunks are processed, append the advertisement text at the end if it's not already included
183
+ if ADVERTISEMENT_TEXT and not advertisement_added:
184
  response_content += "\n\n" + ADVERTISEMENT_TEXT
185
+ advertisement_added = True
186
 
187
  # Now yield the final chunk with the advertisement text appended at the end
188
  yield f"data: {json.dumps(create_chat_completion_data(response_content, request.model, timestamp, 'stop'))}\n\n"