superxu520 commited on
Commit
b93f165
·
1 Parent(s): fe067bc

修复流式输出错乱问题

Browse files
Files changed (1) hide show
  1. app/server/chat.py +29 -10
app/server/chat.py CHANGED
@@ -952,10 +952,13 @@ def _create_real_streaming_response(
952
  yield f"data: {orjson.dumps(data).decode('utf-8')}\n\n"
953
  has_started = True
954
 
955
- if t_delta := chunk.thoughts_delta:
 
 
 
956
  if not last_chunk_was_thought and not full_thoughts:
957
  yield f"data: {orjson.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'created': created_time, 'model': model_name, 'choices': [{'index': 0, 'delta': {'content': '<think>'}, 'finish_reason': None}]}).decode('utf-8')}\n\n"
958
- full_thoughts += t_delta
959
  data = {
960
  "id": completion_id,
961
  "object": "chat.completion.chunk",
@@ -966,14 +969,19 @@ def _create_real_streaming_response(
966
  ],
967
  }
968
  yield f"data: {orjson.dumps(data).decode('utf-8')}\n\n"
 
969
  last_chunk_was_thought = True
970
 
971
- if text_delta := chunk.text_delta:
 
 
 
972
  if last_chunk_was_thought:
973
  yield f"data: {orjson.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'created': created_time, 'model': model_name, 'choices': [{'index': 0, 'delta': {'content': '</think>\n'}, 'finish_reason': None}]}).decode('utf-8')}\n\n"
974
  last_chunk_was_thought = False
975
- full_text += text_delta
976
- if visible_delta := suppressor.process(text_delta):
 
977
  data = {
978
  "id": completion_id,
979
  "object": "chat.completion.chunk",
@@ -988,6 +996,7 @@ def _create_real_streaming_response(
988
  ],
989
  }
990
  yield f"data: {orjson.dumps(data).decode('utf-8')}\n\n"
 
991
  except Exception as e:
992
  logger.exception(f"Error during OpenAI streaming: {e}")
993
  yield f"data: {orjson.dumps({'error': {'message': 'Streaming error occurred.', 'type': 'server_error', 'param': None, 'code': None}}).decode('utf-8')}\n\n"
@@ -1147,19 +1156,29 @@ def _create_responses_real_streaming_response(
1147
  try:
1148
  async for chunk in generator:
1149
  all_outputs.append(chunk)
1150
- if t_delta := chunk.thoughts_delta:
 
 
 
 
1151
  if not last_chunk_was_thought and not full_thoughts:
1152
  yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': '<think>'}).decode('utf-8')}\n\n"
1153
- full_thoughts += t_delta
1154
  yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': t_delta}).decode('utf-8')}\n\n"
 
1155
  last_chunk_was_thought = True
1156
- if text_delta := chunk.text_delta:
 
 
 
 
1157
  if last_chunk_was_thought:
1158
  yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': '</think>\n'}).decode('utf-8')}\n\n"
1159
  last_chunk_was_thought = False
1160
- full_text += text_delta
1161
- if visible_delta := suppressor.process(text_delta):
 
1162
  yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': visible_delta}).decode('utf-8')}\n\n"
 
1163
  except Exception as e:
1164
  logger.exception(f"Error during Responses API streaming: {e}")
1165
  yield f"data: {orjson.dumps({**base_event, 'type': 'error', 'error': {'message': 'Streaming error.'}}).decode('utf-8')}\n\n"
 
952
  yield f"data: {orjson.dumps(data).decode('utf-8')}\n\n"
953
  has_started = True
954
 
955
+ # Robust thought delta calculation
956
+ new_thoughts = chunk.thoughts or ""
957
+ if len(new_thoughts) > len(full_thoughts):
958
+ t_delta = new_thoughts[len(full_thoughts) :]
959
  if not last_chunk_was_thought and not full_thoughts:
960
  yield f"data: {orjson.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'created': created_time, 'model': model_name, 'choices': [{'index': 0, 'delta': {'content': '<think>'}, 'finish_reason': None}]}).decode('utf-8')}\n\n"
961
+
962
  data = {
963
  "id": completion_id,
964
  "object": "chat.completion.chunk",
 
969
  ],
970
  }
971
  yield f"data: {orjson.dumps(data).decode('utf-8')}\n\n"
972
+ full_thoughts = new_thoughts
973
  last_chunk_was_thought = True
974
 
975
+ # Robust text delta calculation
976
+ new_text = chunk.text or ""
977
+ if len(new_text) > len(full_text):
978
+ text_delta = new_text[len(full_text) :]
979
  if last_chunk_was_thought:
980
  yield f"data: {orjson.dumps({'id': completion_id, 'object': 'chat.completion.chunk', 'created': created_time, 'model': model_name, 'choices': [{'index': 0, 'delta': {'content': '</think>\n'}, 'finish_reason': None}]}).decode('utf-8')}\n\n"
981
  last_chunk_was_thought = False
982
+
983
+ visible_delta = suppressor.process(text_delta)
984
+ if visible_delta:
985
  data = {
986
  "id": completion_id,
987
  "object": "chat.completion.chunk",
 
996
  ],
997
  }
998
  yield f"data: {orjson.dumps(data).decode('utf-8')}\n\n"
999
+ full_text = new_text
1000
  except Exception as e:
1001
  logger.exception(f"Error during OpenAI streaming: {e}")
1002
  yield f"data: {orjson.dumps({'error': {'message': 'Streaming error occurred.', 'type': 'server_error', 'param': None, 'code': None}}).decode('utf-8')}\n\n"
 
1156
  try:
1157
  async for chunk in generator:
1158
  all_outputs.append(chunk)
1159
+
1160
+ # Robust thought delta calculation
1161
+ new_thoughts = chunk.thoughts or ""
1162
+ if len(new_thoughts) > len(full_thoughts):
1163
+ t_delta = new_thoughts[len(full_thoughts) :]
1164
  if not last_chunk_was_thought and not full_thoughts:
1165
  yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': '<think>'}).decode('utf-8')}\n\n"
 
1166
  yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': t_delta}).decode('utf-8')}\n\n"
1167
+ full_thoughts = new_thoughts
1168
  last_chunk_was_thought = True
1169
+
1170
+ # Robust text delta calculation
1171
+ new_text = chunk.text or ""
1172
+ if len(new_text) > len(full_text):
1173
+ text_delta = new_text[len(full_text) :]
1174
  if last_chunk_was_thought:
1175
  yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': '</think>\n'}).decode('utf-8')}\n\n"
1176
  last_chunk_was_thought = False
1177
+
1178
+ visible_delta = suppressor.process(text_delta)
1179
+ if visible_delta:
1180
  yield f"data: {orjson.dumps({**base_event, 'type': 'response.output_text.delta', 'output_index': 0, 'delta': visible_delta}).decode('utf-8')}\n\n"
1181
+ full_text = new_text
1182
  except Exception as e:
1183
  logger.exception(f"Error during Responses API streaming: {e}")
1184
  yield f"data: {orjson.dumps({**base_event, 'type': 'error', 'error': {'message': 'Streaming error.'}}).decode('utf-8')}\n\n"