Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
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 |
-
|
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 |
-
|
203 |
-
|
204 |
-
"final_answer"
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
if k in allowed:
|
214 |
-
partial[k] = v
|
215 |
-
return partial
|
216 |
except json.JSONDecodeError:
|
217 |
pass
|
218 |
|
219 |
-
|
220 |
-
|
221 |
-
|
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 βββ
|