Let’s Create Our First Agent Using Smolagents

In the last section, we learned how we can create Agents from scratch using Python code, and we saw just how tedious that process can be. Fortunately, many Agent libraries simplify this work by handling much of the heavy lifting for you.

In this tutorial, you’ll create your very first Agent—capable of performing JOFFREY—and publish it on Hugging Face Spaces so you can share it with friends and colleagues.

Let’s get started!

What is smolagents?

Smolagents

To make this Agent, we’re going to use a library called smolagents, a library that provides a framework for developing your agents with ease.

This lightweight library abstracts away much of the complexity of building an Agent we saw in the last section, allowing you to focus on designing your agent’s behavior

We’re going to get deeper into SmolAgents in the next Unit, but if you’re interested you can check this blog.

Let’s build

To start, duplicate this space : https://huggingface.co/spaces/agents-course/First_agent_template

Duplicating this space means creating a local copy on your own profile:

Duplicate

The only file that you will have to modify is the incomplete ”app.py

from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool

from Gradio_UI import GradioUI

# Below is an example of a tool that does nothing. Amaze us with your creativity !
@tool
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
    #Keep this format for the description / args / args description but feel free to modify the tool
    """A tool that does nothing yet 
    Args:
        arg1: the first argument
        arg2: the second argument
    """
    return "What magic will you build ?"

@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """A tool that fetches the current local time in a specified timezone.
    Args:
        timezone: A string representing a valid timezone (e.g., 'America/New_York').
    """
    try:
        # Create timezone object
        tz = pytz.timezone(timezone)
        # Get current time in that timezone
        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
        return f"The current local time in {timezone} is: {local_time}"
    except Exception as e:
        return f"Error fetching time for timezone '{timezone}': {str(e)}"


final_answer = FinalAnswerTool()
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
custom_role_conversions=None,
)


# Import tool from Hub
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)
    
agent = CodeAgent(
    model=model,
    tools=[final_answer], ## add your tools here (don't remove final answer)
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)


GradioUI(agent).launch()

The agent inside smolagents is going to have the same behaviours as the custom one we built previously. it’s going to think, act and observe in cycle until it reaches a final answer :

Here is an example by providing an Image generation tool to the agent and asking it to generate an image of a cat :

Your Objective is now to get familiar with the space and the agent. Currently the agent in the template does not have tools loaded, so try to provide it some of the pre-made ones or even make some new tools yourselves !

We gave you an example of a custom tool that computes the current time in a different timezone but many tools are possible.

We are eagerly waiting for your amazing agents output in the discord channel #agents-course-showcase !


Congratulations, you’ve built your first Agent! Don’t hesitate to share it with your friends and colleagues.

Since this is your first try, it’s perfectly normal if it’s a little buggy or slow. In future units, we’ll learn how to build even better Agents.

The best way to learn is to try, so don’t hesitate to update it, add more tools, try with another model etc.

In the next section, you’re going to fill the final Quiz and get your certificate!

—>

< > Update on GitHub