Agents · Hugging Face (original) (raw)

Smolagents is an experimental API which is subject to change at any time. Results returned by the agents can vary as the APIs or underlying models are prone to change.

To learn more about agents and tools make sure to read the introductory guide. This page contains the API docs for the underlying classes.

Agents

Our agents inherit from MultiStepAgent, which means they can act in multiple steps, each step consisting of one thought, then one tool call and execution. Read more in this conceptual guide.

We provide two types of agents, based on the main Agent class.

Both require arguments model and list of tools tools at initialization.

Classes of agents

class smolagents.MultiStepAgent

< source >

( tools: list model: Model prompt_templates: smolagents.agents.PromptTemplates | None = None instructions: str | None = None max_steps: int = 20 add_base_tools: bool = False verbosity_level: LogLevel = <LogLevel.INFO: 1> managed_agents: list | None = None step_callbacks: list[collections.abc.Callable] | dict[typing.Type[smolagents.memory.MemoryStep], collections.abc.Callable | list[collections.abc.Callable]] | None = None planning_interval: int | None = None name: str | None = None description: str | None = None provide_run_summary: bool = False final_answer_checks: list[collections.abc.Callable] | None = None return_full_result: bool = False logger: smolagents.monitoring.AgentLogger | None = None )

Parameters

Agent class that solves the given task step by step, using the ReAct framework: While the objective is not reached, the agent will perform a cycle of action (given by the LLM) and observation (obtained from the environment).

( model_output: str split_token: str )

Parameters

Parse action from the LLM output

from_dict

< source >

( agent_dict: dict **kwargs ) → MultiStepAgent

Parameters

Instance of the agent class.

Create agent from a dictionary representation.

from_folder

< source >

( folder: str | pathlib.Path **kwargs )

Parameters

Loads an agent from a local folder.

from_hub

< source >

( repo_id: str token: str | None = None trust_remote_code: bool = False **kwargs )

Parameters

Loads an agent defined on the Hub.

Loading a tool from the Hub means that you’ll download the tool and execute it locally. ALWAYS inspect the tool you’re downloading before loading it within your runtime, as you would do when installing a package using pip/npm/apt.

To be implemented in child classes

Interrupts the agent execution.

provide_final_answer

< source >

( task: str ) → str

Parameters

Final answer to the task.

Provide the final answer to the task, based on the logs of the agent’s interactions.

push_to_hub

< source >

( repo_id: str commit_message: str = 'Upload agent' private: bool | None = None token: bool | str | None = None create_pr: bool = False )

Parameters

Upload the agent to the Hub.

replay

< source >

( detailed: bool = False )

Parameters

Prints a pretty replay of the agent’s steps.

run

< source >

( task: str stream: bool = False reset: bool = True images: list['PIL.Image.Image'] | None = None additional_args: dict | None = None max_steps: int | None = None return_full_result: bool | None = None )

Parameters

Run the agent for the given task.

Example:

from smolagents import CodeAgent agent = CodeAgent(tools=[]) agent.run("What is the result of 2 power 3.7384?")

save

< source >

( output_dir: str | pathlib.Path relative_path: str | None = None )

Parameters

Saves the relevant code files for your agent. This will copy the code of your agent in output_dir as well as autogenerate:

Perform one step in the ReAct framework: the agent thinks, acts, and observes the result. Returns either None if the step is not final, or the final answer.

to_dict

< source >

( ) → dict

Dictionary representation of the agent.

Convert the agent to a dictionary representation.

Creates a rich tree visualization of the agent’s structure.

write_memory_to_messages

< source >

( summary_mode: bool = False )

Reads past llm_outputs, actions, and observations or errors from the memory into a series of messages that can be used as input to the LLM. Adds a number of keywords (such as PLAN, error, etc) to help the LLM.

class smolagents.CodeAgent

< source >

( tools: list model: Model prompt_templates: smolagents.agents.PromptTemplates | None = None additional_authorized_imports: list[str] | None = None planning_interval: int | None = None executor: PythonExecutor = None executor_type: typing.Literal['local', 'blaxel', 'e2b', 'modal', 'docker', 'wasm'] = 'local' executor_kwargs: dict[str, typing.Any] | None = None max_print_outputs_length: int | None = None stream_outputs: bool = False use_structured_outputs_internally: bool = False code_block_tags: str | tuple[str, str] | None = None **kwargs )

Parameters

In this agent, the tool calls will be formulated by the LLM in code format, then parsed and executed.

Clean up resources used by the agent, such as the remote Python executor.

from_dict

< source >

( agent_dict: dict **kwargs ) → CodeAgent

Parameters

Instance of the CodeAgent class.

Create CodeAgent from a dictionary representation.

class smolagents.ToolCallingAgent

< source >

( tools: list model: Model prompt_templates: smolagents.agents.PromptTemplates | None = None planning_interval: int | None = None stream_outputs: bool = False max_tool_threads: int | None = None **kwargs )

Parameters

This agent uses JSON-like tool calls, using method model.get_tool_call to leverage the LLM engine’s tool calling capabilities.

execute_tool_call

< source >

( tool_name: str arguments: dict[str, str] | str )

Parameters

Execute a tool or managed agent with the provided arguments.

The arguments are replaced with the actual values from the state if they refer to state variables.

process_tool_calls

< source >

( chat_message: ChatMessage memory_step: ActionStep ) → ToolCall | ToolOutput

Parameters

Yields

ToolCall | ToolOutput

Process tool calls from the model output and update agent memory.

stream_to_gradio

smolagents.stream_to_gradio

< source >

( agent task: str task_images: list | None = None reset_agent_memory: bool = False additional_args: dict | None = None )

Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages.

GradioUI

