Spaces:
Running
Running
Zenith Wang
commited on
Commit
·
6a10e69
1
Parent(s):
2c0e18d
Fix CoT and Answer content separation logic
Browse files
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,
|
| 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 |
-
|
|
|
|
| 214 |
delta = chunk.choices[0].delta
|
| 215 |
|
| 216 |
-
#
|
| 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 |
-
|
| 233 |
-
|
|
|
|
| 234 |
# 如果通过 reasoning 字段获取了 CoT,content 就是最终答案
|
| 235 |
if has_reasoning_field:
|
| 236 |
-
final_content +=
|
| 237 |
-
full_response
|
| 238 |
else:
|
| 239 |
# 否则尝试解析 <reasoning> 标签
|
| 240 |
-
full_response +=
|
| 241 |
|
| 242 |
-
# 检测reasoning标签
|
| 243 |
-
if '<reasoning>' in
|
| 244 |
in_reasoning = True
|
| 245 |
-
|
|
|
|
|
|
|
| 246 |
if len(parts) > 1:
|
| 247 |
-
reasoning_content
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
|
|
|
| 250 |
in_reasoning = False
|
| 251 |
-
parts = full_response.split('</reasoning>')
|
| 252 |
if len(parts) > 1:
|
| 253 |
-
|
| 254 |
-
final_content = parts[1]
|
| 255 |
elif in_reasoning:
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
parts = full_response.split('</reasoning>')
|
| 259 |
-
if len(parts) > 1:
|
| 260 |
-
final_content = parts[1]
|
| 261 |
else:
|
| 262 |
-
#
|
| 263 |
-
if
|
| 264 |
final_content = full_response
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
# 最终格式化
|
| 278 |
-
if reasoning_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 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
except Exception as e:
|
| 288 |
-
|
| 289 |
-
|
| 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
|