Artificial Intelligence (AI) agents are revolutionizing how we interact with data, perform tasks, and make decisions. These intelligent tools use Large Language Models (LLMs) to automate workflows. In this article, I’ll explore the fundamentals of AI agents, focusing on a Hugging Face library, smolagents, to create a simple agent. This agent will retrieve a list of universities and their websites using an API.
I also highly recommend the HuggingFace Free AI Agents course.
What is Smolagents?
Smolagents is a lightweight library designed to simplify AI agents’ creation. It prioritizes simplicity and functionality, letting developers build powerful agents with minimal code. According to the smolagents team:
- Simplicity: The library is composed of almost one thousand lines of code.
- Code Agents: Smolagents enables execution of code-based actions using sandboxed environments.
- Hub Integrations: Developers can share and load tools directly via Hugging Face’s Hub.
- LLM Support: Smolagents supports Hugging Face models, OpenAI’s GPT, Anthropic’s Claude, and more via LiteLLM integration.
In this blog, I’ll use smolagents to create an agent that fetches university data using an API.
Step-by-Step Guide to Building Your University Search Agent
Let’s break down the process of creating a simple smolagents-based AI agent.
1. Installing Required Libraries
To get started, install the necessary dependencies:
!pip install -q huggingface_hub
!pip install smolagents
!pip install requests
2. Defining the API Search Tool
Smolagents uses tools that are functions equipped with type hints and docstrings (follow the link to learn more about type hints and docstrings). Here’s our search_university
tool:
from smolagents import tool
import requests
@tool
def search_university(country: str, number: int) -> list:
"""
Search for universities in a country using API.
Args:
country: The country name where we want to search for universities.
number: The number of universities to return.
Returns:
A list of universities and their details, including their websites.
"""
try:
response = requests.get(f'http://universities.hipolabs.com/search?country={country}')
response.raise_for_status()
api_response = response.json()[:number]
universities = [
{"name": uni.get("name"), "web_pages": uni.get("web_pages")}
for uni in api_response
]
return universities
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return []
Explanation:
- Decorator (@tool): Converts the function into a tool that the agent can use.
- Input Parameters:
country
: Specifies the country to search for universities.number
: Limits the number of universities retrieved.
- Logic: Sends an HTTP GET request to the API, parses the JSON response, and extracts university names and websites.
3. Fetching Data
To test the tool, call it directly:
universities = search_university('Italy', 5)
print(universities)
This will return a list of 5 universities in Italy with their websites.
4. Building the Agent
Now, let’s create our smolagents agent using the CodeAgent class.
from smolagents import CodeAgent, HfApiModel
agent = CodeAgent(
tools=[search_university],
model=HfApiModel(),
max_steps=5,
verbosity_level=2
)
response = agent.run("Give me a list of some universities in Italy")
Explanation:
- Tools: The agent is equipped with the
search_university
tool. - Model:
HfApiModel
uses Hugging Face’s inference API to process queries. You can change the default model (QWEN) by assigning another model_id. - Agent Execution: The
run
method handles queries like “Find universities in Italy.”
5. Observing Results
The agent will output university names and websites based on the query.
[
{
"name":"Pontificia Università S. Tommaso",
"web_pages":[
"http://www.angelicum.org/"
]
},
{
"name":"Pontificio Ateneo Antonianum",
"web_pages":[
"http://www.antonianum.ofm.org/"
]
},
{
"name":"University of Modena",
"web_pages":[
"http://www.casa.unimo.it/"
]
},
{
"name":"Abdus Salam international centre for theoretical physics",
"web_pages":[
"http://www.ictp.trieste.it/"
]
},
{
"name":"Institute for Advanced Studies Lucca",
"web_pages":[
"http://www.imtlucca.it/"
]
}
]
Conclusion
With smolagents, building AI agents becomes remarkably accessible. Whether you’re searching for something or automating other workflows, this library makes it easy to craft efficient, code-driven solutions.
By using tools like smolagents and Hugging Face’s resources, you can integrate AI seamlessly into projects, boosting your productivity. HuggingFace offers a free course on building AI Agents which is a gem!!! I highly recommend it!