jjvelezo commited on
Commit
48c606b
·
verified ·
1 Parent(s): d88049a

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +21 -21
agent.py CHANGED
@@ -1,31 +1,31 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, WikipediaSearchTool
2
  import datetime
3
 
4
-
5
-
6
- class DuckDuckGoAgent:
7
  def __init__(self):
8
-
 
 
9
  self.agent = CodeAgent(
10
- tools=[DuckDuckGoSearchTool(), WikipediaSearchTool()],
11
- model=HfApiModel(),
12
  additional_authorized_imports=['datetime']
13
  )
14
 
15
  def __call__(self, question: str) -> str:
16
- """Busca la respuesta usando Internet o Wikipedia, luego limita la longitud."""
17
- print(f"Running search for question: {question}")
18
- try:
19
- response = self.agent.run(question)
20
- if not response:
21
- return "No information found."
22
 
 
 
 
 
23
 
24
- max_length = 200
25
- response = response.strip()
26
- if len(response) > max_length:
27
- response = response[:max_length].rsplit('.', 1)[0] + '.'
28
- return response
29
- except Exception as e:
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)