lucasnseq commited on
Commit
50cf7de
·
verified ·
1 Parent(s): bce22dc

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +111 -101
agent.py CHANGED
@@ -1,102 +1,112 @@
1
- # Libs
2
- import os
3
- import yaml
4
- import importlib
5
-
6
- from smolagents import (
7
- CodeAgent,
8
- DuckDuckGoSearchTool,
9
- VisitWebpageTool,
10
- WikipediaSearchTool,
11
- OpenAIServerModel,
12
- SpeechToTextTool,
13
- FinalAnswerTool,
14
- )
15
-
16
- # Local
17
- from tools import GetTaskFileTool, LoadXlsxFileTool, LoadTextFileTool
18
-
19
- sys_instruction = (
20
- "You are a general AI assistant. Answer each question by reporting your thoughts, "
21
- "then submit ONLY a concise text using the 'final_answer' tool. "
22
- "Final answer MUST be a number, a few words, or a comma-separated list of numbers and/or strings. "
23
- "For numbers, avoid commas and units unless specified. For strings, avoid articles and abbreviations, "
24
- "and write digits in full unless stated otherwise. Apply these rules for list elements as well."
25
- )
26
-
27
- prompts = yaml.safe_load(
28
- importlib.resources.files("smolagents.prompts").joinpath("code_agent.yaml").read_text()
29
- )
30
- prompts["system_prompt"] = sys_instruction + prompts["system_prompt"]
31
-
32
- # req_instruction = (
33
- req_instruction = (
34
- "You are an expert and helpful agent named {{name}}.\n"
35
- "A valued client has assigned you the following task:\n"
36
- "---\n"
37
- "Task:\n"
38
- "{{task}}\n"
39
- "---\n"
40
- "Use your tools as needed. Before completing the task, plan your actions carefully.\n"
41
- "While completing the task, think step by step. And after completing the task, carefully double check your solution.\n\n"
42
- "If you respond correctly, you will be rewarded with a very high bonus.\n\n"
43
- "Your final_answer MUST be:\n"
44
- "- a number,\n"
45
- "- a short phrase,\n"
46
- "- or a comma-separated list of numbers or strings (no articles or abbreviations).\n\n"
47
- "Only the content passed to the final_answer tool will be preserved—everything else will be discarded."
48
- )
49
- prompts['managed_agent']['task'] = req_instruction
50
- prompts['managed_agent']['report'] = "{{final_answer}}"
51
-
52
- # print(prompts["system_prompt"])
53
- # print(prompts['planning'])
54
- # print(prompts['managed_agent'])
55
- # print(prompts['final_answer'])
56
-
57
- def get_model(
58
- model_id: str = "gpt-4.1-mini",
59
- model_temperature: float = 0.7,
60
- ):
61
- """
62
- Create and return an OpenAIServerModel instance with the specified model ID and temperature.
63
- """
64
- # Load the model
65
- model = OpenAIServerModel(
66
- model_id=model_id,
67
- api_key=os.getenv("OPENAI_API_KEY"),
68
- temperature=model_temperature
69
- )
70
- return model
71
-
72
- def get_agent(
73
- model_id: str = "gpt-4.1-mini",
74
- model_temperature: float = 0.7,
75
- agent_max_steps: int = 15,
76
- ):
77
- """
78
- Create and return a CodeAgent instance with the specified model and tools.
79
- """
80
-
81
- # Defne the agent with the tools
82
- agent = CodeAgent(
83
- tools=[
84
- DuckDuckGoSearchTool(),
85
- VisitWebpageTool(),
86
- WikipediaSearchTool(),
87
- GetTaskFileTool(),
88
- SpeechToTextTool(),
89
- LoadXlsxFileTool(),
90
- LoadTextFileTool(),
91
- FinalAnswerTool(),
92
- ],
93
- model=get_model(
94
- model_id=model_id,
95
- model_temperature=model_temperature,
96
- ),
97
- prompt_templates=prompts,
98
- max_steps=agent_max_steps,
99
- additional_authorized_imports = ["pandas"],
100
- name="GAIAAgent",
101
- )
 
 
 
 
 
 
 
 
 
 
102
  return agent
 
