File size: 1,035 Bytes
667e88a
 
 
 
ef96de0
 
 
667e88a
ef96de0
667e88a
 
 
ef96de0
 
 
667e88a
ef96de0
 
 
667e88a
 
 
 
 
 
 
 
ef96de0
 
 
 
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, 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."