Spaces:
Running
Running
Zenith Wang
commited on
Commit
·
b37d787
1
Parent(s):
765ca1b
Add support for delta.reasoning field to properly display CoT streaming
Browse files
app.py
CHANGED
|
@@ -207,36 +207,61 @@ def process_message(message, history, images, system_prompt, temperature, max_to
|
|
| 207 |
in_reasoning = False
|
| 208 |
reasoning_content = ""
|
| 209 |
final_content = ""
|
|
|
|
| 210 |
|
| 211 |
for chunk in response:
|
| 212 |
-
if chunk.choices and chunk.choices[0]
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
-
#
|
| 218 |
-
if
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
reasoning_content = parts[1]
|
| 223 |
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
if len(parts) > 1:
|
| 235 |
-
final_content = parts[1]
|
| 236 |
else:
|
| 237 |
-
#
|
| 238 |
-
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
# 格式化显示
|
| 242 |
if reasoning_content and final_content:
|
|
|
|
| 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:
|