You must have gradio installed to use the UI. Please run pip install 'smolagents[gradio]' if it’s not the case.

class smolagents.GradioUI

< source >

( agent: MultiStepAgent file_upload_folder: str | None = None reset_agent_memory: bool = False )

Parameters

Raises

ModuleNotFoundError

Gradio interface for interacting with a MultiStepAgent.

This class provides a web interface to interact with the agent in real-time, allowing users to submit prompts, upload files, and receive responses in a chat-like format. It uses the modern gradio.ChatInterface component for a native chatbot experience. It can reset the agent’s memory at the start of each interaction if desired. It supports file uploads via multimodal input. This class requires the gradio extra to be installed: pip install 'smolagents[gradio]'.

Example:

from smolagents import CodeAgent, GradioUI, InferenceClientModel

model = InferenceClientModel(model_id="meta-llama/Meta-Llama-3.1-8B-Instruct") agent = CodeAgent(tools=[], model=model) gradio_ui = GradioUI(agent, file_upload_folder="uploads", reset_agent_memory=True) gradio_ui.launch()

launch

< source >

( share: bool = True **kwargs )

Parameters

Launch the Gradio app with the agent interface.

upload_file

< source >

( file file_uploads_log: list allowed_file_types: list | None = None )

Parameters

Handle file upload with validation.

Prompts

Prompt templates for the agent.

class smolagents.PlanningPromptTemplate

< source >

( )

Parameters

Prompt templates for the planning step.

class smolagents.ManagedAgentPromptTemplate

< source >

( )

Parameters

Prompt templates for the managed agent.

class smolagents.FinalAnswerPromptTemplate

< source >

( )

Parameters

Prompt templates for the final answer.

Memory

Smolagents use memory to store information across multiple steps.

class smolagents.AgentMemory

< source >

( system_prompt: str )

Parameters

Memory for the agent, containing the system prompt and all steps taken by the agent.

This class is used to store the agent’s steps, including tasks, actions, and planning steps. It allows for resetting the memory, retrieving succinct or full step information, and replaying the agent’s steps.

Attributes:

Return a full representation of the agent’s steps, including model input messages.

Return a succinct representation of the agent’s steps, excluding model input messages.

replay

< source >

( logger: AgentLogger detailed: bool = False )

Parameters

Prints a pretty replay of the agent’s steps.

Reset the agent’s memory, clearing all steps and keeping the system prompt.

Returns all code actions from the agent’s steps, concatenated as a single script.

Python code executors

Local Python executor

class smolagents.LocalPythonExecutor

< source >

( additional_authorized_imports: list max_print_outputs_length: int | None = None additional_functions: dict[str, collections.abc.Callable] | None = None timeout_seconds: int | None = 30 )

Parameters

Executor of Python code in a local environment.

This executor evaluates Python code with restricted access to imports and built-in functions, making it suitable for running untrusted code. It maintains state between executions, allows for custom tools and functions to be made available to the code, and captures print outputs separately from return values.

Remote Python executors

class smolagents.remote_executors.RemotePythonExecutor

< source >

( additional_imports: list logger )

Execute code, return the result and output, also determining if the result is the final answer.

Send variables to the kernel namespace using pickle.

BlaxelExecutor

class smolagents.BlaxelExecutor

< source >

( additional_imports: list logger sandbox_name: str | None = None image: str = 'blaxel/jupyter-notebook' memory: int = 4096 ttl: str | None = None region: typing.Optional[str] = None )

Parameters

Executes Python code using Blaxel sandboxes.

Blaxel provides fast-launching virtual machines that start from hibernation in under 25ms and scale back to zero after inactivity while maintaining memory state.

Sync wrapper to clean up sandbox and resources.

Ensure cleanup on deletion.

install_packages

< source >

( additional_imports: list )

Helper method to install packages asynchronously.

run_code_raise_errors

< source >

( code: str ) → CodeOutput

Parameters

Code output containing the result, logs, and whether it is the final answer.

Execute Python code in the Blaxel sandbox and return the result.

E2BExecutor

class smolagents.E2BExecutor

< source >

( additional_imports: list logger **kwargs )

Parameters

Executes Python code using E2B.

Clean up the E2B sandbox and resources.

ModalExecutor

class smolagents.ModalExecutor

< source >

( additional_imports: list logger app_name: str = 'smolagent-executor' port: int = 8888 create_kwargs: typing.Optional[dict] = None )

Parameters

Executes Python code using Modal.

Ensure cleanup on deletion.

DockerExecutor

class smolagents.DockerExecutor

< source >

( additional_imports: list logger host: str = '127.0.0.1' port: int = 8888 image_name: str = 'jupyter-kernel' build_new_image: bool = True container_run_kwargs: dict[str, typing.Any] | None = None dockerfile_content: str | None = None )

Executes Python code using Jupyter Kernel Gateway in a Docker container.

Clean up the Docker container and resources.

Ensure cleanup on deletion.

WasmExecutor

class smolagents.WasmExecutor

< source >

( additional_imports: list logger deno_path: str = 'deno' deno_permissions: list[str] | None = None timeout: int = 60 )

Parameters

Remote Python code executor in a sandboxed WebAssembly environment powered by Pyodide and Deno.

This executor combines Deno’s secure runtime with Pyodide’s WebAssembly‑compiled Python interpreter to deliver s trong isolation guarantees while enabling full Python execution.

Clean up resources used by the executor.

Ensure cleanup on deletion.

install_packages

< source >

( additional_imports: list ) → list[str]

Parameters

Installed packages.

Install additional Python packages in the Pyodide environment.

run_code_raise_errors

< source >

( code: str ) → CodeOutput

Parameters

Code output containing the result, logs, and whether it is the final answer.

Execute Python code in the Pyodide environment and return the result.

Update on GitHub