Create agent.py
Browse files
agent.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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, AnalyzeImageTool
|
| 18 |
+
|
| 19 |
+
sys_instruction = (
|
| 20 |
+
"You are a general-purpose AI assistant. For each question, first reason through your answer, "
|
| 21 |
+
"then use the 'final_answer' tool to submit ONLY the final output.\n"
|
| 22 |
+
"Your final answer MUST be one of the following:\n"
|
| 23 |
+
"- A number (without commas or units, unless explicitly instructed),\n"
|
| 24 |
+
"- A short phrase,\n"
|
| 25 |
+
"- A comma-separated list of numbers or strings.\n"
|
| 26 |
+
"For strings, avoid articles and abbreviations, and write digits fully unless told otherwise. "
|
| 27 |
+
"Apply these formatting rules consistently to each item in a list."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
prompts = yaml.safe_load(
|
| 31 |
+
importlib.resources.files("smolagents.prompts").joinpath("code_agent.yaml").read_text()
|
| 32 |
+
)
|
| 33 |
+
prompts["system_prompt"] = sys_instruction + prompts["system_prompt"]
|
| 34 |
+
|
| 35 |
+
# req_instruction = (
|
| 36 |
+
req_instruction = (
|
| 37 |
+
"You are a highly capable and autonomous agent named {{name}}, designed to solve complex tasks efficiently.\n"
|
| 38 |
+
"A valued client has assigned you the following task:\n"
|
| 39 |
+
"---\n"
|
| 40 |
+
"Task:\n"
|
| 41 |
+
"{{task}}\n"
|
| 42 |
+
"---\n"
|
| 43 |
+
"To complete this task successfully, follow these steps carefully:\n"
|
| 44 |
+
" 1. Comprehend the task and identify the intended goal.\n"
|
| 45 |
+
" 2. Break the task into clear, logical steps.\n"
|
| 46 |
+
" 3. Select and prepare the tools or resources you need.\n"
|
| 47 |
+
" 4. Set up the required environment or context.\n"
|
| 48 |
+
" 5. Execute each step methodically.\n"
|
| 49 |
+
" 6. Monitor outcomes and identify any deviations.\n"
|
| 50 |
+
" 7. Revise your plan if necessary based on feedback.\n"
|
| 51 |
+
" 8. Maintain internal state and track progress.\n"
|
| 52 |
+
" 9. Verify that the goal has been fully achieved.\n"
|
| 53 |
+
" 10. Present the final result clearly and concisely.\n"
|
| 54 |
+
"If you succeed, you will be rewarded with a significant bonus.\n\n"
|
| 55 |
+
"Your final_answer MUST be:\n"
|
| 56 |
+
"- a number (retain its original type; do not include units),\n"
|
| 57 |
+
"- a concise phrase,\n"
|
| 58 |
+
"- or a comma-separated list of numbers or strings (no articles, no abbreviations).\n\n"
|
| 59 |
+
"Only the content passed to the final_answer tool will be preserved. Any other content will be discarded."
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
prompts['managed_agent']['task'] = req_instruction
|
| 63 |
+
prompts['managed_agent']['report'] = "{{final_answer}}"
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def get_model(
|
| 67 |
+
model_id: str = "gpt-4.1-mini",
|
| 68 |
+
model_temperature: float = 0.7,
|
| 69 |
+
):
|
| 70 |
+
"""
|
| 71 |
+
Create and return an OpenAIServerModel instance with the specified model ID and temperature.
|
| 72 |
+
"""
|
| 73 |
+
# Load the model
|
| 74 |
+
if "gpt" in model_id:
|
| 75 |
+
model = OpenAIServerModel(
|
| 76 |
+
model_id=model_id,
|
| 77 |
+
api_key=os.getenv("OPENAI_API_KEY"),
|
| 78 |
+
temperature=model_temperature
|
| 79 |
+
)
|
| 80 |
+
elif "gemini" in model_id:
|
| 81 |
+
model = OpenAIServerModel(
|
| 82 |
+
model_id=model_id,
|
| 83 |
+
api_key=os.getenv("GOOGLEAI_API_KEY"),
|
| 84 |
+
api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
|
| 85 |
+
temperature=model_temperature
|
| 86 |
+
)
|
| 87 |
+
else:
|
| 88 |
+
raise ValueError(f"Unknown model_id: {model_id}. Supported models are gpt and gemini.")
|
| 89 |
+
return model
|
| 90 |
+
|
| 91 |
+
def get_agent(
|
| 92 |
+
model_id: str = "gpt-4.1-mini",
|
| 93 |
+
model_temperature: float = 0.7,
|
| 94 |
+
agent_max_steps: int = 15,
|
| 95 |
+
):
|
| 96 |
+
"""
|
| 97 |
+
Create and return a CodeAgent instance with the specified model and tools.
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
# Defne the agent with the tools
|
| 101 |
+
agent = CodeAgent(
|
| 102 |
+
tools=[
|
| 103 |
+
DuckDuckGoSearchTool(),
|
| 104 |
+
VisitWebpageTool(),
|
| 105 |
+
WikipediaSearchTool(),
|
| 106 |
+
GetTaskFileTool(),
|
| 107 |
+
SpeechToTextTool(),
|
| 108 |
+
LoadXlsxFileTool(),
|
| 109 |
+
LoadTextFileTool(),
|
| 110 |
+
AnalyzeImageTool(model_id=model_id),
|
| 111 |
+
FinalAnswerTool(),
|
| 112 |
+
],
|
| 113 |
+
model=get_model(
|
| 114 |
+
model_id=model_id,
|
| 115 |
+
model_temperature=model_temperature,
|
| 116 |
+
),
|
| 117 |
+
prompt_templates=prompts,
|
| 118 |
+
max_steps=agent_max_steps,
|
| 119 |
+
additional_authorized_imports = ["pandas"],
|
| 120 |
+
name="GAIAAgent",
|
| 121 |
+
)
|
| 122 |
+
return agent
|
| 123 |
+
|
| 124 |
+
def test():
|
| 125 |
+
"""test tests something. """
|
| 126 |
+
# Test the agent with a simple task
|
| 127 |
+
return True
|