jjvelezo commited on
Commit
61e90fc
·
verified ·
1 Parent(s): 39990b6

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +20 -12
agent.py CHANGED
@@ -3,12 +3,14 @@ import datetime
3
 
4
  class SmartDuckDuckGoAgent:
5
  def __init__(self):
6
- # Herramientas de búsqueda
7
  self.wikipedia_tool = WikipediaSearchTool()
8
  self.duckduckgo_tool = DuckDuckGoSearchTool()
 
9
  # Modelo
10
  self.model = HfApiModel()
11
- # Agente con herramientas y modelo
 
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
- # 1. Intentar buscar en Wikipedia
20
- wiki_response = self.wikipedia_tool.search(question)
21
- if wiki_response and "No results found" not in wiki_response:
22
- return wiki_response
23
-
24
- # 2. Si Wikipedia falla, intentar DuckDuckGo
25
- duck_response = self.duckduckgo_tool.run(question)
26
- if duck_response and "No results found" not in duck_response:
27
- return duck_response
 
 
 
 
 
 
28
 
29
- # 3. Si ambos fallan, usar el modelo para generar una respuesta educada
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