Niansuh commited on
Commit
06ff1c4
·
verified ·
1 Parent(s): f8c03ff

Update api/utils.py

Browse files
Files changed (1) hide show
  1. api/utils.py +6 -5
api/utils.py CHANGED
@@ -99,8 +99,8 @@ async def process_streaming_response(request: ChatRequest):
99
  "imageGenerationMode": False,
100
  }
101
 
102
- response_content = ""
103
- advertisement_added = False
104
 
105
  async with httpx.AsyncClient() as client:
106
  try:
@@ -126,10 +126,10 @@ async def process_streaming_response(request: ChatRequest):
126
  # Add chunk to the response content
127
  response_content += cleaned_content
128
 
129
- # Only yield cleaned content in chunks
130
  yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
131
 
132
- # Append advertisement at the end, ensuring it happens only once
133
  if ADVERTISEMENT_TEXT and not advertisement_added:
134
  response_content += "\n\n" + ADVERTISEMENT_TEXT
135
  advertisement_added = True
@@ -191,7 +191,8 @@ async def process_non_streaming_response(request: ChatRequest):
191
  "imageGenerationMode": False,
192
  }
193
 
194
- full_response = ""
 
195
  async with httpx.AsyncClient() as client:
196
  try:
197
  async with client.stream("POST", f"{BASE_URL}/api/chat", headers=headers_api_chat, json=json_data) as response:
 
99
  "imageGenerationMode": False,
100
  }
101
 
102
+ response_content = "" # Store accumulated response content
103
+ advertisement_added = False # Track if advertisement is already added
104
 
105
  async with httpx.AsyncClient() as client:
106
  try:
 
126
  # Add chunk to the response content
127
  response_content += cleaned_content
128
 
129
+ # Yield cleaned chunk as part of the stream (do not repeat this later)
130
  yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
131
 
132
+ # Append advertisement at the very end of the response, once
133
  if ADVERTISEMENT_TEXT and not advertisement_added:
134
  response_content += "\n\n" + ADVERTISEMENT_TEXT
135
  advertisement_added = True
 
191
  "imageGenerationMode": False,
192
  }
193
 
194
+ full_response = "" # Store full non-streaming response content
195
+
196
  async with httpx.AsyncClient() as client:
197
  try:
198
  async with client.stream("POST", f"{BASE_URL}/api/chat", headers=headers_api_chat, json=json_data) as response: