Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -3,12 +3,14 @@ import datetime
|
|
3 |
|
4 |
class SmartDuckDuckGoAgent:
|
5 |
def __init__(self):
|
6 |
-
#
|
7 |
self.wikipedia_tool = WikipediaSearchTool()
|
8 |
self.duckduckgo_tool = DuckDuckGoSearchTool()
|
|
|
9 |
# Modelo
|
10 |
self.model = HfApiModel()
|
11 |
-
|
|
|
12 |
self.agent = CodeAgent(
|
13 |
tools=[self.wikipedia_tool, self.duckduckgo_tool],
|
14 |
model=self.model,
|
@@ -16,17 +18,23 @@ class SmartDuckDuckGoAgent:
|
|
16 |
)
|
17 |
|
18 |
def __call__(self, question: str) -> str:
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
#
|
30 |
fallback_prompt = f"""I couldn't find a direct answer to the following question:
|
31 |
{question}
|
32 |
|
|
|
3 |
|
4 |
class SmartDuckDuckGoAgent:
|
5 |
def __init__(self):
|
6 |
+
# Definir los tools de búsqueda
|
7 |
self.wikipedia_tool = WikipediaSearchTool()
|
8 |
self.duckduckgo_tool = DuckDuckGoSearchTool()
|
9 |
+
|
10 |
# Modelo
|
11 |
self.model = HfApiModel()
|
12 |
+
|
13 |
+
# Crear el agente con los tools y el modelo
|
14 |
self.agent = CodeAgent(
|
15 |
tools=[self.wikipedia_tool, self.duckduckgo_tool],
|
16 |
model=self.model,
|
|
|
18 |
)
|
19 |
|
20 |
def __call__(self, question: str) -> str:
|
21 |
+
# Intentar buscar en Wikipedia primero
|
22 |
+
try:
|
23 |
+
wiki_response = self.wikipedia_tool.search(question) # Usa 'search' en lugar de 'run'
|
24 |
+
if wiki_response:
|
25 |
+
return wiki_response
|
26 |
+
except Exception as e:
|
27 |
+
print(f"Error buscando en Wikipedia: {e}")
|
28 |
+
|
29 |
+
# Si Wikipedia no tiene resultados, intentar con DuckDuckGo
|
30 |
+
try:
|
31 |
+
duck_response = self.duckduckgo_tool.run(question)
|
32 |
+
if duck_response:
|
33 |
+
return duck_response
|
34 |
+
except Exception as e:
|
35 |
+
print(f"Error buscando en DuckDuckGo: {e}")
|
36 |
|
37 |
+
# Si ambos fallan, usar el modelo para generar una respuesta
|
38 |
fallback_prompt = f"""I couldn't find a direct answer to the following question:
|
39 |
{question}
|
40 |
|