Zenith Wang commited on
Commit
6a10e69
·
1 Parent(s): 2c0e18d

Fix CoT and Answer content separation logic

Browse files
Files changed (1) hide show
  1. app.py +68 -53
app.py CHANGED
@@ -200,93 +200,108 @@ def process_message(message, history, images, system_prompt, temperature, max_to
200
  stream=True
201
  )
202
 
203
- print("[DEBUG] API call successful, starting streaming")
204
 
205
  # 流式输出
206
  full_response = ""
 
207
  in_reasoning = False
208
  reasoning_content = ""
209
  final_content = ""
210
- has_reasoning_field = False
211
 
212
  for chunk in response:
213
- if chunk.choices and chunk.choices[0]:
 
214
  delta = chunk.choices[0].delta
215
 
216
- # 优先检查 delta.reasoning 字段(Step-3 API 的 CoT 内容)
217
  if hasattr(delta, 'reasoning') and delta.reasoning:
218
  has_reasoning_field = True
219
  reasoning_content += delta.reasoning
220
  print(f"[DEBUG] CoT chunk: {delta.reasoning[:50] if len(delta.reasoning) > 50 else delta.reasoning}")
221
-
222
- # 实时更新显示 CoT 内容
223
- if final_content:
224
- display_text = f"💭 **Chain of Thought:**\n\n{reasoning_content}\n\n---\n\n📝 **Answer:**\n\n{final_content}"
225
- else:
226
- display_text = f"💭 **Chain of Thought:**\n\n{reasoning_content}\n\n---\n\n📝 **Answer:**\n\n*Generating...*"
227
-
228
- history[-1][1] = display_text
229
- yield history
230
 
231
  # 处理常规 content 字段
232
- delta_content = delta.content if hasattr(delta, 'content') else None
233
- if delta_content:
 
234
  # 如果通过 reasoning 字段获取了 CoT,content 就是最终答案
235
  if has_reasoning_field:
236
- final_content += delta_content
237
- full_response += delta_content
238
  else:
239
  # 否则尝试解析 <reasoning> 标签
240
- full_response += delta_content
241
 
242
- # 检测reasoning标签
243
- if '<reasoning>' in full_response and not in_reasoning:
244
  in_reasoning = True
245
- parts = full_response.split('<reasoning>')
 
 
246
  if len(parts) > 1:
247
- reasoning_content = parts[1]
248
-
249
- if in_reasoning and '</reasoning>' in full_response:
 
 
 
250
  in_reasoning = False
251
- parts = full_response.split('</reasoning>')
252
  if len(parts) > 1:
253
- reasoning_content = parts[0].split('<reasoning>')[-1]
254
- final_content = parts[1]
255
  elif in_reasoning:
256
- reasoning_content = full_response.split('<reasoning>')[-1]
257
- elif '</reasoning>' in full_response:
258
- parts = full_response.split('</reasoning>')
259
- if len(parts) > 1:
260
- final_content = parts[1]
261
  else:
262
- # 没有reasoning标签的情况
263
- if '<reasoning>' not in full_response:
264
  final_content = full_response
265
-
266
- # 格式化显示
267
- if reasoning_content and final_content:
268
- display_text = f"💭 **Chain of Thought:**\n\n{reasoning_content.strip()}\n\n---\n\n📝 **Answer:**\n\n{final_content.strip()}"
269
- elif reasoning_content:
270
- display_text = f"💭 **Chain of Thought:**\n\n{reasoning_content.strip()}\n\n---\n\n📝 **Answer:**\n\n*Generating...*"
271
- else:
272
- display_text = full_response
273
-
274
- history[-1][1] = display_text
275
- yield history
 
 
 
 
 
 
 
 
 
 
 
 
276
 
277
  # 最终格式化
278
- if reasoning_content or final_content:
 
279
  final_display = f"💭 **Chain of Thought:**\n\n{reasoning_content.strip()}\n\n---\n\n📝 **Answer:**\n\n{final_content.strip()}"
280
  history[-1][1] = final_display
 
 
 
 
 
 
 
 
 
281
  else:
282
- history[-1][1] = full_response
283
-
284
- print(f"[DEBUG] Streaming completed. Response length: {len(full_response)}")
285
- yield history
286
-
287
  except Exception as e:
288
- error_msg = f" Error: {str(e)}"
289
- print(f"[ERROR] {error_msg}")
290
  traceback.print_exc()
291
  history[-1][1] = f"❌ Error: {str(e)}"
292
  yield history
 
200
  stream=True
201
  )
202
 
203
+ print("[DEBUG] API call successful, processing stream...")
204
 
205
  # 流式输出
206
  full_response = ""
207
+ chunk_count = 0
208
  in_reasoning = False
209
  reasoning_content = ""
210
  final_content = ""
211
+ has_reasoning_field = False # 标记是否通过 delta.reasoning 字段获取 CoT
212
 
213
  for chunk in response:
214
+ chunk_count += 1
215
+ if chunk.choices and len(chunk.choices) > 0:
216
  delta = chunk.choices[0].delta
217
 
218
+ # 检查 delta.reasoning 字段(Step-3 API 的 CoT 内容)
219
  if hasattr(delta, 'reasoning') and delta.reasoning:
220
  has_reasoning_field = True
221
  reasoning_content += delta.reasoning
222
  print(f"[DEBUG] CoT chunk: {delta.reasoning[:50] if len(delta.reasoning) > 50 else delta.reasoning}")
 
 
 
 
 
 
 
 
 
223
 
224
  # 处理常规 content 字段
225
+ if hasattr(delta, 'content') and delta.content:
226
+ content = delta.content
227
+
228
  # 如果通过 reasoning 字段获取了 CoT,content 就是最终答案
229
  if has_reasoning_field:
230
+ final_content += content
231
+ full_response = reasoning_content + final_content # 完整响应包含两部分
232
  else:
233
  # 否则尝试解析 <reasoning> 标签
234
+ full_response += content
235
 
236
+ # 检测 <reasoning> 标签
237
+ if '<reasoning>' in content and not in_reasoning:
238
  in_reasoning = True
239
+ # 分割内容
240
+ parts = content.split('<reasoning>')
241
+ final_content += parts[0]
242
  if len(parts) > 1:
243
+ reasoning_content += parts[1]
244
+ elif '</reasoning>' in content and in_reasoning:
245
+ # 结束推理部分
246
+ parts = content.split('</reasoning>')
247
+ if parts[0]:
248
+ reasoning_content += parts[0]
249
  in_reasoning = False
 
250
  if len(parts) > 1:
251
+ final_content += parts[1]
 
252
  elif in_reasoning:
253
+ # 在推理标签内
254
+ reasoning_content += content
 
 
 
255
  else:
256
+ # 在推理标签外或没有标签
257
+ if not reasoning_content: # 如果还没有推理内容,说明没有reasoning标签
258
  final_content = full_response
259
+ else:
260
+ final_content += content
261
+
262
+ # 实时更新显示
263
+ if reasoning_content and final_content:
264
+ # 有推理和最终答案
265
+ display_text = f"💭 **Chain of Thought:**\n\n{reasoning_content.strip()}\n\n---\n\n📝 **Answer:**\n\n{final_content.strip()}"
266
+ elif reasoning_content:
267
+ # 只有推理过程
268
+ display_text = f"💭 **Chain of Thought:**\n\n{reasoning_content.strip()}\n\n---\n\n📝 **Answer:**\n\n*Generating...*"
269
+ else:
270
+ # 只有答案或普通回复
271
+ display_text = final_content if final_content else full_response
272
+
273
+ history[-1][1] = display_text
274
+
275
+ if chunk_count % 5 == 0:
276
+ print(f"[DEBUG] Received {chunk_count} chunks")
277
+ if has_reasoning_field:
278
+ print(f"[DEBUG] CoT length: {len(reasoning_content)}, Answer length: {len(final_content)}")
279
+ yield history
280
+
281
+ print(f"[DEBUG] Stream complete. Total chunks: {chunk_count}, Total chars: {len(full_response)}")
282
 
283
  # 最终格式化
284
+ if reasoning_content:
285
+ # 如果有推理内容,使用格式化显示
286
  final_display = f"💭 **Chain of Thought:**\n\n{reasoning_content.strip()}\n\n---\n\n📝 **Answer:**\n\n{final_content.strip()}"
287
  history[-1][1] = final_display
288
+ yield history
289
+ elif final_content:
290
+ # 只有最终答案
291
+ history[-1][1] = final_content.strip()
292
+ yield history
293
+ elif full_response:
294
+ # 使用完整响应
295
+ history[-1][1] = full_response.strip()
296
+ yield history
297
  else:
298
+ print("[DEBUG] No response content received")
299
+ history[-1][1] = "⚠️ No response received from API"
300
+ yield history
301
+
 
302
  except Exception as e:
303
+ print(f"[DEBUG] API error: {e}")
304
+ import traceback
305
  traceback.print_exc()
306
  history[-1][1] = f"❌ Error: {str(e)}"
307
  yield history