Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,31 +1,31 @@
|
|
1 |
-
from smolagents import CodeAgent,
|
2 |
import datetime
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
class DuckDuckGoAgent:
|
7 |
def __init__(self):
|
8 |
-
|
|
|
|
|
9 |
self.agent = CodeAgent(
|
10 |
-
tools=[
|
11 |
-
model=
|
12 |
additional_authorized_imports=['datetime']
|
13 |
)
|
14 |
|
15 |
def __call__(self, question: str) -> str:
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
if not response:
|
21 |
-
return "No information found."
|
22 |
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
print(f"Error during agent search: {e}")
|
31 |
-
return "Sorry, I couldn't find an answer."
|
|
|
1 |
+
from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, WikipediaSearchTool
|
2 |
import datetime
|
3 |
|
4 |
+
class SmartDuckDuckGoAgent:
|
|
|
|
|
5 |
def __init__(self):
|
6 |
+
self.wikipedia_tool = WikipediaSearchTool()
|
7 |
+
self.duckduckgo_tool = DuckDuckGoSearchTool()
|
8 |
+
self.model = HfApiModel()
|
9 |
self.agent = CodeAgent(
|
10 |
+
tools=[self.wikipedia_tool, self.duckduckgo_tool],
|
11 |
+
model=self.model,
|
12 |
additional_authorized_imports=['datetime']
|
13 |
)
|
14 |
|
15 |
def __call__(self, question: str) -> str:
|
16 |
+
# 1. Intentar buscar en Wikipedia
|
17 |
+
wiki_response = self.wikipedia_tool.run(question)
|
18 |
+
if wiki_response and "No results found" not in wiki_response:
|
19 |
+
return wiki_response
|
|
|
|
|
20 |
|
21 |
+
# 2. Si Wikipedia falla, intentar DuckDuckGo
|
22 |
+
duck_response = self.duckduckgo_tool.run(question)
|
23 |
+
if duck_response and "No results found" not in duck_response:
|
24 |
+
return duck_response
|
25 |
|
26 |
+
# 3. Si ambos fallan, usar el modelo para generar una respuesta educada
|
27 |
+
fallback_prompt = f"""I couldn't find a direct answer to the following question:
|
28 |
+
{question}
|
29 |
+
|
30 |
+
Based on your general knowledge, provide a reasonable and well-written answer."""
|
31 |
+
return self.model.run(fallback_prompt)
|
|
|
|