Update app.py
Browse files
app.py
CHANGED
@@ -203,6 +203,17 @@ class BasicAgent:
|
|
203 |
print(f"\nLLM Error: {str(e)}")
|
204 |
raise
|
205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
def _analyze_question(self, state: AgentState) -> AgentState:
|
207 |
# Check for file attachments
|
208 |
if state["file_url"]:
|
@@ -217,13 +228,18 @@ class BasicAgent:
|
|
217 |
|
218 |
# Regular text question analysis
|
219 |
prompt = (
|
220 |
-
"
|
221 |
-
"{\
|
222 |
f"Question: {state['question']}"
|
223 |
)
|
224 |
try:
|
225 |
raw = self._call_llm(prompt)
|
226 |
-
|
|
|
|
|
|
|
|
|
|
|
227 |
state["needs_search"] = bool(decision.get("needs_search", False))
|
228 |
state["search_query"] = decision.get("search_query", state["question"])
|
229 |
except Exception as e:
|
|
|
203 |
print(f"\nLLM Error: {str(e)}")
|
204 |
raise
|
205 |
|
206 |
+
def _safe_parse(self, raw: str) -> dict:
|
207 |
+
"""Fallback parser for when JSON parsing fails."""
|
208 |
+
try:
|
209 |
+
# Try to extract a dict-like structure
|
210 |
+
match = re.search(r'\{.*\}', raw, re.DOTALL)
|
211 |
+
if match:
|
212 |
+
return ast.literal_eval(match.group(0))
|
213 |
+
except:
|
214 |
+
pass
|
215 |
+
return {"needs_search": True, "search_query": ""}
|
216 |
+
|
217 |
def _analyze_question(self, state: AgentState) -> AgentState:
|
218 |
# Check for file attachments
|
219 |
if state["file_url"]:
|
|
|
228 |
|
229 |
# Regular text question analysis
|
230 |
prompt = (
|
231 |
+
"Return ONLY valid JSON:\n"
|
232 |
+
"{\"needs_search\": bool, \"search_query\": str}\n\n"
|
233 |
f"Question: {state['question']}"
|
234 |
)
|
235 |
try:
|
236 |
raw = self._call_llm(prompt)
|
237 |
+
try:
|
238 |
+
decision = json.loads(raw)
|
239 |
+
except json.JSONDecodeError:
|
240 |
+
print(f"JSON parse error, falling back to safe parse. Raw response: {raw}")
|
241 |
+
decision = self._safe_parse(raw)
|
242 |
+
|
243 |
state["needs_search"] = bool(decision.get("needs_search", False))
|
244 |
state["search_query"] = decision.get("search_query", state["question"])
|
245 |
except Exception as e:
|