naman1102 commited on
Commit
ccf10a2
Β·
1 Parent(s): 477bf79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -21
app.py CHANGED
@@ -32,7 +32,7 @@ def plan_node(state: AgentState) -> AgentState:
32
  If confident: return {"final_answer": "<answer>"}
33
  Otherwise: return exactly one of
34
  {"wiki_query": "..."},
35
-
36
  {"ocr_path": "..."},
37
  {"excel_path": "...", "excel_sheet_name": "..."},
38
  {"audio_path": "..."}
@@ -54,7 +54,7 @@ def plan_node(state: AgentState) -> AgentState:
54
  " and nothing else.\n"
55
  " β€’ Otherwise, return exactly one of:\n"
56
  " {\"wiki_query\":\"<Wikipedia search>\"}\n"
57
- # " {\"web_search_query\":\"<search terms>\"}\n"
58
  " {\"ocr_path\":\"<image path or task_id>\"}\n"
59
  " {\"excel_path\":\"<xlsx path>\", \"excel_sheet_name\":\"<sheet name>\"}\n"
60
  " {\"audio_path\":\"<audio path or task_id>\"}\n"
@@ -185,7 +185,7 @@ def inspect_node(state: AgentState) -> AgentState:
185
  "and nothing else.\n"
186
  "Otherwise, return exactly one of these JSON literals to fetch another tool:\n"
187
  " {\"wiki_query\":\"<query for Wikipedia>\"}\n"
188
- # " {\"web_search_query\":\"<search terms>\"}\n"
189
  " {\"ocr_path\":\"<image path or task_id>\"}\n"
190
  " {\"excel_path\":\"<xls path>\", \"excel_sheet_name\":\"<sheet name>\"}\n"
191
  " {\"audio_path\":\"<audio path or task_id>\"}\n"
@@ -196,30 +196,32 @@ def inspect_node(state: AgentState) -> AgentState:
196
  raw = llm_response.content.strip()
197
 
198
  new_msgs = state["messages"] + [AIMessage(content=raw)]
 
 
199
  try:
200
  parsed = json.loads(raw)
201
  if isinstance(parsed, dict):
202
- partial: AgentState = {"messages": new_msgs}
203
- allowed = {
204
- "final_answer",
205
- "wiki_query",
206
- "web_search_query",
207
- "ocr_path",
208
- "excel_path",
209
- "excel_sheet_name",
210
- "audio_path"
211
- }
212
- for k, v in parsed.items():
213
- if k in allowed:
214
- partial[k] = v
215
- return partial
216
  except json.JSONDecodeError:
217
  pass
218
 
219
- return {
220
- "messages": new_msgs,
221
- "final_answer": "ERROR: could not parse inspect decision."
222
- }
 
 
 
223
 
224
 
225
  # ─── 6) finalize_node ───
 
32
  If confident: return {"final_answer": "<answer>"}
33
  Otherwise: return exactly one of
34
  {"wiki_query": "..."},
35
+ {"web_search_query": "..."},
36
  {"ocr_path": "..."},
37
  {"excel_path": "...", "excel_sheet_name": "..."},
38
  {"audio_path": "..."}
 
54
  " and nothing else.\n"
55
  " β€’ Otherwise, return exactly one of:\n"
56
  " {\"wiki_query\":\"<Wikipedia search>\"}\n"
57
+ " {\"web_search_query\":\"<search terms>\"}\n"
58
  " {\"ocr_path\":\"<image path or task_id>\"}\n"
59
  " {\"excel_path\":\"<xlsx path>\", \"excel_sheet_name\":\"<sheet name>\"}\n"
60
  " {\"audio_path\":\"<audio path or task_id>\"}\n"
 
185
  "and nothing else.\n"
186
  "Otherwise, return exactly one of these JSON literals to fetch another tool:\n"
187
  " {\"wiki_query\":\"<query for Wikipedia>\"}\n"
188
+
189
  " {\"ocr_path\":\"<image path or task_id>\"}\n"
190
  " {\"excel_path\":\"<xls path>\", \"excel_sheet_name\":\"<sheet name>\"}\n"
191
  " {\"audio_path\":\"<audio path or task_id>\"}\n"
 
196
  raw = llm_response.content.strip()
197
 
198
  new_msgs = state["messages"] + [AIMessage(content=raw)]
199
+
200
+ # Try to parse the LLM’s JSON
201
  try:
202
  parsed = json.loads(raw)
203
  if isinstance(parsed, dict):
204
+ # Check if the LLM explicitly provided "final_answer"
205
+ if "final_answer" in parsed:
206
+ return {"messages": new_msgs, "final_answer": parsed["final_answer"]}
207
+
208
+ # Check if it requested exactly one tool
209
+ valid_keys = {"wiki_query", "ocr_path", "excel_path", "excel_sheet_name", "audio_path"}
210
+ requested_keys = set(parsed.keys()) & valid_keys
211
+ if len(requested_keys) == 1:
212
+ # Return exactly that key so the graph goes to store_prev_state β†’ tools
213
+ return {**{"messages": new_msgs}, **{k: parsed[k] for k in requested_keys}}
214
+
 
 
 
215
  except json.JSONDecodeError:
216
  pass
217
 
218
+ # ----- NEW FALLBACK: If LLM didn’t give a valid tool key or final_answer,
219
+ # return the existing INTERIM_ANSWER as the final answer. -----
220
+ if ia := state.get("final_answer"):
221
+ return {"messages": new_msgs, "final_answer": ia}
222
+
223
+ # If there is no INTERIM_ANSWER either, then we genuinely cannot proceed
224
+ return {"messages": new_msgs, "final_answer": "ERROR: could not parse inspect decision."}
225
 
226
 
227
  # ─── 6) finalize_node ───