BiGuan commited on
Commit
6cbffae
·
verified ·
1 Parent(s): e7fb476

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -1
app.py CHANGED
@@ -377,10 +377,24 @@ def increment_tool_count(state: AgentState) -> dict:
377
  def finish_node(state: AgentState) -> dict:
378
  last = state["messages"][-1]
379
  content = last.content
380
- # 提取最终答案(纯文本)
381
  answer = content.strip().split("\n")[-1].strip()
382
  if "FINAL ANSWER:" in answer:
383
  answer = answer.split("FINAL ANSWER:")[-1].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384
  return {"final_answer": answer}
385
 
386
  def build_graph():
 
377
  def finish_node(state: AgentState) -> dict:
378
  last = state["messages"][-1]
379
  content = last.content
 
380
  answer = content.strip().split("\n")[-1].strip()
381
  if "FINAL ANSWER:" in answer:
382
  answer = answer.split("FINAL ANSWER:")[-1].strip()
383
+
384
+ # 若答案仍为空,尝试从历史消息中提取最后一条有内容的 AI 消息
385
+ if not answer:
386
+ for m in reversed(state["messages"]):
387
+ if isinstance(m, AIMessage) and m.content.strip():
388
+ answer = m.content.strip().split("\n")[-1].strip()
389
+ break
390
+
391
+ # 依然无答案时,输出原因
392
+ if not answer:
393
+ if state.get("tool_attempts", 0) >= 5:
394
+ answer = "Unable to determine answer: reached maximum tool calls without conclusion."
395
+ else:
396
+ answer = "Unable to determine answer: insufficient information."
397
+
398
  return {"final_answer": answer}
399
 
400
  def build_graph():