final-agent-course / agent.py
jjvelezo's picture
Update agent.py
667e88a verified
raw
history blame
1.04 kB
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, WikipediaSearchTool
import datetime
class DuckDuckGoAgent:
def __init__(self):
self.agent = CodeAgent(
tools=[DuckDuckGoSearchTool(), WikipediaSearchTool()],
model=HfApiModel(),
additional_authorized_imports=['datetime']
)
def __call__(self, question: str) -> str:
"""Busca la respuesta usando Internet o Wikipedia, luego limita la longitud."""
print(f"Running search for question: {question}")
try:
response = self.agent.run(question)
if not response:
return "No information found."
max_length = 200
response = response.strip()
if len(response) > max_length:
response = response[:max_length].rsplit('.', 1)[0] + '.'
return response
except Exception as e:
print(f"Error during agent search: {e}")
return "Sorry, I couldn't find an answer."