DSRs
A high-performance DSPy rewrite in Rust for building LM-powered applications
Documentation โข API Reference โข Examples โข Issues โข Discord
๐ Overview
DSRs (DSPy Rust) is a ground-up rewrite of the DSPy framework in Rust, designed for building robust, high-performance applications powered by Language Models. Unlike a simple port, DSRs leverages Rust's type system, memory safety, and concurrency features to provide a more efficient and reliable foundation for LM applications.
๐ฆ Installation
Add DSRs to your Cargo.toml:
[]
# Option 1: Use the shorter alias (recommended)
= { = "dspy-rs", = "0.5.0" }
# Option 2: Use the full name
= "0.5.0"
Or use cargo:
# Option 1: Add with alias (recommended)
# Option 2: Add with full name
๐ง Quick Start
Here's a simple example to get you started:
use Result;
use *;
async
Result:
Answer: "Positive"
๐๏ธ Architecture
DSRs follows a modular architecture with clear separation of concerns:
dsrs/
โโโ core/ # Core abstractions (LM, Module, Signature)
โโโ adapter/ # LM provider adapters (OpenAI, etc.)
โโโ data/ # Data structures (Example, Prediction)
โโโ predictors/ # Built-in predictors (Predict, Chain, etc.)
โโโ evaluate/ # Evaluation framework and metrics
โโโ macros/ # Derive macros for signatures
Core Components
1. Signatures - Define Input/Output Specifications
// Enable chain-of-thought reasoning
2. Modules - Composable Pipeline Components
3. Predictors - Pre-built LM Interaction Patterns
// Get prediction
let predict = new;
4. Language Models - Configurable LM Backends
// Configure with OpenAI
let lm = LMbuilder
.api_key
.model
.temperature
.max_tokens
.build;
5. Evaluation - Evaluating your Modules
// Evaluate your module
let test_examples = load_test_data;
let module = new;
// Automatically runs predictions and computes average metric
let score = module.evaluate.await;
println!;
6. Optimization - Optimize your Modules
DSRs provides two powerful optimizers:
COPRO (Collaborative Prompt Optimization)
// Create and configure the optimizer
let optimizer = COPRObuilder
.breadth // Number of candidates per iteration
.depth // Number of refinement iterations
.build;
// Prepare training data
let train_examples = load_training_data;
// Compile optimizes the module in-place
let mut module = new;
optimizer.compile.await?;
MIPROv2 (Multi-prompt Instruction Proposal Optimizer v2) - Advanced optimizer using LLMs
// MIPROv2 uses a 3-stage process:
// 1. Generate execution traces
// 2. LLM generates candidate prompts with best practices
// 3. Evaluate and select the best prompt
let optimizer = builder
.num_candidates // Number of candidate prompts to generate
.num_trials // Number of evaluation trials
.minibatch_size // Examples per evaluation
.temperature // Temperature for prompt generation
.build;
optimizer.compile.await?;
See examples/08-optimize-mipro.rs for a complete example (requires parquet feature).
Component Freezing:
// The Optimizable derive macro automatically implements the trait and marks Module Optimizable
๐ Examples
Example 1: Multi-Step Reasoning Pipeline
use *;
๐งช Testing
Run the test suite:
# All tests
# Specific test
# With output
# Run examples
๐ ๏ธ Other Features
Chain of Thought (CoT) Reasoning
// Enable CoT with attribute
Optimizer Comparison
| Feature | COPRO | MIPROv2 |
|---|---|---|
| Approach | Iterative refinement | LLM-guided generation |
| Complexity | Simple | Advanced |
| Best For | Quick optimization | Best results |
| Training Data | Uses scores | Uses traces & descriptions |
| Prompting Tips | No | Yes (15+ best practices) |
| Program Understanding | Basic | LLM-generated descriptions |
| Few-shot Examples | No | Yes (auto-selected) |
When to use COPRO:
- Fast iteration needed
- Simple tasks
- Limited compute budget
When to use MIPROv2:
- Best possible results needed
- Complex reasoning tasks
- Have good training data (15+ examples recommended)
๐ Project Status
โ ๏ธ Beta Release - DSRs is in active development. The API is stabilizing but may have breaking changes.
๐ค Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Setup
# Clone the repository
# Build the project
# Run tests
# Run with examples
# Check formatting
# Run clippy
๐ License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
๐ Acknowledgments
- Inspired by the original DSPy framework
- Built with the amazing Rust ecosystem
- Special thanks to the DSPy community for the discussion and ideas
- MIPROv2 implementation