Introduction to Hugging Face smolagents

AI Agents with Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

AI Agents with Hugging Face smolagents

AI Agents with Hugging Face smolagents

AI Agents with Hugging Face smolagents

AI Agents with Hugging Face smolagents

AI Agents with Hugging Face smolagents

AI agent definition

AI Agents with Hugging Face smolagents

From Chatbots to Agents

Chatbots:

  • Respond with natural language
  • Passive and reactive

Agents:

  • Can take actions (web search, read files, analyze data, etc.)
  • Actively reason toward a goal

Agents follow a cycle:

AI Agents with Hugging Face smolagents

Example: Competitor Pricing Research

All from a single prompt!

AI Agents with Hugging Face smolagents

What Is smolagents?

  • Lightweight Python framework
  • Developed by Hugging Face

Supports two types of agents:

  • ToolCallingAgent: Uses structured function calls
  • CodeAgent: Writes and runs Python code
AI Agents with Hugging Face smolagents

How Function-Calling Works

Action 1: {"tool": "search_company", "company": "Competitor A"}
Action 2: {"tool": "get_pricing", "company": "Competitor A", "plan": "Basic"}
Action 3: {"tool": "get_pricing", "company": "Competitor A", "plan": "Pro"}
Action 4: {"tool": "search_company", "company": "Competitor B"}
Action 5: {"tool": "get_pricing", "company": "Competitor B", "plan": "Basic"}
AI Agents with Hugging Face smolagents

How Code Agents Work

competitors = ["Competitor A", "Competitor B", "Competitor C"]
pricing_data = {}

for company in competitors:
    company_info = search_company(company)
    plans = extract_pricing_plans(company_info)
    pricing_data[company] = plans

most_affordable_option = min(pricing_data, 
                      key=lambda x: pricing_data[x]['basic_plan'])
AI Agents with Hugging Face smolagents

The Code Agent Flow

Research shows ~20% higher success rate than function-calling methods.

1 https://huggingface.co/papers/2402.01030
AI Agents with Hugging Face smolagents

Let's practice!

AI Agents with Hugging Face smolagents

Preparing Video For Download...