JC321 commited on
Commit
6eb2af7
·
verified ·
1 Parent(s): 99a10b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -178,13 +178,22 @@ def chatbot_response(message, history):
178
  # 调用 MCP 工具
179
  tool_result = call_mcp_tool(tool_name, tool_args)
180
 
 
 
 
 
 
 
 
 
 
181
  # 记录工具调用(包含结果)
182
  tool_calls_log.append({"name": tool_name, "arguments": tool_args, "result": tool_result})
183
 
184
  messages.append({
185
  "role": "tool",
186
  "name": tool_name,
187
- "content": json.dumps(tool_result),
188
  "tool_call_id": tool_call.id
189
  })
190
 
@@ -270,13 +279,18 @@ function toggleToolDetails(id) {
270
  yield response_prefix + accumulated_text
271
 
272
  except Exception as e:
273
- yield f"❌ Error: {str(e)}"
 
 
 
 
 
274
 
275
  # ========== Gradio 界面 ==========
276
  with gr.Blocks(title="Financial & Market AI Assistant", css="""
277
  .contain { max-width: 100% !important; padding: 0 !important; }
278
  #component-0 { height: 100vh !important; }
279
- .chatbot { height: calc(100vh - 200px) !important; }
280
  """) as demo:
281
  gr.Markdown("# 🤖 Financial & Market AI Assistant")
282
 
@@ -289,16 +303,8 @@ with gr.Blocks(title="Financial & Market AI Assistant", css="""
289
  "Get the latest market news about crypto",
290
  "Compare Microsoft's latest earnings with its current stock price",
291
  ],
292
- title="💬 Chat with AI",
293
- chatbot=gr.Chatbot(height=700, show_copy_button=True),
294
  )
295
-
296
- # 底部信息栏(缩小)
297
- gr.Markdown("""
298
- <div style='padding: 8px; background: #f8f9fa; border-top: 1px solid #dee2e6; margin-top: 10px; font-size: 0.75em; color: #6c757d; text-align: center;'>
299
- ✅ <strong>AI:</strong> Qwen/Qwen2.5-72B-Instruct:novita &nbsp;|&nbsp; 📊 <strong>Data:</strong> SEC Financial Reports + Market & Stock Data
300
- </div>
301
- """)
302
 
303
  # 启动应用
304
  if __name__ == "__main__":
 
178
  # 调用 MCP 工具
179
  tool_result = call_mcp_tool(tool_name, tool_args)
180
 
181
+ # 限制返回结果大小,避免超长内容导致500错误
182
+ result_str = json.dumps(tool_result, ensure_ascii=False)
183
+ if len(result_str) > 4000:
184
+ # 截断过长的结果
185
+ tool_result_truncated = {"_truncated": True, "preview": result_str[:4000] + "..."}
186
+ result_for_llm = json.dumps(tool_result_truncated)
187
+ else:
188
+ result_for_llm = result_str
189
+
190
  # 记录工具调用(包含结果)
191
  tool_calls_log.append({"name": tool_name, "arguments": tool_args, "result": tool_result})
192
 
193
  messages.append({
194
  "role": "tool",
195
  "name": tool_name,
196
+ "content": result_for_llm,
197
  "tool_call_id": tool_call.id
198
  })
199
 
 
279
  yield response_prefix + accumulated_text
280
 
281
  except Exception as e:
282
+ import traceback
283
+ error_detail = str(e)
284
+ if "500" in error_detail:
285
+ yield f"❌ Error: 模型服务器错误。可能是数据太大或请求超时。\n\n详细信息: {error_detail[:200]}"
286
+ else:
287
+ yield f"❌ Error: {error_detail}\n\n{traceback.format_exc()[:500]}"
288
 
289
  # ========== Gradio 界面 ==========
290
  with gr.Blocks(title="Financial & Market AI Assistant", css="""
291
  .contain { max-width: 100% !important; padding: 0 !important; }
292
  #component-0 { height: 100vh !important; }
293
+ .chatbot { height: calc(100vh - 150px) !important; }
294
  """) as demo:
295
  gr.Markdown("# 🤖 Financial & Market AI Assistant")
296
 
 
303
  "Get the latest market news about crypto",
304
  "Compare Microsoft's latest earnings with its current stock price",
305
  ],
306
+ chatbot=gr.Chatbot(height=800, show_copy_button=True),
 
307
  )
 
 
 
 
 
 
 
308
 
309
  # 启动应用
310
  if __name__ == "__main__":