Zenith Wang commited on
Commit
762ea56
·
1 Parent(s): 59cc222

Add real-time CoT (Chain of Thought) streaming display

Browse files
Files changed (1) hide show
  1. app.py +51 -3
app.py CHANGED
@@ -145,19 +145,67 @@ def process_message(message, history, system_prompt, temperature, max_tokens, to
145
  # 流式输出
146
  full_response = ""
147
  chunk_count = 0
 
 
 
 
148
  for chunk in response:
149
  chunk_count += 1
150
  if chunk.choices and len(chunk.choices) > 0:
151
  delta = chunk.choices[0].delta
152
  if hasattr(delta, 'content') and delta.content:
153
- full_response += delta.content
154
- history[-1][1] = full_response
155
- if chunk_count % 10 == 0:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  print(f"[DEBUG] Received {chunk_count} chunks, {len(full_response)} chars")
157
  yield history
158
 
159
  print(f"[DEBUG] Stream complete. Total chunks: {chunk_count}, Total chars: {len(full_response)}")
160
 
 
 
 
 
 
 
 
161
  if not full_response:
162
  print("[DEBUG] No response content received")
163
  history[-1][1] = "⚠️ No response received from API"
 
145
  # 流式输出
146
  full_response = ""
147
  chunk_count = 0
148
+ in_reasoning = False
149
+ reasoning_content = ""
150
+ final_content = ""
151
+
152
  for chunk in response:
153
  chunk_count += 1
154
  if chunk.choices and len(chunk.choices) > 0:
155
  delta = chunk.choices[0].delta
156
  if hasattr(delta, 'content') and delta.content:
157
+ content = delta.content
158
+ full_response += content
159
+
160
+ # 检测 <reasoning> 标签
161
+ if '<reasoning>' in content:
162
+ in_reasoning = True
163
+ # 分割内容
164
+ parts = content.split('<reasoning>')
165
+ final_content += parts[0]
166
+ if len(parts) > 1:
167
+ reasoning_content += parts[1]
168
+ elif '</reasoning>' in content:
169
+ # 结束推理部分
170
+ parts = content.split('</reasoning>')
171
+ if parts[0]:
172
+ reasoning_content += parts[0]
173
+ in_reasoning = False
174
+ if len(parts) > 1:
175
+ final_content += parts[1]
176
+ elif in_reasoning:
177
+ # 在推理标签内
178
+ reasoning_content += content
179
+ else:
180
+ # 在推理标签外
181
+ final_content += content
182
+
183
+ # 实时更新显示
184
+ if reasoning_content and final_content:
185
+ # 有推理和最终答案
186
+ display_text = f"💭 **Chain of Thought:**\n\n{reasoning_content}\n\n---\n\n📝 **Answer:**\n\n{final_content}"
187
+ elif reasoning_content:
188
+ # 只有推理过程
189
+ display_text = f"💭 **Chain of Thought:**\n\n{reasoning_content}\n\n---\n\n📝 **Answer:**\n\n*Generating...*"
190
+ else:
191
+ # 只有答案或普通回复
192
+ display_text = full_response
193
+
194
+ history[-1][1] = display_text
195
+
196
+ if chunk_count % 5 == 0:
197
  print(f"[DEBUG] Received {chunk_count} chunks, {len(full_response)} chars")
198
  yield history
199
 
200
  print(f"[DEBUG] Stream complete. Total chunks: {chunk_count}, Total chars: {len(full_response)}")
201
 
202
+ # 最终格式化
203
+ if reasoning_content:
204
+ # 如果有推理内容,使用格式化显示
205
+ final_display = f"💭 **Chain of Thought:**\n\n{reasoning_content}\n\n---\n\n📝 **Answer:**\n\n{final_content.strip()}"
206
+ history[-1][1] = final_display
207
+ yield history
208
+
209
  if not full_response:
210
  print("[DEBUG] No response content received")
211
  history[-1][1] = "⚠️ No response received from API"