Mastering SmolAgents: Building AI Agents with Tools
NBD Lite #41 - Transform Ideas into AI Agents with SmolAgents Tools
This article continues the previous one on developing Intelligent Agents with Smolagents. If you haven’t read it yet, check it out below.
As introduced in the previous article, Smolagents is an agent framework that simplifies creating agents with just a few lines of code.
It’s a powerful yet lightweight tool, allowing us to run various open-source LLMs as agents tailored to specific use cases.
To build our skills, we’ll explore customizing agents in Smolagents and using tools to enhance our workflows.
Let’s get into it.
Building Agent with Smolagents
Smolagents by Hugging Face offers a straightforward approach to building agents. A minimal agent setup requires only an LLM and the tools you want to use—everything else is optional.
Once your agent is set up, you can run it. It will utilize Python code to execute tasks efficiently.
Let’s start by installing the library.
pip install smolagents huggingface_hub
Don't forget to get your Hugging Face access token and use the code below to log in.
from huggingface_hub import login
login('YOUR-API-KEY')
Next, we’ll build a minimal agent to assist with our tasks. As mentioned earlier, all you need to run a Smolagents agent is an LLM and the necessary tools.
In the code below, we’ll run the agent using Mistral-7B-Instruct-v0.3 as an example. Feel free to use any other model available on the Hugging Face Hub.
from smolagents import CodeAgent, HfApiModel
model_id = "mistralai/Mistral-7B-Instruct-v0.3"
model = HfApiModel(model_id=model_id)
agent = CodeAgent(tools=[], model=model_id)
agent.run(
"What is the 15th prime number?",
)
Final answer: The 15th prime number is 47. To solve this, I would typically generate a list of prime numbers using
a helper function and then choose the 15th element from that list. However, in this case, it seems like I don't
have access to the necessary tools to perform that operation, so I will simply state the 15th prime number
directly.
As you can see from the result above, the agent performs the task quite well. However, the output indicates it lacks access to the necessary tools, so the model provides a direct response instead.
In this case, we can add a tool to help the model execute its task more effectively.
For example, using the code below, let’s create a custom tool to check whether a number is prime.
from transformers import tool
@tool
def is_prime(n: int) -> bool:
"""
Check if a number is a prime number.
Args:
n: The number to check.
Returns:
True if n is a prime number, False otherwise.
"""
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
When defining the code for a tool, include the expected output and a description of how the tool works. This information is essential for the agent to understand and use the tool effectively.
Once the tool is ready, we can pass it to the agents using the code below.
agent = CodeAgent(tools=[is_prime], model=model)
agent.run("Check if the number 17 is a prime number and provide the result in cheerful way.")
The output in the image above results from the agent executing the tool we created earlier. The agent clearly understood the task and utilized the tool.
As additional information, you can set the add_base_tools
parameter as True to allow the agents access to the essential tool.
agent = CodeAgent(tools=[],add_base_tools=True, model=model)
Smolagents comes with three built-in tools:
DuckDuckGo Web Search
Python Code Interpreter (available only for
ToolCallingAgent
as CodeAgent already has by default)Transcriber (Speech-to-Text)
These are the essential tools, but you’ll need to develop custom tools to fit your specific use cases.
Additionally, since Smolagents' execution is based on code actions, the Python interpreter doesn’t allow imports outside the predefined safe list. If you want agents to access additional libraries, you must manually specify which Python imports are permitted.
For example, here is how we pass additional Python libraries that allow for Smolagents agents.
agent = CodeAgent(tools=[], model=model, additional_authorized_imports=['requests', 'bs4'])
agent.run("Could you get me the title of the page at url 'https://www.nb-data.com/p/getting-started-with-huggingface'?. Don't use tools")
You can see that the agent uses the requests
and bs4
libraries to complete the task. If these libraries are removed from the authorized list, the task will likely fail to execute.
That’s all for building agents and using tools with Smolagents. I hope you found it helpful!
Is there anything else you’d like to discuss? Let’s dive into it together!
👇👇👇