Niansuh commited on
Commit
7fb95d2
·
verified ·
1 Parent(s): e710865

Update api/utils.py

Browse files
Files changed (1) hide show
  1. api/utils.py +28 -25
api/utils.py CHANGED
@@ -120,9 +120,8 @@ async def process_streaming_response(request: ChatRequest):
120
 
121
  json_data = build_json_data(request, h_value, model_prefix)
122
 
123
- # Initialize buffer to handle BLOCKED_MESSAGE that may be split across chunks
124
- buffer = ""
125
- buffer_size = len(BLOCKED_MESSAGE) - 1
126
 
127
  async with httpx.AsyncClient() as client:
128
  try:
@@ -136,32 +135,36 @@ async def process_streaming_response(request: ChatRequest):
136
  response.raise_for_status()
137
  async for chunk in response.aiter_text():
138
  if chunk:
139
- buffer += chunk
 
140
 
141
- # Remove any occurrence of BLOCKED_MESSAGE in buffer
142
- if BLOCKED_MESSAGE in buffer:
143
  logger.info("Blocked message detected in response.")
144
- buffer = buffer.replace(BLOCKED_MESSAGE, '')
145
-
146
- # Process the buffer
147
- # Keep last buffer_size characters in buffer to handle partial BLOCKED_MESSAGE
148
- while len(buffer) >= buffer_size:
149
- content_to_yield = buffer[:-buffer_size+1]
150
- buffer = buffer[-buffer_size+1:]
151
-
152
- # Remove model prefix if present
153
- cleaned_content = strip_model_prefix(content_to_yield, model_prefix)
154
- timestamp = int(datetime.now().timestamp())
155
- yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
156
-
157
- # Process any remaining content in buffer
158
- if buffer:
159
- # Remove any occurrence of BLOCKED_MESSAGE in buffer
160
- if BLOCKED_MESSAGE in buffer:
 
 
 
161
  logger.info("Blocked message detected in remaining buffer.")
162
- buffer = buffer.replace(BLOCKED_MESSAGE, '')
163
 
164
- cleaned_content = strip_model_prefix(buffer, model_prefix)
165
  timestamp = int(datetime.now().timestamp())
166
  yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
167
 
 
120
 
121
  json_data = build_json_data(request, h_value, model_prefix)
122
 
123
+ # Initialize rolling buffer to handle BLOCKED_MESSAGE split across chunks
124
+ rolling_buffer = ""
 
125
 
126
  async with httpx.AsyncClient() as client:
127
  try:
 
135
  response.raise_for_status()
136
  async for chunk in response.aiter_text():
137
  if chunk:
138
+ # Combine rolling buffer with current chunk
139
+ combined_chunk = rolling_buffer + chunk
140
 
141
+ # Remove any occurrence of BLOCKED_MESSAGE in combined_chunk
142
+ if BLOCKED_MESSAGE in combined_chunk:
143
  logger.info("Blocked message detected in response.")
144
+ combined_chunk = combined_chunk.replace(BLOCKED_MESSAGE, '')
145
+
146
+ # Remove model prefix if present
147
+ cleaned_content = strip_model_prefix(combined_chunk, model_prefix)
148
+
149
+ # Yield the cleaned content
150
+ timestamp = int(datetime.now().timestamp())
151
+ yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
152
+
153
+ # Update rolling buffer with the end of the combined_chunk
154
+ # Keep only the last len(BLOCKED_MESSAGE) - 1 characters
155
+ rolling_buffer = combined_chunk[-(len(BLOCKED_MESSAGE) - 1):]
156
+ else:
157
+ # If chunk is empty, reset rolling buffer
158
+ rolling_buffer = ""
159
+
160
+ # After streaming is done, check if any remaining content is in the rolling buffer
161
+ if rolling_buffer:
162
+ # Remove any occurrence of BLOCKED_MESSAGE in rolling buffer
163
+ if BLOCKED_MESSAGE in rolling_buffer:
164
  logger.info("Blocked message detected in remaining buffer.")
165
+ rolling_buffer = rolling_buffer.replace(BLOCKED_MESSAGE, '')
166
 
167
+ cleaned_content = strip_model_prefix(rolling_buffer, model_prefix)
168
  timestamp = int(datetime.now().timestamp())
169
  yield f"data: {json.dumps(create_chat_completion_data(cleaned_content, request.model, timestamp))}\n\n"
170