Spaces:
Sleeping
Sleeping
tobyvertommen Claude Sonnet 4.6 commited on
Commit ·
cac1a6d
1
Parent(s): b6181ef
Add read_webpage tool for reliable page content extraction
Browse filesDuckDuckGo snippets cause hallucination and inconsistent answers.
read_webpage fetches the full page text, enabling agent to read:
- Wikipedia articles directly
- Sports stats pages (Baseball Reference)
- Scientific papers and LibreTexts exercises
- News articles with specific award numbers
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
from langchain_openai import ChatOpenAI
|
| 6 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 7 |
from langchain_experimental.tools import PythonREPLTool
|
|
|
|
| 8 |
from langchain_core.messages import SystemMessage, HumanMessage, ToolMessage
|
| 9 |
|
| 10 |
# --- Constants ---
|
|
@@ -12,26 +14,44 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
|
| 13 |
SYSTEM_PROMPT = """You are a general AI assistant. Answer GAIA benchmark questions accurately.
|
| 14 |
|
| 15 |
-
|
| 16 |
-
- duckduckgo_search:
|
| 17 |
-
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
FINAL ANSWER: [your answer]
|
| 28 |
|
| 29 |
-
STRICT format rules (
|
| 30 |
-
- Numbers: digits only, no
|
| 31 |
-
- Country names: full name (e.g. "
|
| 32 |
- Strings: no surrounding quotes, no trailing punctuation, no articles (a/an/the)
|
| 33 |
- Lists: comma-separated, no spaces after commas (e.g. "a,b,c" not "a, b, c")
|
| 34 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
class BasicAgent:
|
|
@@ -39,6 +59,7 @@ class BasicAgent:
|
|
| 39 |
self.llm = ChatOpenAI(model="gpt-4o", temperature=0)
|
| 40 |
self.tools = [
|
| 41 |
DuckDuckGoSearchRun(),
|
|
|
|
| 42 |
PythonREPLTool(),
|
| 43 |
]
|
| 44 |
self.tools_map = {t.name: t for t in self.tools}
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import pandas as pd
|
| 6 |
from langchain_openai import ChatOpenAI
|
| 7 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 8 |
from langchain_experimental.tools import PythonREPLTool
|
| 9 |
+
from langchain_core.tools import tool
|
| 10 |
from langchain_core.messages import SystemMessage, HumanMessage, ToolMessage
|
| 11 |
|
| 12 |
# --- Constants ---
|
|
|
|
| 14 |
|
| 15 |
SYSTEM_PROMPT = """You are a general AI assistant. Answer GAIA benchmark questions accurately.
|
| 16 |
|
| 17 |
+
Available tools:
|
| 18 |
+
- duckduckgo_search: find URLs and snippets. Use for initial discovery.
|
| 19 |
+
- read_webpage: read the FULL content of a specific URL. Use this after finding a relevant URL via search to get precise details.
|
| 20 |
+
- Python_REPL: calculations, data analysis. ALWAYS use print() to show results.
|
| 21 |
+
|
| 22 |
+
Research strategy:
|
| 23 |
+
1. Search to find the relevant page/source.
|
| 24 |
+
2. Use read_webpage on the specific URL to read actual content — do not rely only on search snippets.
|
| 25 |
+
3. For Wikipedia questions: read the Wikipedia page directly.
|
| 26 |
+
4. For sports stats: read Baseball Reference, Sports Reference, or similar stat pages.
|
| 27 |
+
5. For reversed/encoded text: decode yourself, no tools needed.
|
| 28 |
+
6. For YouTube videos: search for the video title + key question terms.
|
| 29 |
+
7. For questions mentioning attached files: file is unavailable — search the web for the answer.
|
| 30 |
+
|
| 31 |
+
When you have your final answer, output ONLY:
|
| 32 |
FINAL ANSWER: [your answer]
|
| 33 |
|
| 34 |
+
STRICT format rules (exact match scoring):
|
| 35 |
+
- Numbers: digits only, no $, no commas, no units unless asked
|
| 36 |
+
- Country names: full name (e.g. "Malta" not "MLT" or "Malta (MLT)")
|
| 37 |
- Strings: no surrounding quotes, no trailing punctuation, no articles (a/an/the)
|
| 38 |
- Lists: comma-separated, no spaces after commas (e.g. "a,b,c" not "a, b, c")
|
| 39 |
+
- Best guess required — never output "No answer found" or placeholder text"""
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@tool
|
| 43 |
+
def read_webpage(url: str) -> str:
|
| 44 |
+
"""Read the full text content of a webpage. Use after finding a relevant URL via search to get precise information."""
|
| 45 |
+
try:
|
| 46 |
+
headers = {"User-Agent": "Mozilla/5.0 (compatible; research-agent/1.0)"}
|
| 47 |
+
resp = requests.get(url, headers=headers, timeout=15, allow_redirects=True)
|
| 48 |
+
if resp.status_code != 200:
|
| 49 |
+
return f"Could not fetch page: HTTP {resp.status_code}"
|
| 50 |
+
text = re.sub(r"<[^>]+>", " ", resp.text)
|
| 51 |
+
text = re.sub(r"\s+", " ", text).strip()
|
| 52 |
+
return text[:6000]
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return f"Error reading page: {e}"
|
| 55 |
|
| 56 |
|
| 57 |
class BasicAgent:
|
|
|
|
| 59 |
self.llm = ChatOpenAI(model="gpt-4o", temperature=0)
|
| 60 |
self.tools = [
|
| 61 |
DuckDuckGoSearchRun(),
|
| 62 |
+
read_webpage,
|
| 63 |
PythonREPLTool(),
|
| 64 |
]
|
| 65 |
self.tools_map = {t.name: t for t in self.tools}
|