File size: 3,283 Bytes
fe9507a
27884a0
 
 
 
 
 
fe9507a
 
27884a0
 
 
fe9507a
27884a0
fe9507a
 
27884a0
fe9507a
27884a0
 
fe9507a
 
 
 
 
27884a0
 
 
fe9507a
27884a0
fe9507a
27884a0
 
 
fe9507a
 
27884a0
 
 
fe9507a
27884a0
fe9507a
27884a0
fe9507a
27884a0
fe9507a
 
 
27884a0
 
fe9507a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import importlib
import os

import requests
import yaml
import pandas as pd

from config import DEFAULT_API_URL
from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, WikipediaSearchTool, Tool, OpenAIServerModel, SpeechToTextTool

class GetTaskFileTool(Tool):
    name = "get_task_file_tool"
    description = """This tool downloads the file content associated with the given task_id if exists. Returns absolute file path"""
    inputs = {
        "task_id": {"type": "string", "description": "Task id"},
        "file_name": {"type": "string", "description": "File name"},
    }
    output_type = "string"

    def forward(self, task_id: str, file_name: str) -> str:
        response = requests.get(f"{DEFAULT_API_URL}/files/{task_id}", timeout=15)
        response.raise_for_status()
        with open(file_name, 'wb') as file:
            file.write(response.content)
        return os.path.abspath(file_name)

class LoadXlsxFileTool(Tool):
    name = "load_xlsx_file_tool"
    description = """This tool loads xlsx file into pandas and returns it"""
    inputs = {
        "file_path": {"type": "string", "description": "File path"}
    }
    output_type = "object"

    def forward(self, file_path: str) -> object:
        return pd.read_excel(file_path)

class LoadTextFileTool(Tool):
    name = "load_text_file_tool"
    description = """This tool loads any text file"""
    inputs = {
        "file_path": {"type": "string", "description": "File path"}
    }
    output_type = "string"

    def forward(self, file_path: str) -> object:
        with open(file_path, 'r', encoding='utf-8') as file:
            return file.read()


prompts = yaml.safe_load(
    importlib.resources.files("smolagents.prompts").joinpath("code_agent.yaml").read_text()
)
prompts["system_prompt"] = ("You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string. "
                            + prompts["system_prompt"])

def init_agent():
    gemini_model = OpenAIServerModel(
            model_id="gemini-2.0-flash",
            api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
            api_key=os.getenv("API_KEY"),
            temperature=0.7
    )
    agent = CodeAgent(
        tools=[
            DuckDuckGoSearchTool(),
            VisitWebpageTool(),
            WikipediaSearchTool(),
            GetTaskFileTool(),
            SpeechToTextTool(),
            LoadXlsxFileTool(),
            LoadTextFileTool()
        ],
        model=gemini_model,
        prompt_templates=prompts,
        max_steps=15,
        additional_authorized_imports = ["pandas"]
    )
    return agent