Trace DSPy programs

Trace DSPy predictors and model calls in Future AGI with traceAI-DSPy.

Trace a DSPy predictor’s inputs, outputs and model calls in Future AGI.

1. Install

Use Python 3.10–3.12 and install the instrumentor and DSPy:

pip install traceAI-DSPy dspy

2. Set Environment Variables

Set your Future AGI and OpenAI keys. Use data approved for tracing: the instrumentor records predictor inputs and outputs, model messages and error details by default. See trace masking before using sensitive data.

import os

os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
os.environ["FI_API_KEY"] = "your-futureagi-api-key"
os.environ["FI_SECRET_KEY"] = "your-futureagi-secret-key"

3. Initialize Trace Provider

Set up the trace provider to create a new project in FutureAGI, establish telemetry data pipelines.

from fi_instrumentation import register
from fi_instrumentation.fi_types import ProjectType

trace_provider = register(
    project_type=ProjectType.OBSERVE,
    project_name="dspy_project",
)

4. Instrument your Project

Initialize the DSPy instrumentor to enable automatic tracing.

from traceai_dspy import DSPyInstrumentor

DSPyInstrumentor().instrument(tracer_provider=trace_provider)

5. Create DSPy Components and Run your application

Run your DSPy application to send traces to Future AGI.

import dspy

class BasicQA(dspy.Signature):
    """Answer questions with short factoid answers."""

    question = dspy.InputField()
    answer = dspy.OutputField(desc="often between 1 and 5 words")

if __name__ == "__main__":
    turbo = dspy.LM(model="openai/gpt-4")

    dspy.settings.configure(lm=turbo)

    # Define the predictor.
    generate_answer = dspy.Predict(BasicQA)

    # Call the predictor on a particular input.
    pred = generate_answer(question="What is the capital of the united states?")
    print(f"Predicted Answer: {pred.answer}")
    trace_provider.force_flush()

View the trace

In Future AGI, open Observe and select the project configured above to inspect the trace.

Was this page helpful?

Questions & Discussion