File size: 2,015 Bytes
37c9a6b
6220346
 
 
a6c261a
 
37c9a6b
6220346
37c9a6b
 
 
 
 
 
cdf8921
 
 
a6c261a
cdf8921
37c9a6b
 
 
 
 
 
 
 
 
 
6220346
37c9a6b
 
 
 
 
 
 
 
 
 
6220346
 
37c9a6b
 
 
ad8f765
 
 
 
37c9a6b
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from abc import abstractmethod, ABC
from smolagents import CodeAgent, Tool
from agent.constants import ADDITIONAL_AUTHORIZED_IMPORT
from tools.tools_utils import ToolsUtils


class BaseAgent(ABC):
    def __init__(self, model_name: str, tools: list[Tool] | None = None, planning_interval: int = 3, max_steps: int = 12, use_all_custom_tools: bool = True):
        self.model_name: str = model_name
        self.planning_interval = planning_interval
        self.max_steps = max_steps
        self.use_all_custom_tools = use_all_custom_tools
        self.tools: list[Tool] = self.init_tools(tools)
        self.agent = self.init_agent()

    def __call__(self, question: str) -> str:
        print(f"Agent received question (first 50 chars): {question[:50]}...")
        fixed_answer = self.agent.run(question)
        print(f"Agent returning fixed answer: {fixed_answer}")
        return fixed_answer

    @abstractmethod
    def get_model(self):
        pass

    def init_tools(self, tools: list[Tool] | None = None):
        if tools is None:
            tools = []
        if self.use_all_custom_tools:
            tools = ToolsUtils.get_default_tools()
        return tools

    def add_tool(self, tool: Tool):
        self.tools.append(tool)

    def init_agent(self):
        agent = CodeAgent(
            model=self.get_model(),
            tools=[t for t in self.tools],
            add_base_tools=True,
            verbosity_level=2,
            additional_authorized_imports=ADDITIONAL_AUTHORIZED_IMPORT,
            planning_interval=self.planning_interval,
            max_steps=self.max_steps
        )
        agent.system_prompt += ("\nWhen answering, provide ONLY the precise answer requested. "
                                "Do not include explanations, steps, reasoning, or additional text. "
                                "Be direct and specific. GAIA benchmark requires exact matching answers.")
        # print(agent.system_prompt)
        print("Agent initialized.")
        return agent