Update agent.py
Browse files
agent.py
CHANGED
|
@@ -43,9 +43,8 @@ def wiki_search(query: str) -> str:
|
|
| 43 |
docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 44 |
if not docs:
|
| 45 |
return "No Wikipedia results found."
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
".join(d.page_content[:2200] for d in docs)
|
| 49 |
except Exception as e:
|
| 50 |
return f"Wikipedia unavailable: {type(e).__name__}."
|
| 51 |
|
|
@@ -67,8 +66,8 @@ def fetch_page(url: str) -> str:
|
|
| 67 |
soup = BeautifulSoup(r.text, "html.parser")
|
| 68 |
for tag in soup(["script", "style", "nav", "footer", "header"]):
|
| 69 |
tag.decompose()
|
| 70 |
-
|
| 71 |
-
", strip=True)
|
| 72 |
return text[:9000] if text else "Page appears empty."
|
| 73 |
except Exception as e:
|
| 74 |
return f"Could not fetch page: {type(e).__name__}."
|
|
@@ -104,7 +103,8 @@ TOOLS = [wiki_search, web_search, fetch_page, run_python]
|
|
| 104 |
# PRECHECKS
|
| 105 |
# ==========================================================
|
| 106 |
|
| 107 |
-
def maybe_answer_direct(question: str) -> str
|
|
|
|
| 108 |
global LAST_PRECHECK
|
| 109 |
q = question.strip()
|
| 110 |
ql = q.lower()
|
|
@@ -183,15 +183,12 @@ Final format:
|
|
| 183 |
FINAL ANSWER: <answer>
|
| 184 |
"""
|
| 185 |
|
|
|
|
| 186 |
ROUTE_PROMPTS = {
|
| 187 |
-
'python': BASE_RULES + "
|
| 188 |
-
|
| 189 |
-
'
|
| 190 |
-
|
| 191 |
-
'web': BASE_RULES + "
|
| 192 |
-
This is a web or URL task. Use web_search and fetch_page sparingly.",
|
| 193 |
-
'default': BASE_RULES + "
|
| 194 |
-
Choose the shortest reliable path."
|
| 195 |
}
|
| 196 |
|
| 197 |
# ==========================================================
|
|
@@ -329,20 +326,22 @@ def _validate_by_question(answer: str, question: str) -> str:
|
|
| 329 |
return parts[-1] if len(parts) >= 1 and len(parts) <= 4 else 'N/A'
|
| 330 |
|
| 331 |
if 'ioc country code' in q:
|
| 332 |
-
|
|
|
|
| 333 |
return m.group(0) if m else 'N/A'
|
| 334 |
|
| 335 |
if 'how many' in q or 'what is the final numeric output' in q:
|
| 336 |
-
|
|
|
|
| 337 |
return m.group(0) if m else 'N/A'
|
| 338 |
|
| 339 |
if 'algebraic notation' in q:
|
| 340 |
-
|
|
|
|
| 341 |
return m.group(0) if m else 'N/A'
|
| 342 |
|
| 343 |
if 'comma-separated' in q or 'use them in the form' in q:
|
| 344 |
-
if len(a) <= 80 and '
|
| 345 |
-
' not in a:
|
| 346 |
return a
|
| 347 |
return 'N/A'
|
| 348 |
|
|
@@ -405,4 +404,4 @@ def get_last_trace() -> dict:
|
|
| 405 |
'model_error': LAST_MODEL_ERROR,
|
| 406 |
'route': LAST_ROUTE,
|
| 407 |
'precheck': LAST_PRECHECK,
|
| 408 |
-
}
|
|
|
|
| 43 |
docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 44 |
if not docs:
|
| 45 |
return "No Wikipedia results found."
|
| 46 |
+
# Fixed: String was broken across lines
|
| 47 |
+
return "\n\n".join(d.page_content[:2200] for d in docs)
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
return f"Wikipedia unavailable: {type(e).__name__}."
|
| 50 |
|
|
|
|
| 66 |
soup = BeautifulSoup(r.text, "html.parser")
|
| 67 |
for tag in soup(["script", "style", "nav", "footer", "header"]):
|
| 68 |
tag.decompose()
|
| 69 |
+
# Fixed: String was broken across lines
|
| 70 |
+
text = soup.get_text(separator="\n", strip=True)
|
| 71 |
return text[:9000] if text else "Page appears empty."
|
| 72 |
except Exception as e:
|
| 73 |
return f"Could not fetch page: {type(e).__name__}."
|
|
|
|
| 103 |
# PRECHECKS
|
| 104 |
# ==========================================================
|
| 105 |
|
| 106 |
+
def maybe_answer_direct(question: str) -> str or None:
|
| 107 |
+
# Fixed: Corrected indentation for this block
|
| 108 |
global LAST_PRECHECK
|
| 109 |
q = question.strip()
|
| 110 |
ql = q.lower()
|
|
|
|
| 183 |
FINAL ANSWER: <answer>
|
| 184 |
"""
|
| 185 |
|
| 186 |
+
# Fixed: String formatting for route prompts
|
| 187 |
ROUTE_PROMPTS = {
|
| 188 |
+
'python': BASE_RULES + "\nThis is a structured task. Prefer one precise run_python call when needed.",
|
| 189 |
+
'wiki': BASE_RULES + "\nThis is stable factual knowledge. Prefer wiki_search first.",
|
| 190 |
+
'web': BASE_RULES + "\nThis is a web or URL task. Use web_search and fetch_page sparingly.",
|
| 191 |
+
'default': BASE_RULES + "\nChoose the shortest reliable path."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
}
|
| 193 |
|
| 194 |
# ==========================================================
|
|
|
|
| 326 |
return parts[-1] if len(parts) >= 1 and len(parts) <= 4 else 'N/A'
|
| 327 |
|
| 328 |
if 'ioc country code' in q:
|
| 329 |
+
# Fixed: Changed invalid to \b
|
| 330 |
+
m = re.search(r'\b[A-Z]{3}\b', a)
|
| 331 |
return m.group(0) if m else 'N/A'
|
| 332 |
|
| 333 |
if 'how many' in q or 'what is the final numeric output' in q:
|
| 334 |
+
# Fixed: Changed invalid to \b
|
| 335 |
+
m = re.search(r'\b\d+(?:\.\d+)?\b', a)
|
| 336 |
return m.group(0) if m else 'N/A'
|
| 337 |
|
| 338 |
if 'algebraic notation' in q:
|
| 339 |
+
# Fixed: Changed invalid to \b
|
| 340 |
+
m = re.search(r'\b(?:O-O-O|O-O|[KQRBN]?[a-h]?[1-8]?x?[a-h][1-8](?:=[QRBN])?[+#]?)\b', a)
|
| 341 |
return m.group(0) if m else 'N/A'
|
| 342 |
|
| 343 |
if 'comma-separated' in q or 'use them in the form' in q:
|
| 344 |
+
if len(a) <= 80 and '\n' not in a:
|
|
|
|
| 345 |
return a
|
| 346 |
return 'N/A'
|
| 347 |
|
|
|
|
| 404 |
'model_error': LAST_MODEL_ERROR,
|
| 405 |
'route': LAST_ROUTE,
|
| 406 |
'precheck': LAST_PRECHECK,
|
| 407 |
+
}
|