Observability for Smolagents with Opik

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 with built-in support for multiple tools and LLM providers.

Account Setup

Comet provides a hosted version of the Opik platform, simply create an account and grab your API Key.

You can also run the Opik platform locally, see the installation guide for more information.

Getting Started

Installation

To use the Smolagents integration with Opik, you will need to have Smolagents and the required OpenTelemetry packages installed:

$pip install --upgrade opik 'smolagents[telemetry,toolkit]' opentelemetry-sdk opentelemetry-exporter-otlp

Configuring Smolagents

In order to use Smolagents, you will need to configure your LLM provider API keys. For this example, we’ll use OpenAI. You can find or create your API keys in these pages:

You can set them as environment variables:

$export OPENAI_API_KEY="YOUR_API_KEY"

Or set them programmatically:

1import os
2import getpass
3
4if "OPENAI_API_KEY" not in os.environ:
5 os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")

Configuring OpenTelemetry

You will need to set the following environment variables to configure OpenTelemetry to send data to Opik:

If you are using Opik Cloud, you will need to set the following environment variables:

$export OTEL_EXPORTER_OTLP_ENDPOINT=https://www.comet.com/opik/api/v1/private/otel
>export OTEL_EXPORTER_OTLP_HEADERS='Authorization=<your-api-key>,Comet-Workspace=default'

To log the traces to a specific project, you can add the projectName parameter to the OTEL_EXPORTER_OTLP_HEADERS environment variable:

$export OTEL_EXPORTER_OTLP_HEADERS='Authorization=<your-api-key>,Comet-Workspace=default,projectName=<your-project-name>'

You can also update the Comet-Workspace parameter to a different value if you would like to log the data to a different workspace.

Using Opik with Smolagents

To track your Smolagents applications, you will need to configure OpenTelemetry to instrument the framework:

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 SmolagentsInstrumentor
7
8# Import OTLP exporter
9from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
10
11# Set up the tracer provider with span processor
12trace_provider = TracerProvider()
13trace_provider.add_span_processor(
14 SimpleSpanProcessor(OTLPSpanExporter())
15)
16
17# Instrument SmolAgents
18SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)
19
20# Now use SmolAgents as usual
21from smolagents import CodeAgent, WebSearchTool, OpenAIServerModel
22
23model = OpenAIServerModel(model_id="gpt-4o")
24agent = CodeAgent(
25 tools=[WebSearchTool()],
26 model=model,
27 stream_outputs=True
28)
29
30agent.run("How many seconds would it take for a leopard at full speed to run through Pont des Arts?")
Smolagents tracing

Further improvements

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