DSPy

Active
GitHub Python MIT

Description

DSPy is a declarative LLM programming framework focused on optimizable prompts and program structure, suitable for complex agent workflows.

Key Features

  • Declarative LLM programming — Define LM call logic in Python code instead of prompts for modular AI system construction
  • Automatic prompt optimization — DSPy Compiler automatically optimizes prompts and few-shot examples without manual tuning
  • Modular pipeline composition — Freely combine ChainOfThought, ReAct, multi-hop and other modules for complex reasoning chains
  • RAG pipeline support — Built-in retrieval-augmented generation pipelines with vector retrieval, reranking components
  • Agent loop framework — Build agent loops orchestrating tool calls, reasoning, and retrieval as optimizable programs
  • Differentiable weight optimization — Fine-tune LLMs with simultaneous optimization of prompts and model weights

Use Cases

💡 Build classification and information extraction pipelines — Define classifiers declaratively and auto-optimize accuracy via Compiler
💡 Multi-step RAG systems — Orchestrate retrieval, reranking, generation steps with automatic prompt optimization per step
💡 Agent tool-calling flows — Orchestrate tool selection, parameter extraction, result integration as optimizable agent pipelines
💡 Academic research and benchmarks — Rapidly build NLP task pipelines for systematic optimization and evaluation

Quick Start

pip install dspy

import dspy

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

# Define a simple module
class RAG(dspy.Module):
    def __init__(self, passages_per_query=3):
        self.retrieve = dspy.Retrieve(k=passages_per_query)
        self.generate = dspy.ChainOfThought('context, question -> answer')

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

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

Related Projects

Related Articles