1
+ # Libs
2
+ import os
3
+ import yaml
4
+ import importlib
5
+
6
+ from smolagents import (
7
+ CodeAgent,
8
+ DuckDuckGoSearchTool,
9
+ VisitWebpageTool,
10
+ WikipediaSearchTool,
11
+ OpenAIServerModel,
12
+ SpeechToTextTool,
13
+ FinalAnswerTool,
14
+ )
15
+
16
+ # Local
17
+ from tools import GetTaskFileTool, LoadXlsxFileTool, LoadTextFileTool
18
+
19
+ sys_instruction = (
20
+ "You are a general AI assistant. Answer each question by reporting your thoughts, "
21
+ "then submit ONLY a concise text using the 'final_answer' tool. "
22
+ "Final answer MUST be a number, a few words, or a comma-separated list of numbers and/or strings. "
23
+ "For numbers, avoid commas and units unless specified. For strings, avoid articles and abbreviations, "
24
+ "and write digits in full unless stated otherwise. Apply these rules for list elements as well."
25
+ )
26
+
27
+ prompts = yaml.safe_load(
28
+ importlib.resources.files("smolagents.prompts").joinpath("code_agent.yaml").read_text()
29
+ )
30
+ prompts["system_prompt"] = sys_instruction + prompts["system_prompt"]
31
+
32
+ # req_instruction = (
33
+ req_instruction = (
34
+ "You are an expert and helpful agent named {{name}}.\n"
35
+ "A valued client has assigned you the following task:\n"
36
+ "---\n"
37
+ "Task:\n"
38
+ "{{task}}\n"
39
+ "---\n"
40
+ "Use your tools as needed. Before completing the task, plan your actions carefully.\n"
41
+ "While completing the task, think step by step. And after completing the task, carefully double check your solution.\n\n"
42
+ "If you respond correctly, you will be rewarded with a very high bonus.\n\n"
43
+ "Your final_answer MUST be:\n"
44
+ "- a number,\n"
45
+ "- a short phrase,\n"
46
+ "- or a comma-separated list of numbers or strings (no articles or abbreviations).\n\n"
47
+ "Only the content passed to the final_answer tool will be preserved—everything else will be discarded."
48
+ )
49
+ prompts['managed_agent']['task'] = req_instruction
50
+ prompts['managed_agent']['report'] = "{{final_answer}}"
51
+
52
+ # print(prompts["system_prompt"])
53
+ # print(prompts['planning'])
54
+ # print(prompts['managed_agent'])
55
+ # print(prompts['final_answer'])
56
+
57
+ def get_model(
58
+ model_id: str = "gpt-4.1-mini",
59
+ model_temperature: float = 0.7,
60
+ ):
61
+ """
62
+ Create and return an OpenAIServerModel instance with the specified model ID and temperature.
63
+ """
64
+ # Load the model
65
+ if "gpt" in model_id:
66
+ model = OpenAIServerModel(
67
+ model_id=model_id,
68
+ api_key=os.getenv("OPENAI_API_KEY"),
69
+ temperature=model_temperature
70
+ )
71
+ elif "gemini" in model_id:
72
+ model = OpenAIServerModel(
73
+ model_id=model_id,
74
+ api_key=os.getenv("GOOGLEAI_API_KEY"),
75
+ api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
76
+ temperature=model_temperature
77
+ )
78
+ else:
79
+ raise ValueError(f"Unknown model_id: {model_id}. Supported models are gpt and gemini.")
80
+ return model
81
+
82
+ def get_agent(
83
+ model_id: str = "gpt-4.1-mini",
84
+ model_temperature: float = 0.7,
85
+ agent_max_steps: int = 15,
86
+ ):
87
+ """
88
+ Create and return a CodeAgent instance with the specified model and tools.
89
+ """
90
+
91
+ # Defne the agent with the tools
92
+ agent = CodeAgent(
93
+ tools=[
94
+ DuckDuckGoSearchTool(),
95
+ VisitWebpageTool(),
96
+ WikipediaSearchTool(),
97
+ GetTaskFileTool(),
98
+ SpeechToTextTool(),
99
+ LoadXlsxFileTool(),
100
+ LoadTextFileTool(),
101
+ FinalAnswerTool(),
102
+ ],
103
+ model=get_model(
104
+ model_id=model_id,
105
+ model_temperature=model_temperature,
106
+ ),
107
+ prompt_templates=prompts,
108
+ max_steps=agent_max_steps,
109
+ additional_authorized_imports = ["pandas"],
110
+ name="GAIAAgent",
111
+ )
112
  return agent