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