Getting Started with HuggingFace Smolagents: Build Intelligent Agents in Minutes
NBD Lite #41 - Agents that build actions in code
A few years ago, I foresaw the rise of Agentic AI as a game-changing trend. Autonomous Agents took center stage in my predictions for 2025 Machine Learning breakthroughs. Now, the future has arrived, and we’re living it.
Those unfamiliar with Agents can be simplified as programs where LLM (Large Language Model) outputs control workflows.
By delegating processes to agents, machines can now perform previously impossible tasks that are now achievable through this technology.
While many agent frameworks are available, I want to highlight a simple yet powerful one that was recently launched: smolagents.
What are smolagents, and why should you know about them? Let’s explore it together!
Smolagents
Smolagents is an agent framework recently launched by the Hugging Face team.
Designed as a lightweight library, it simplifies creating agents with just a few lines of code, enabling developers to focus on practicality rather than building systems from scratch.
Some key advantages of using Smolagents include:
Simplicity: Smolagents is designed for ease of use, with minimal code abstraction, making it intuitive for rapid prototyping and deployment.
Code-Centric Agents: By prioritizing code-based agents, Smolagents enables agents to perform actions directly as Python code. This approach often outperforms traditional tool-based agents by requiring fewer steps and achieving higher accuracy in benchmarks.
Compatibility: Developed by the Hugging Face team, Smolagents integrates seamlessly with any LLM and tools available on the Hugging Face Hub. Additionally, it supports other popular models through LiteLLM.
With that in mind, let’s try using the Smolagents.
First, we need to install the package using the following code:
pip install smolagents huggingface_hub
You will need a HuggingFace access token for this tutorial, so don’t forget to acquire them. Once you have them, use the code below to log in to the HuggingFace from your notebook.
from huggingface_hub import login
login('YOUR-HF-API-KEY')
Let’s try building simple agents with Smolagent. We will use the free inference model from HuggingFace and the web searching tool.
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())
agent.run("How can I travel from Jakarta to Moscow, and how long will it take?")
The agents execute the intended tasks using Python code and generate results. For instance, here’s an example of code executed by Smolagents:
Here is the output from my side:
Final answer: To travel from Jakarta to Moscow, you can take a direct flight or a flight with one or more stops,
depending on your travel preferences and available options. Based on the search results, here's the general
information:
### Travel Options:
1. **Direct Flights:**
Direct flights typically take around 11 to 13 hours, excluding layovers and other factors such as airport
transfers, security checks, etc. For instance, some direct flights are listed as taking about 12 hours.
2. **Flights with Stops:**
Flights with one or more stops can take longer, sometimes up to 19 hours, as indicated by some travel results.
### Flight Duration:
From the various search results, the flight duration is estimated to be typically around 11 to 13 hours for direct
flights. However, this duration can vary based on factors such as the specific airline, route taken, and aircraft
model.
### Sample Travel Duration:
- **Direct Flight Example:**
- **Duration:** Approximately 12 hours
- **Website:** Travelmath (https://www.travelmath.com/flying-time/from/Jakarta,+Indonesia/to/Moscow,+Russia)
- **Flight with Stop Example:**
- **Duration:** Approximately 19 hours
- **Website:** Emirates
(https://www.emirates.com/id/english/destinations/cgk/dme/flights-from-jakarta-to-moscow/)
### Steps to Book a Flight:
1. **Choose a Travel Website:** Visit travel websites such as Google Flights, Skyscanner, Trip.com, or Expedia.
2. **Search for Flights:** Enter your travel dates and select "Jakarta" as your departure city and "Moscow" as your
destination.
3. **Compare Prices and Times:** Check the duration, stops, and prices of different flights available.
4. **Book Your Flight:** Once you've found a suitable flight, you can proceed to book it on the chosen website.
### Additional Tips:
- **Check Flight Times:** Ensure you check the departure and arrival times to make sure they fit your schedule.
- **Consider Layovers:** Be aware of layovers and how they might affect your travel experience.
- **Book in Advance:** Booking tickets in advance can often result in cheaper fares.
By following these steps, you should be able to find a suitable flight from Jakarta to Moscow that fits your needs.
The agents can answer questions by searching the web and providing a list of possible answers.
There is still immense potential for using Smolagents by developing custom tools and leveraging various models available. We’ll delve deeper into this in the next edition, so stay tuned!
That’s a brief introduction to Smolagents—I hope you found it enjoyable!
Is there anything else you’d like to discuss? Let’s dive into it together!
👇👇👇