File size: 1,020 Bytes
82bfd97
 
 
 
 
 
10e9b7d
e80aab9
31243f4
 
82bfd97
 
 
 
 
 
 
 
 
 
 
 
 
e80aab9
e514fd7
82bfd97
 
 
 
 
 
 
 
 
 
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
33
34
35
from smolagents import (
    CodeAgent,
    DuckDuckGoSearchTool,
    VisitWebpageTool,
    InferenceClientModel,
)


class BasicAgent:
    def __init__(self):
        token = os.getenv("HF_TOKEN")
        if not token:
            raise ValueError("Add HF_TOKEN in your Space's Settings → Secrets.")

        model = InferenceClientModel(token=token)

        self.agent = CodeAgent(
            tools=[
                DuckDuckGoSearchTool(),
                VisitWebpageTool(),
            ],
            model=model,
            max_steps=12,
        )

    def __call__(self, question: str) -> str:
        prompt = (
            "Answer the following question accurately. "
            "Use web search and visit source pages when needed. "
            "Follow the requested answer format exactly. "
            "Return only the answer, without explanations, citations, "
            "or a 'FINAL ANSWER' prefix.\n\n"
            + question
        )
        return str(self.agent.run(prompt, reset=True)).strip()