naman1102 commited on
Commit
801749c
Β·
1 Parent(s): 5e51a6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -7
app.py CHANGED
@@ -221,13 +221,21 @@ compiled_graph = graph.compile()
221
 
222
  # ─────────────────────────── Public API ────────────────────────────────
223
 
224
- def answer(question: str, task_id: Optional[str] = None) -> str:
225
- state = AgentState(user_question=question, task_id=task_id)
226
- state.add(SystemMessage(content="You are a helpful assistant."))
227
- state.add(HumanMessage(content=question))
228
- ans = compiled_graph.invoke(state)
229
- print(f"Final answer: {ans.final_answer}")
230
- return ans.final_answer or "No answer."
 
 
 
 
 
 
 
 
231
 
232
 
233
 
 
221
 
222
  # ─────────────────────────── Public API ────────────────────────────────
223
 
224
+ def answer(question: str, *, task_id: Optional[str] = None) -> str:
225
+ """Run the agent and return whatever FINAL_ANSWER the graph produces."""
226
+ init_state = AgentState(question, task_id)
227
+ init_state.add(SystemMessage(content="You are a helpful assistant."))
228
+ init_state.add(HumanMessage(content=question))
229
+
230
+ # IMPORTANT: invoke() returns a **new** state instance (or an AddableValuesDict),
231
+ # not the object we pass in. Use the returned value to fetch final_answer.
232
+ out_state = compiled_graph.invoke(init_state)
233
+
234
+ if isinstance(out_state, dict): # AddableValuesDict behaves like a dict
235
+ return out_state.get("final_answer", "No answer.")
236
+ else: # If future versions return the dataclass
237
+ return getattr(out_state, "final_answer", "No answer.")
238
+
239
 
240
 
241