DSRs
A high-performance DSPy rewrite in Rust for building LLM-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 LLM applications.
๐ฆ Installation
Add DSRs to your Cargo.toml
:
[]
# Option 1: Use the shorter alias (recommended)
= { = "dspy-rs", = "0.0.2-beta" }
# Option 2: Use the full name
= "0.0.2-beta"
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 *;
use Result;
async
๐๏ธ Architecture
DSRs follows a modular architecture with clear separation of concerns:
dsrs/
โโโ core/ # Core abstractions (LM, Module, Signature)
โโโ adapter/ # LLM 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 LLM Interaction Patterns
// Get prediction
let predict = new;
4. Language Models - Configurable LLM Backends
// Configure with OpenAI
let lm = LM builder
.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
// Create and configure the optimizer
let optimizer = COPRO builder
.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?;
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
๐ 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