Smolagents is a Huggingface library designed to simplify the creation of agentic workflows for large language models (LLMs). By enabling LLMs to access external tools, manage dynamic workflows, and interact seamlessly with real-world data, Smolagents unlocks the potential of AI to handle tasks that require adaptability and decision-making. This article explores the concept of agents, the unique features of Smolagents, and how it bridges the gap between simplicity and functionality.
Table of Content
- What are Agents?
- When to Use Agents
- Key Features of Smolagents
- Building an Agent with Smolagents
- How Smolagents Compares
Let’s begin by understanding what agents are.
What Are Agents?
Agents enable LLMs to interact with the external world by accessing tools like APIs and search engines. They facilitate dynamic problem-solving through multi-step reasoning and workflows while controlling program execution based on observations and logic. This capability allows LLMs to handle complex tasks, retrieve information, and adapt intelligently to diverse scenarios in real-time.
When to Use Agents
Agents are ideal when workflows cannot be pre-determined or require flexibility. For example:
Deterministic Workflow (No Agent Needed): Pre-defined user paths like FAQs or booking systems.
Dynamic Workflow (Agent Required): Handling complex, multi-faceted queries, such as planning personalized travel itineraries based on variable user constraints.
In cases where deterministic systems fall short, agents provide the adaptability needed for real-world problem-solving.
Key Features of Smolagents
Smolagents focus on simplicity and flexibility while supporting advanced use cases.
Why Choose Smolagents?
Minimalistic Design: Built with ease of use in mind, it reduces abstraction complexity.
Code-First Agents: Supports writing actions in Python code for better composability, object management, and generality.
Secure Execution: Runs code in sandboxed environments, ensuring safe and isolated execution.
Multi-Model Compatibility: Works with open-source models from Hugging Face, as well as proprietary APIs like OpenAI and Anthropic.
Tool Hub Integration: Share and access tools seamlessly via Hugging Face Hub.
Building an Agent with Smolagents
Creating an agent with Smolagents requires:
Tools: Functions that interact with external systems.
Model: An LLM that powers the agent.
Step by Step Implementation
Step 1 : Install the Required Library
!pip install smolagents
Step 2: Let’s create a custom tool to identify the most downloaded model on Hugging Face
from huggingface_hub import list_models
task = "text-classification"
most_downloaded_model = next(iter(list_models(filter=task, sort="downloads", direction=-1)))
print(most_downloaded_model.id)
Output
cross-encoder/ms-marco-MiniLM-L-6-v2
Step 3: Define a tool for model downloads
Now, let’s automate the process of fetching the most downloaded model for any given task.
from smolagents import tool
@tool
def model_download_tool(task: str) -> str:
"""
This is a tool that returns the most downloaded model of a given task on the Hugging Face Hub.
It returns the name of the checkpoint.
Args:
task: The task for which to get the download count.
"""
most_downloaded_model = next(iter(list_models(filter=task, sort="downloads", direction=-1)))
return most_downloaded_model.id
Output
Step 4: Run an agent to utilize the model download tool.
Next, we will create an agent to utilize this tool and provide results interactively.
from smolagents import CodeAgent, HfApiModel
agent = CodeAgent(tools=[model_download_tool], model=HfApiModel())
agent.run(
"Can you give me the name of the model that has the most downloads in the 'text-to-video' task on the Hugging Face Hub?"
)
Step 5: Create a web search tool.
Let’s now develop a more advanced system by integrating a web search tool into a managed agent architecture.
from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, ManagedAgent
model = HfApiModel()
web_agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=model)
managed_web_agent = ManagedAgent(
agent=web_agent,
name="web_search",
description="Runs web searches for you. Give it your query as an argument."
)
manager_agent = CodeAgent(
tools=[], model=model, managed_agents=[managed_web_agent]
)
manager_agent.run("Who is the CEO of Alphabet?")
Output:
How Smolagents Compares
Smolagents provide a streamlined alternative to traditional tool-calling agents by emphasizing code-based actions rather than relying on JSON or text-based instructions. This approach ensures better composability, allowing for reusable and nested structures that enhance workflow efficiency.
It also offers improved object management, enabling seamless handling of outputs like images or files. Leveraging LLMs’ pretraining on code data, It ensures that Python instructions are more intuitive and effective for model interpretation, making them a powerful and practical choice.
Final Words
Smolagents enhance LLMs by enabling dynamic workflows, adaptability, and decision-making for real-world tasks. Its code-first design supports secure execution, multi-model compatibility, and tool integration via Hugging Face Hub, making it ideal for building flexible, powerful AI solutions for automation and complex queries.