AI Agents with Hugging Face smolagents
Adel Nehme
VP of AI Curriculum, DataCamp


from smolagents import tool
import pandas as pd
@tool
def check_inventory(product_name: str) -> int:
    """
    Check the available quantity of a product in the inventory CSV.
    Args:
        product_name (str): The name of the product to look up.
    Returns:
        int: The quantity in stock. Returns 0 if the product is not found.
    """
    df = pd.read_csv("store_inventory.csv")
    match = df[df["product_name"] == product_name]
    stock_quantity = int(match.iloc[0]["quantity"]) if not match.empty else 0
    return stock_quantity

Do we have any t-shirts in stock?

from smolagents import CodeAgent
agent = CodeAgent(
    tools=[check_inventory], # Add custom tool
    model=InferenceClientModel(),
    additional_authorized_imports=["pandas"]  # Allow external package
)
agent.run("Do we have any large t-shirts in stock?")
Yes, we have 8 large t-shirts in stock.

AI Agents with Hugging Face smolagents