Spaces:
Sleeping
Sleeping
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) | |