Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,11 +12,23 @@ from duckduckgo_search import DDGS
|
|
| 12 |
from smolagents import CodeAgent, tool
|
| 13 |
from smolagents.models import LiteLLMModel
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# (Keep Constants as is)
|
| 16 |
# --- Constants ---
|
| 17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
load_dotenv()
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def wikipedia_search(query: str) -> str:
|
|
@@ -26,10 +38,23 @@ def wikipedia_search(query: str) -> str:
|
|
| 26 |
Args:
|
| 27 |
query: Search topic.
|
| 28 |
"""
|
|
|
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
-
return wikipedia.summary(query, sentences=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
return
|
| 33 |
|
| 34 |
|
| 35 |
@tool
|
|
@@ -40,12 +65,12 @@ def web_search(query: str) -> str:
|
|
| 40 |
Args:
|
| 41 |
query: Search query.
|
| 42 |
"""
|
|
|
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
results = list(ddgs.text(query, max_results=5))
|
| 46 |
|
| 47 |
if not results:
|
| 48 |
-
return "No
|
| 49 |
|
| 50 |
return "\n\n".join(
|
| 51 |
f"{r['title']}\n{r['body']}"
|
|
@@ -53,8 +78,7 @@ def web_search(query: str) -> str:
|
|
| 53 |
)
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
-
return
|
| 57 |
-
|
| 58 |
|
| 59 |
# def ask_agent(question: str) -> str:
|
| 60 |
# """
|
|
@@ -88,22 +112,42 @@ class BasicAgent:
|
|
| 88 |
self.model = LiteLLMModel(
|
| 89 |
model_id="groq/llama-3.3-70b-versatile",
|
| 90 |
api_key=os.getenv("GROQ_API_KEY"),
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
self.agent = CodeAgent(
|
| 94 |
model=self.model,
|
| 95 |
tools=[
|
| 96 |
-
wikipedia_search,
|
| 97 |
web_search,
|
|
|
|
| 98 |
],
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
)
|
| 101 |
|
| 102 |
-
def __call__(self, question: str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
|
|
|
|
| 105 |
|
| 106 |
-
return str(
|
| 107 |
|
| 108 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 109 |
"""
|
|
|
|
| 12 |
from smolagents import CodeAgent, tool
|
| 13 |
from smolagents.models import LiteLLMModel
|
| 14 |
|
| 15 |
+
from dotenv import load_dotenv
|
| 16 |
+
from duckduckgo_search import DDGS
|
| 17 |
+
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 cached_web_search(query: str):
|
| 29 |
+
with DDGS(timeout=10) as ddgs:
|
| 30 |
+
return list(ddgs.text(query, max_results=1))
|
| 31 |
+
|
| 32 |
|
| 33 |
@tool
|
| 34 |
def wikipedia_search(query: str) -> str:
|
|
|
|
| 38 |
Args:
|
| 39 |
query: Search topic.
|
| 40 |
"""
|
| 41 |
+
|
| 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 possible pages."
|
| 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
|
|
|
|
| 65 |
Args:
|
| 66 |
query: Search query.
|
| 67 |
"""
|
| 68 |
+
|
| 69 |
try:
|
| 70 |
+
results = cached_web_search(query)
|
|
|
|
| 71 |
|
| 72 |
if not results:
|
| 73 |
+
return "No search results."
|
| 74 |
|
| 75 |
return "\n\n".join(
|
| 76 |
f"{r['title']}\n{r['body']}"
|
|
|
|
| 78 |
)
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
+
return f"Search Error: {e}"
|
|
|
|
| 82 |
|
| 83 |
# def ask_agent(question: str) -> str:
|
| 84 |
# """
|
|
|
|
| 112 |
self.model = LiteLLMModel(
|
| 113 |
model_id="groq/llama-3.3-70b-versatile",
|
| 114 |
api_key=os.getenv("GROQ_API_KEY"),
|
| 115 |
+
temperature=0,
|
| 116 |
)
|
| 117 |
|
| 118 |
self.agent = CodeAgent(
|
| 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 precise QA agent.
|
| 133 |
+
|
| 134 |
+
Rules:
|
| 135 |
+
|
| 136 |
+
1. Answer correctly.
|
| 137 |
+
2. Use at most ONE web search.
|
| 138 |
+
3. Only use Wikipedia if the web search is insufficient.
|
| 139 |
+
4. Never repeat the same search.
|
| 140 |
+
5. Never search for the same entity twice.
|
| 141 |
+
6. Do not explain your reasoning.
|
| 142 |
+
7. Return ONLY the final answer.
|
| 143 |
+
8. Stop as soon as you know the answer.
|
| 144 |
+
|
| 145 |
+
Question:
|
| 146 |
|
| 147 |
+
{question}
|
| 148 |
+
"""
|
| 149 |
|
| 150 |
+
return str(self.agent.run(prompt))
|
| 151 |
|
| 152 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 153 |
"""
|