Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,14 +18,19 @@ from functools import lru_cache
|
|
| 18 |
|
| 19 |
from smolagents import CodeAgent, tool
|
| 20 |
from smolagents.models import LiteLLMModel
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# (Keep Constants as is)
|
| 23 |
# --- Constants ---
|
| 24 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 25 |
load_dotenv()
|
| 26 |
|
|
|
|
|
|
|
| 27 |
@lru_cache(maxsize=100)
|
| 28 |
-
def
|
|
|
|
| 29 |
with DDGS(timeout=10) as ddgs:
|
| 30 |
return list(ddgs.text(query, max_results=1))
|
| 31 |
|
|
@@ -42,20 +47,24 @@ def wikipedia_search(query: str) -> str:
|
|
| 42 |
wikipedia.set_lang("en")
|
| 43 |
|
| 44 |
try:
|
|
|
|
| 45 |
return wikipedia.summary(query, sentences=2)
|
| 46 |
|
| 47 |
except wikipedia.DisambiguationError as e:
|
|
|
|
| 48 |
try:
|
| 49 |
return wikipedia.summary(e.options[0], sentences=2)
|
|
|
|
| 50 |
except Exception:
|
| 51 |
-
return "Wikipedia returned multiple
|
| 52 |
|
| 53 |
except wikipedia.PageError:
|
|
|
|
| 54 |
return "Wikipedia page not found."
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
-
return f"Wikipedia Error: {e}"
|
| 58 |
|
|
|
|
| 59 |
|
| 60 |
@tool
|
| 61 |
def web_search(query: str) -> str:
|
|
@@ -67,7 +76,8 @@ def web_search(query: str) -> str:
|
|
| 67 |
"""
|
| 68 |
|
| 69 |
try:
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
if not results:
|
| 73 |
return "No search results."
|
|
@@ -115,32 +125,28 @@ class BasicAgent:
|
|
| 115 |
temperature=0,
|
| 116 |
)
|
| 117 |
|
| 118 |
-
self.agent =
|
| 119 |
model=self.model,
|
| 120 |
tools=[
|
| 121 |
web_search,
|
| 122 |
wikipedia_search,
|
| 123 |
],
|
| 124 |
max_steps=3,
|
| 125 |
-
add_base_tools=False,
|
| 126 |
-
verbosity_level=0,
|
| 127 |
)
|
| 128 |
|
| 129 |
def __call__(self, question: str):
|
| 130 |
|
| 131 |
prompt = f"""
|
| 132 |
-
You are a
|
| 133 |
|
| 134 |
Rules:
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
7. Return ONLY the final answer.
|
| 143 |
-
8. Stop as soon as you know the answer.
|
| 144 |
|
| 145 |
Question:
|
| 146 |
|
|
|
|
| 18 |
|
| 19 |
from smolagents import CodeAgent, tool
|
| 20 |
from smolagents.models import LiteLLMModel
|
| 21 |
+
from smolagents import ToolCallingAgent, tool
|
| 22 |
+
from ddgs import DDGS
|
| 23 |
|
| 24 |
# (Keep Constants as is)
|
| 25 |
# --- Constants ---
|
| 26 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 27 |
load_dotenv()
|
| 28 |
|
| 29 |
+
from functools import lru_cache
|
| 30 |
+
|
| 31 |
@lru_cache(maxsize=100)
|
| 32 |
+
def cached_search(query: str):
|
| 33 |
+
|
| 34 |
with DDGS(timeout=10) as ddgs:
|
| 35 |
return list(ddgs.text(query, max_results=1))
|
| 36 |
|
|
|
|
| 47 |
wikipedia.set_lang("en")
|
| 48 |
|
| 49 |
try:
|
| 50 |
+
|
| 51 |
return wikipedia.summary(query, sentences=2)
|
| 52 |
|
| 53 |
except wikipedia.DisambiguationError as e:
|
| 54 |
+
|
| 55 |
try:
|
| 56 |
return wikipedia.summary(e.options[0], sentences=2)
|
| 57 |
+
|
| 58 |
except Exception:
|
| 59 |
+
return "Wikipedia returned multiple pages."
|
| 60 |
|
| 61 |
except wikipedia.PageError:
|
| 62 |
+
|
| 63 |
return "Wikipedia page not found."
|
| 64 |
|
| 65 |
except Exception as e:
|
|
|
|
| 66 |
|
| 67 |
+
return str(e)
|
| 68 |
|
| 69 |
@tool
|
| 70 |
def web_search(query: str) -> str:
|
|
|
|
| 76 |
"""
|
| 77 |
|
| 78 |
try:
|
| 79 |
+
|
| 80 |
+
results = cached_search(query)
|
| 81 |
|
| 82 |
if not results:
|
| 83 |
return "No search results."
|
|
|
|
| 125 |
temperature=0,
|
| 126 |
)
|
| 127 |
|
| 128 |
+
self.agent = ToolCallingAgent(
|
| 129 |
model=self.model,
|
| 130 |
tools=[
|
| 131 |
web_search,
|
| 132 |
wikipedia_search,
|
| 133 |
],
|
| 134 |
max_steps=3,
|
|
|
|
|
|
|
| 135 |
)
|
| 136 |
|
| 137 |
def __call__(self, question: str):
|
| 138 |
|
| 139 |
prompt = f"""
|
| 140 |
+
You are a factual question answering assistant.
|
| 141 |
|
| 142 |
Rules:
|
| 143 |
|
| 144 |
+
- Use as few tool calls as possible.
|
| 145 |
+
- Prefer web_search first.
|
| 146 |
+
- Use wikipedia_search only if needed.
|
| 147 |
+
- Never repeat the same search.
|
| 148 |
+
- Stop immediately once you know the answer.
|
| 149 |
+
- Return ONLY the final answer.
|
|
|
|
|
|
|
| 150 |
|
| 151 |
Question:
|
| 152 |
|