Hugging Face smolagents

How to use the SmolagentsInstrumentor to trace smolagents by Hugging Face

smolagents is a minimalist AI agent framework developed by Hugging Face, designed to simplify the creation and deployment of powerful agents with just a few lines of code. It focuses on simplicity and efficiency, making it easy for developers to leverage large language models (LLMs) for various applications.

Install

pip install openinference-instrumentation-smolagents smolagents arize-otel

Setup

Add your HF_TOKEN as an environment variable:

os.environ["HF_TOKEN"] = "<your_hf_token_value>"

Next, register a tracer provider to send traces to the Arize platform.

from arize.otel import register

# Setup OTel via our convenience function
tracer_provider = register(
    space_id = "your-space-id", # in app space settings page
    api_key = "your-api-key", # in app space settings page
    project_name = "your-project-name", # name this to whatever you would like
)

Finally, use the smolagents autoinstrumentor to automatically track and visualize every step and call made by your agent.

from openinference.instrumentation.smolagents import SmolagentsInstrumentor

SmolagentsInstrumentor().instrument(tracer_provider=tracer_provider)

Create & Run an Agent

from smolagents import (
   CodeAgent,
   DuckDuckGoSearchTool,
   VisitWebpageTool,
   HfApiModel,
)

hf_model = HfApiModel()

agent = CodeAgent(
    tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
    model=hf_model,
    add_base_tools=True
)

agent.run("fetch the share price of google from 2020 to 2024, and create a line graph from it?")

Observe

Now that you have tracing setup, all invocations and steps of your Agent will be sent to the Arize platform for observability and evaluation.

Last updated

Was this helpful?