Run This Ai
EN DE

Getting Started with DSPy: Build Your First Optimized LLM Pipeline

Learn how to install DSPy, configure LLMs, build classification and RAG pipelines, and automatically optimize them with teleprompters.

DSPy Logo

Introduction

DSPy is a framework for programming—not prompting—language models. In this quick start guide, you will learn how to install DSPy, set up your first pipeline, and let the compiler optimize it automatically. By the end, you will have a working classification or RAG pipeline that outperforms hand-crafted prompts.

Installation

DSPy is available via pip and works with Python 3.9+. Install it in your preferred environment:

pip install dspy-ai

For the latest features, install from source:

pip install git+https://github.com/stanfordnlp/dspy.git

Configure Your LLM

DSPy supports all major LLM providers out of the box. Configure your preferred model with a few lines:

import dspy

# OpenAI
lm = dspy.LM('openai/gpt-4o-mini')

# Or local via Ollama
# lm = dspy.LM('ollama_chat/mixtral', api_base='http://localhost:11434')

dspy.configure(lm=lm)

Build Your First Program

Define a simple classification module. DSPy modules are declarative — you describe the input/output signatures, and the compiler handles the rest.

class Sentiment(dspy.Signature):
    """Classify sentiment."""
    text = dspy.InputField()
    sentiment = dspy.OutputField(desc='positive or negative')

classify = dspy.ChainOfThought(Sentiment)
result = classify(text='DSPy makes LLM development so much easier!')
print(result.sentiment)  # 'positive'
DSPy Optimization Flow

Automatic Optimization with Teleprompters

The real power of DSPy comes from its teleprompters — optimizers that automatically improve your program using labeled data. Here is how to use BootstrapFewShot:

from dspy.teleprompt import BootstrapFewShot

# Create training data
trainset = [
    dspy.Example(text='I love this product!', sentiment='positive').with_inputs('text'),
    dspy.Example(text='This is terrible.', sentiment='negative').with_inputs('text'),
]

# Compile an optimized program
optimizer = BootstrapFewShot(metric=lambda gold, pred: gold.sentiment == pred.sentiment)
optimized_classify = optimizer.compile(classify, trainset=trainset)

# Now your program is optimized!
result = optimized_classify(text='Works perfectly!')

Building a RAG Pipeline

DSPy makes RAG straightforward. Combine retrieval with generation in a single, optimizable module:

class RAG(dspy.Module):
    def __init__(self, num_passages=3):
        self.retrieve = dspy.Retrieve(k=num_passages)
        self.generate_answer = dspy.ChainOfThought('context, question -> answer')

    def forward(self, question):
        context = self.retrieve(question).passages
        return self.generate_answer(context=context, question=question)

rag = RAG()
result = rag(question='What is DSPy?')
print(result.answer)
DSPy Universal Compatibility

Why This Matters for Self-Hosting

DSPy is designed to work seamlessly with self-hosted models. Whether you run Ollama, vLLM, or llama.cpp on your own infrastructure, DSPy's provider-agnostic API means you can optimize pipelines using local models just as easily as cloud APIs. This gives you full control over costs, latency, and data privacy while still benefiting from state-of-the-art prompt optimization.

Next Steps

Explore DSPy's advanced features: multi-hop QA, agent-style tool use with dspy.ReAct, custom teleprompters, and integration with LangChain/LlamaIndex. The official documentation and active Discord community are excellent resources for diving deeper.

#llm #python #tutorial #dspy #prompt-optimization #rag