Introduction to Hugging Face smolagents
1. Introduction to Hugging Face smolagents
Hi there! And welcome to the course, I'm thrilled to be your host, as we explore the Hugging Face smolagents framework together!2. Course Learning Outcomes
By the end of this course, you'll be able to:3. Course Learning Outcomes
Understand how code agents work and why they're powerful,4. Course Learning Outcomes
build agents that solve real-world tasks using Python,5. Course Learning Outcomes
create custom tools to extend what agents can do,6. Course Learning Outcomes
and design multi-agent workflows to solve more complex problems.7. What is an AI Agent?
But before we dive in, let's clarify what an agent actually is. An AI agent is a system that uses a large language model to interact with its environment to achieve a user-defined objective.8. From Chatbots to Agents
Unlike typical chatbots, which focus on responding to user input with natural language and are generally passive, agents can also take actions. For example, they can search the web, read files, analyze data, and actively reason towards a goal. Agents follow a thought-action-observation cycle, reasoning step-by-step until the task is complete.9. Example: Competitor Pricing Research
For example, say you wanted to do competitor pricing research. An AI agent could search company websites, extract pricing info, and analyze and compare the data to generate a summary or report. And all of that could happen from a single prompt!10. What Is smolagents?
That's where smolagents comes in. smolagents is a lightweight Python framework developed by Hugging Face for building intelligent agents. It supports two types of agents: ToolCallingAgent, which uses structured function calls, and CodeAgent, which dynamically writes and runs Python code to solve tasks from start to finish. In this course, we'll focus on Code Agents, since they're smolagents' specialty. That said, it's important to understand both agent types. Let's take a closer look at how they work.11. How Function-Calling Works
Most traditional agent frameworks, including smolagents' ToolCallingAgent, use function-calling. You define a set of tools in advance, and the agent selects one at each step by generating a structured JSON call. For our competitor research example, a function-calling agent would approach actions like filling out individual forms. Notice how each action is a separate JSON call: first searching for Competitor A, then getting their basic plan pricing, then their pro plan pricing, then searching for Competitor B, and so on. This requires many individual steps.12. How Code Agents Work
A code agent, on the other hand, would write a custom analysis function in Python. Here, the agent: defines a list called competitors with three company names, creates an empty dictionary called pricing_data to store the results, uses a for loop to iterate through each company, search for their information, extract their pricing plans, and store everything in the dictionary. Finally, it finds the most affordable option using the min() function.13. The Code Agent Flow
So instead of calling predefined functions, code agents write Python code as they need. Meaning, they are able to generate and execute full scripts to carry out tasks end to end, often combining logic, data processing, and reasoning in one place. The code agent approach is typically faster and more readable than function-calling. Research shows it has around 20% higher success rate than traditional function-calling methods.14. Let's practice!
Throughout the course, we'll explore step by step how to build code agents with smolagents, but for now, let's see them in action!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.