File size: 4,452 Bytes
50cf7de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5d264f
50cf7de
 
f17bad1
 
 
 
 
 
 
 
50cf7de
 
 
 
 
 
 
 
 
f17bad1
50cf7de
 
 
 
 
f17bad1
 
 
 
 
 
 
 
 
 
 
 
50cf7de
f17bad1
 
 
 
50cf7de
f17bad1
50cf7de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5d264f
50cf7de
 
 
 
 
 
 
 
 
 
 
f17bad1
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Libs
import os
import yaml
import importlib

from smolagents import (
    CodeAgent,
    DuckDuckGoSearchTool,
    VisitWebpageTool,
    WikipediaSearchTool,
    OpenAIServerModel,
    SpeechToTextTool,
    FinalAnswerTool,
)

# Local
from tools import GetTaskFileTool, LoadXlsxFileTool, LoadTextFileTool, AnalyzeImageTool

sys_instruction = (
    "You are a general-purpose AI assistant. For each question, first reason through your answer, "
    "then use the 'final_answer' tool to submit ONLY the final output.\n"
    "Your final answer MUST be one of the following:\n"
    "- A number (without commas or units, unless explicitly instructed),\n"
    "- A short phrase,\n"
    "- A comma-separated list of numbers or strings.\n"
    "For strings, avoid articles and abbreviations, and write digits fully unless told otherwise. "
    "Apply these formatting rules consistently to each item in a list."
)

prompts = yaml.safe_load(
    importlib.resources.files("smolagents.prompts").joinpath("code_agent.yaml").read_text()
)
prompts["system_prompt"] = sys_instruction + prompts["system_prompt"]

# req_instruction = (
req_instruction = (
    "You are a highly capable and autonomous agent named {{name}}, designed to solve complex tasks efficiently.\n"
    "A valued client has assigned you the following task:\n"
    "---\n"
    "Task:\n"
    "{{task}}\n"
    "---\n"
    "To complete this task successfully, follow these steps carefully:\n"
    "    1. Comprehend the task and identify the intended goal.\n"
    "    2. Break the task into clear, logical steps.\n"
    "    3. Select and prepare the tools or resources you need.\n"
    "    4. Set up the required environment or context.\n"
    "    5. Execute each step methodically.\n"
    "    6. Monitor outcomes and identify any deviations.\n"
    "    7. Revise your plan if necessary based on feedback.\n"
    "    8. Maintain internal state and track progress.\n"
    "    9. Verify that the goal has been fully achieved.\n"
    "   10. Present the final result clearly and concisely.\n"
    "If you succeed, you will be rewarded with a significant bonus.\n\n"
    "Your final_answer MUST be:\n"
    "- a number (retain its original type; do not include units),\n"
    "- a concise phrase,\n"
    "- or a comma-separated list of numbers or strings (no articles, no abbreviations).\n\n"
    "Only the content passed to the final_answer tool will be preserved. Any other content will be discarded."
)

prompts['managed_agent']['task'] = req_instruction
prompts['managed_agent']['report'] = "{{final_answer}}"


def get_model(
        model_id: str = "gpt-4.1-mini",
        model_temperature: float = 0.7,
    ):
    """
    Create and return an OpenAIServerModel instance with the specified model ID and temperature.
    """
    # Load the model
    if "gpt" in model_id:
        model = OpenAIServerModel(
                model_id=model_id,
                api_key=os.getenv("OPENAI_API_KEY"),
                temperature=model_temperature
        )
    elif "gemini" in model_id:
        model = OpenAIServerModel(
                model_id=model_id,
                api_key=os.getenv("GOOGLEAI_API_KEY"),
                api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
                temperature=model_temperature
        )
    else:
        raise ValueError(f"Unknown model_id: {model_id}. Supported models are gpt and gemini.")
    return model

def get_agent(
        model_id: str = "gpt-4.1-mini",
        model_temperature: float = 0.7,
        agent_max_steps: int = 15,
    ):
    """
    Create and return a CodeAgent instance with the specified model and tools.
    """

    # Defne the agent with the tools
    agent = CodeAgent(
        tools=[
            DuckDuckGoSearchTool(),
            VisitWebpageTool(),
            WikipediaSearchTool(),
            GetTaskFileTool(),
            SpeechToTextTool(),
            LoadXlsxFileTool(),
            LoadTextFileTool(),
            AnalyzeImageTool(model_id=model_id),
            FinalAnswerTool(),
        ],
        model=get_model(
            model_id=model_id,
            model_temperature=model_temperature,
        ),
        prompt_templates=prompts,
        max_steps=agent_max_steps,
        additional_authorized_imports = ["pandas"],
        name="GAIAAgent",
    )
    return agent

def test():
    """test tests something. """
    # Test the agent with a simple task
    return True