Smolagents a Smol Library to build great Agents

Are you waiting for the future or creating it?
Be a part of Gen AI Launch Pad 2024 and take charge of what’s next. Act today for a better tomorrow.
Introduction
smolagents is a lightweight library for constructing and running agents with just a few lines of code. It emphasizes simplicity, security, and versatility by providing:
- Code Agents: Agents that execute actions through code.
- ToolCalling Agents: Agents that output actions as JSON or text.
- Integration with multiple language models (LLMs), including OpenAI, Anthropic, and Hugging Face.
In this blog, we will:
- Explore the key features of smolagents.
- Analyze various examples, including building a search tool, creating custom tools, and running a Gradio interface.
- Offer real-world applications and resources to deepen your knowledge.
Detailed Explanation
1. Setting Up smolagents
Before diving into examples, we start by installing the required libraries and configuring API keys. Here's the setup process:
Code Snippet
!pip install smolagents datasets langchain langchain_community rank_bm25 import os from google.colab import userdata os.environ["OPENROUTER_API_KEY"] = userdata.get('OPENROUTER_API_KEY') os.environ["ANTHROPIC_API_KEY"] = userdata.get('ANTHROPIC_API_KEY') os.environ['HF_TOKEN'] = userdata.get('HF_TOKEN')
Explanation
- Install required dependencies for smolagents and its integrations.
- Set up environment variables to manage API keys securely.
Expected Output
No visual output is expected from this block. Ensure dependencies are installed and keys are correctly configured.
Real-World Application
This setup is critical for leveraging external APIs and tools in smolagents, enabling secure and seamless communication with LLMs.
2. Basic Usage of Code Agents
A CodeAgent executes Python code as part of its reasoning process. Below is an example using a DuckDuckGo search tool and a Hugging Face model.
Code Snippet
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel()) agent.run("What is the cube root of 27?")
Explanation
- CodeAgent: The main agent class for executing Python code.
- DuckDuckGoSearchTool: A tool for performing web searches.
- HfApiModel: A wrapper for Hugging Face models.
Expected Output
Out - Final answer: 3.0
- The agent calculates the cube root of 27 using Python code and provides the correct answer.
Real-World Application
CodeAgents are ideal for automating tasks that require computation or integration with external tools, such as data analysis or web scraping.
3. Custom Tools
You can extend smolagents by creating custom tools. Below is an example of a tool to fetch the most downloaded model for a given task on the Hugging Face Hub.
Code Snippet
from smolagents import Tool class HFModelDownloadsTool(Tool): name = "model_download_counter" description = """ This tool returns the most downloaded model of a given task on the Hugging Face Hub. It returns the name of the checkpoint.""" inputs = {"task": {"type": "string", "description": "The task category."}} output_type = "string" def forward(self, task: str): from huggingface_hub import list_models model = next(iter(list_models(filter=task, sort="downloads", direction=-1))) return model.id model_downloads_tool = HFModelDownloadsTool() model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct") agent = CodeAgent(tools=[HFModelDownloadsTool()], model=model) agent.run("What is the most downloaded model for text classification?")
Explanation
- HFModelDownloadsTool: Fetches the most popular model for a given task.
- list_models: A Hugging Face function to search and sort models.
Expected Output
The name of the most downloaded model for the specified task.
Real-World Application
Custom tools allow you to extend smolagents for domain-specific tasks, such as retrieving information from APIs or databases.
4. Gradio User Interface
A simple Gradio interface can be created for the agent, enabling an interactive user experience.
Code Snippet
from smolagents import GradioUI ui = GradioUI(agent) ui.launch()
Explanation
- GradioUI: Provides an easy-to-use graphical interface for interacting with agents.
- ui.launch(): Launches the interface in a web browser.
Expected Output
A Gradio interface with an input box for user queries and an output area for agent responses.
Real-World Application
Use Gradio to create accessible demos or deploy interactive tools for end-users.
5. Retrieval-Augmented Generation (RAG)
Combine retrieval techniques with language models for information-heavy tasks. Below is an example using a custom retriever tool.
Code Snippet
import datasets from langchain.docstore.document import Document from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain_community.retrievers import BM25Retriever knowledge_base = datasets.load_dataset("m-ric/huggingface_doc", split="train") knowledge_base = knowledge_base.filter(lambda row: row["source"].startswith("huggingface/transformers")) source_docs = [ Document(page_content=doc["text"], metadata={"source": doc["source"].split("/")[1]}) for doc in knowledge_base ] text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50) docs_processed = text_splitter.split_documents(source_docs) class RetrieverTool(Tool): name = "retriever" description = "Retrieves relevant parts of the Transformers documentation." inputs = {"query": {"type": "string", "description": "Search query."}} output_type = "string" def __init__(self, docs, **kwargs): super().__init__(**kwargs) self.retriever = BM25Retriever.from_documents(docs, k=10) def forward(self, query: str) -> str: docs = self.retriever.invoke(query) return "\n".join([f"Document {i}: {doc.page_content}" for i, doc in enumerate(docs)]) retriever_tool = RetrieverTool(docs_processed) agent = CodeAgent(tools=[retriever_tool], model=model) agent_output = agent.run("Which is slower in Transformers training, the forward or backward pass?") print(agent_output)
Explanation
- BM25Retriever: Performs semantic search on a preprocessed knowledge base.
- RecursiveCharacterTextSplitter: Splits documents into manageable chunks.
Expected Output
Relevant documents from the Transformers documentation, highlighting differences between the forward and backward passes.
Real-World Application
RAG is useful for tasks requiring precise answers from large knowledge bases, such as technical documentation or academic research.
Conclusion
The smolagents library simplifies the creation of intelligent agents by combining modularity with the power of LLMs. From basic computation to complex retrieval tasks, smolagents empowers developers to integrate AI into various applications effortlessly.
Key Takeaways
- Smolagents makes building agents intuitive and efficient.
- It supports various LLMs and custom tools for diverse use cases.
- Features like Gradio integration and RAG enable real-world applications across industries.
Next Steps
- Explore the smolagents GitHub repository to dive deeper into its capabilities.
- Experiment with advanced use cases, such as multi-agent collaboration or API integrations.
Resources
- Hugging Face Transformers Documentation
- Gradio Official Website
- LangChain Documentation
- smolagents GitHub Repository
- smolagents Build Fast With AI NoteBook
---------------------------------
Stay Updated:- Follow Build Fast with AI pages for all the latest AI updates and resources.
Experts predict 2025 will be the defining year for Gen AI implementation.Want to be ahead of the curve?
Join Build Fast with AI’s Gen AI Launch Pad 2025 - your accelerated path to mastering AI tools and building revolutionary applications.