DSPy

DSPy automates the optimization of language model prompts and weights, removing manual tuning from the development process. The framework handles prompt refinement systematically, improving reliability when building complex LLM-based systems.

This guide demonstrates capturing telemetry from DSPy applications using InteractiveAI.

Prerequisites

  • InteractiveAI account with API credentials

  • LLM provider credentials (OpenAI, Ollama, or other supported provider)


Installation

pip install interactiveai dspy openinference-instrumentation-dspy

Configuration

Set your API credentials as environment variables:

import os

# InteractiveAI credentials
# Obtain keys from Settings > API Keys in the dashboard
os.environ["INTERACTIVEAI_PUBLIC_KEY"] = "pk-ia-..."
os.environ["INTERACTIVEAI_SECRET_KEY"] = "sk-ia-..."
os.environ["INTERACTIVEAI_HOST"] = "https://app.interactiveai.com"

# Model provider credentials
os.environ["OPENAI_API_KEY"] = "sk-proj-..."

Initialize the client and confirm connectivity:


Enabling Trace Capture

DSPy has a dedicated OpenInference instrumentor that captures all LM calls automatically:


Configuring DSPy

Initialize a language model and set it as the default for DSPy operations:


Running DSPy Modules

With instrumentation active, all DSPy operations generate traces automatically.

Chain-of-Thought Reasoning

Retrieval-Augmented Generation

ReAct with Tool Usage

source: https://dspy.ai/arrow-up-right


Enriching Traces with Context

Layer the InteractiveAI SDK on top of DSPy instrumentation to attach identifiers and metadata:


Trace Visibility

The InteractiveAI dashboard displays:

  • LM calls with prompts and completions

  • Chain-of-thought reasoning steps

  • Tool invocations and retrieval operations

  • Token usage and latency per operation

Last updated

Was this helpful?