Smolagents

Smolagents is a framework from HuggingFace that allows you to create AI agents with various capabilities. The framework provides a simple way to build agents that can perform tasks like coding, searching the web, and more.

Opik integrates seamlessly with Smolagents through OpenTelemetry, allowing you to trace and monitor your agent executions in detail.

Getting started

First, ensure you have both opik and smolagents packages installed with the telemetry components:

$pip install opik 'smolagents[telemetry,toolkit]'

In addition, you can configure Opik using the opik configure command:

$opik configure

Or programmatically:

1import opik
2opik.configure(use_local=False) # Set to True if using a local Opik server

Setting up OpenTelemetry for SmolAgents

To enable tracing of your SmolAgents execution with Opik, you need to set up OpenTelemetry. First, configure the necessary environment variables:

1import opik
2import os
3
4opik_config = opik.config.get_from_user_inputs()
5
6# Set the OpenTelemetry endpoint
7endpoint = "https://www.comet.com/opik/api/v1/private/otel"
8os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = endpoint
9
10# Set the headers with authorization and project information
11headers = (
12 f"Authorization={opik_config.api_key},"
13 f"projectName={opik_config.project_name},"
14 f"Comet-Workspace={opik_config.workspace}"
15)
16os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = headers

The projectName and Comet-Workspace headers are optional and can be set to Default and default respectively. If you are using the self-hosted version of Opik, you will need to update the endpoint URL. You can learn more about this in the Opik OpenTelemetry documentation.

Next, set up the instrumentation for SmolAgents:

1# Import telemetry components
2from opentelemetry.sdk.trace import TracerProvider
3from opentelemetry.sdk.trace.export import SimpleSpanProcessor
4
5# Import SmolAgents instrumentation
6from openinference.instrumentation.smolagents import (
7 SmolagentsInstrumentor
8)
9
10# Import OTLP exporter
11from opentelemetry.exporter.otlp.proto.http.trace_exporter import (
12 OTLPSpanExporter
13)
14
15# Set up the tracer provider with span processor
16trace_provider = TracerProvider()
17trace_provider.add_span_processor(
18 SimpleSpanProcessor(OTLPSpanExporter())
19)
20
21# Instrument SmolAgents
22SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)

Using SmolAgents with Opik

After setting up the instrumentation, you can use SmolAgents as usual. All agent executions will be automatically traced and sent to Opik:

1from smolagents import CodeAgent, WebSearchTool, OpenAIServerModel
2
3model = OpenAIServerModel(
4 model_id="gpt-4o",
5)
6
7agent = CodeAgent(
8 tools=[WebSearchTool()],
9 model=model,
10 stream_outputs=True
11)
12
13agent.run("How many seconds would it take for a leopard at full speed to run through Pont des Arts?")

With the OpenTelemetry configuration in place, all agent executions, including intermediate steps and tool usage, will be logged in Opik. This allows you to visualize the entire execution flow, examine inputs and outputs, and analyze the performance of your agents.

Features

The Opik integration with SmolAgents provides:

  1. Detailed tracing of agent execution flows
  2. Visualization of tool calls and their results
  3. Tracking of LLM inputs and outputs
  4. Performance metrics for agent executions

Further improvements

If you would like to see us improve this integration, simply open a new feature request on Github.