final-agent-course / agent.py
jjvelezo's picture
Create agent.py
ef96de0 verified
raw
history blame
646 Bytes
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
class DuckDuckGoAgent:
def __init__(self):
self.agent = CodeAgent(
tools=[DuckDuckGoSearchTool()],
model=HfApiModel()
)
def __call__(self, question: str) -> str:
"""Recibe una pregunta, busca en DuckDuckGo y devuelve una respuesta."""
print(f"Running search for question: {question}")
try:
response = self.agent.run(question)
return response
except Exception as e:
print(f"Error during agent search: {e}")
return "Sorry, I couldn't find an answer."