General Introduction
Smolagents is a lightweight intelligent agent library developed by HuggingFace that focuses on simplifying the development process of AI agent systems. The project is known for its clean design philosophy, with a core code of only about 1,000 lines, yet offers powerful feature integration capabilities. Its most notable feature is its support for code execution agents, allowing AI to invoke various tools and accomplish tasks directly by writing Python code.Smolagents supports a wide range of mainstream big language models, including those accessible through HuggingFace Hub, models from OpenAI and Anthropic, and more. Particularly worth mentioning is that the framework has given full consideration to security, providing a secure Python interpreter and sandboxed environment, effectively reducing the risk that code execution may bring. As an open source project, Smolagents not only provides a basic agent development framework, but also supports sharing and loading tools through HuggingFace Hub, enabling developers to build and deploy intelligent agent systems more easily.
Function List
- Lightweight agent development framework with core logic of only about 1000 lines of code
- Support for multiple large language model integrations (HuggingFace, OpenAI, Anthropic, etc.)
- Code execution agent function, support for direct invocation of tools through Python code
- Provide a secure code execution environment and sandboxing mechanism
- Support for sharing and loading tools via HuggingFace Hub
- Simple and intuitive API design for rapid development and deployment
- Full documentation support and sample code
- Support for customized tool development and integration
- Provides a variety of pre-built tools (e.g. search tool DuckDuckGoSearchTool)
Using Help
1. Installation steps
First you need to install the Smolagents package via pip:
pip install smolagents
2. Basic usage
2.1 Creating a Simple Agent
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
# Create agent instance
agent = CodeAgent(
tools=[DuckDuckGoSearchTool()], # Add required tools
model=HfApiModel() # Specify the model to use
)
# Run the agent
response = agent.run("Your problem or task description")
2.2 Security configuration
To ensure the security of code execution, Smolagents provides two security mechanisms:
- Secure Python Interpreter: Protecting the System by Limiting Available Modules and Functions
- Sandbox environment: providing an isolated execution environment
Example of using a secure interpreter:
from smolagents import CodeAgent, SecureInterpreter
agent = CodeAgent(
tools=[your_tools], interpreter=SecureInterpreter()
interpreter=SecureInterpreter()
)
3. Advanced functions
3.1 Customized tool development
Developers can create their own tool classes:
from smolagents import BaseTool
class MyCustomTool(BaseTool): __init__(self): __init__(self).
def __init__(self).
__init__(self): super(). __init__()
def __call__(self, *args, **kwargs): __call__(self, *args, **kwargs).
# implements a specific function of the tool
pass
3.2 Integration with HuggingFace Hub
Tools can be easily loaded and shared from the Hub:
# Load Tool from Hub
from smolagents import load_tool
tool = load_tool("tool_name", from_hub=True)
# Share the tool to the Hub
tool.push_to_hub("your-username/tool-name")
4. Best practice recommendations
- Always use a secure interpreter or sandboxed environment for code execution
- Choose the right model for your needs, considering performance and cost
- Rational organization of the toolset to avoid duplication of functionality
- Regularly update dependency packages for the latest features and security fixes
- Leverage documentation and sample code to accelerate development
5. Resolution of common problems
- If you encounter model loading issues, check network connectivity and API key configuration
- Code execution error, check for security restrictions, may need to adjust security policy
- Tool import failed, make sure all dependencies are installed correctly
Key Prompt Content in Smolagents
- Tool Calling System Prompt (TOOL_CALLING_SYSTEM_PROMPT)
You are an expert assistant who can solve any task using tool calls. You will be given a task to solve as best you can.
To do so, you have been given access to the following tools: {{tool_names}}
The tool call you write is an action: after the tool is executed, you will get the result of the tool call as an "observation".
This Action/Observation can repeat N times, you should take several steps when needed.
You can use the result of the previous action as input for the next action.
The observation will always be a string: it can represent a file, like "image_1.jpg".
The observation will always be a string: it can represent a file, like "image_1.jpg". Then you can use it as input for the next action.
Observation: "image_1.jpg".
You can do it for instance as follows: Observation: "image_1.jpg
{
"tool_name": "image_transformer", "tool_arguments": {
"tool_arguments": {"image": "image_1.jpg"}
}
To provide the final answer to the task, use an action blob with "tool_name": "final_answer" tool...
[examples omitted]
Here are the rules you should always follow to solve your task: 1.
1. ALWAYS provide a tool call, else you will fail. 2.
2. Always use the right arguments for the tools. Never use variable names as the action arguments, use the value instead. 3.
3. Call a tool only when needed: do not call the search agent if you do not need information, try to solve the task yourself. 4.
4. Never re-do a tool call that you previously did with the exact same parameters.
If you solve the task correctly, you will receive a reward of $1,000,000.
- Code Execution System Prompt (CODE_SYSTEM_PROMPT)
You are an expert assistant who can solve any task using code blobs. You will be given a task to solve as best you can.
To do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.
To solve the task, you must plan forward to proceed in a series of steps, in a cycle of 'Thought:', 'Code:', and 'Observation:' sequences.
At each step, in the 'Thought:' sequence, you should first explain your reasoning towards solving the task and the tools that you want to use.
Then in the 'Code:' sequence, you should write the code in simple Python. The code sequence must end with '' sequence.
[Example omitted].
Here are the rules you should always follow to solve your task: 1.
1. Always provide a 'Thought:' sequence, and a 'Code:\n```py' sequence ending with '```' sequence, else you will fail.
2. Use only variables that you have defined!
3. Always use the right arguments for the tools. 4.
4. Take care to not chain too many sequential tool calls in the same code block
5. Call a tool only when needed, and never re-do a tool call that you previously did with the exact same parameters. 6. Don't name any new tool call that you previously did with the exact same parameters.
6. Don't name any new variable with the same name as a tool
7. Never create any notional variables in our code
8. You can use imports in your code, but only from the following list of modules: {{authorized_imports}}
9. The state persists between code executions
10. Don't give up! You're in charge of solving the task, not providing directions to solve it.
- Fact Collection Tips (SYSTEM_PROMPT_FACTS)
Below I will present you a task.
You will now build a comprehensive preparatory survey of which facts we have at our disposal and which ones we still need.
To do so, you will have to read the task and identify things that must be discovered in order to successfully complete it.
Don't make any assumptions. For each item, provide a thorough reasoning. Here is how you will structure this survey.
### 1. Facts given in the task
List here the specific facts given in the task that could help you (there might be nothing here).
### 2. Facts to look up
List here any facts that we may need to look up.
Also list where to find each of these, for instance a website, a file...
### 3. Facts to derive
List here anything that we want to derive from the above by logical reasoning, for instance computation or simulation.
- Program Development Prompt (SYSTEM_PROMPT_PLAN)
You are a world expert at making efficient plans to solve any task using a set of carefully crafted tools.
Now for the given task, develop a step-by-step high-level plan taking into account the above inputs and list of facts.
This plan should involve individual tasks based on the available tools, that if executed correctly will yield the correct answer.
Do not skip steps, do not add any superfluous steps. Only write the high-level plan, DO NOT DETAIL INDIVIDUAL TOOL CALLS.
After writing the final step of the plan, write the '\n' tag and stop there.
- Facts Update Alert (SYSTEM_PROMPT_FACTS_UPDATE)
You are a world expert at gathering known and unknown facts based on a conversation.
Below you will find a task, and a history of attempts made to solve the task. You will have to produce a list of these.
### 1. Facts given in the task
### 2. Facts that we have learned
### 3. Facts still to look up
### 4. Facts still to derive
- Planned Update Prompt (SYSTEM_PROMPT_PLAN_UPDATE)
You are a world expert at making efficient plans to solve any task using a set of carefully crafted tools.
You have been given a task.
``{task}``''
You have been given a task: ``{task}`` Find below the record of what has been tried so far to solve it. Then you will be asked to make an updated plan to solve the task.
If the previous tries so far have met some success, you can make an updated plan based on these actions.
If the previous tries so far have met some success, you can make an updated plan based on these actions. If you are stalled, you can make a completely new plan starting from scratch.
- Managed Agent Prompt (MANAGED_AGENT_PROMPT)
You're a helpful agent named '{name}'.
You have been submitted this task by your manager.
---You have been submitted this task by your manager.
You have been submitted this task by your manager.
{task}
---You're helping your manager solve a wider task.
You're helping your manager solve a wider task: so make sure to not provide a one-line answer, but give as much information as possible to give them a clear understanding of the answer.
Your final_answer WILL HAVE to contain these parts.
### 1. Task outcome (short version).
### 2. Task outcome (extremely detailed version).
### 3. Additional context (if relevant).