Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<img width="768" alt="logo" src="https://github.com/user-attachments/assets/bdb80520-216e-4742-b016-b71ca6eaac03" />

# DSRs
<em>A high-performance DSPy rewrite in Rust for building LLM-powered applications</em>
<em>A high-performance DSPy rewrite in Rust for building LM-powered applications</em>

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
[![Rust](https://img.shields.io/badge/rust-1.70+-orange.svg)](https://www.rust-lang.org)
Expand All @@ -18,7 +18,7 @@

## 🚀 Overview

**DSRs** (DSPy Rust) is a ground-up rewrite of the [DSPy framework](https://github.com/stanfordnlp/dspy) 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.
**DSRs** (DSPy Rust) is a ground-up rewrite of the [DSPy framework](https://github.com/stanfordnlp/dspy) 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

Expand All @@ -27,10 +27,10 @@ Add DSRs to your `Cargo.toml`:
```toml
[dependencies]
# Option 1: Use the shorter alias (recommended)
dsrs = { package = "dspy-rs", version = "0.0.2-beta" }
dsrs = { package = "dspy-rs", version = "0.5.0" }

# Option 2: Use the full name
dspy-rs = "0.0.2-beta"
dspy-rs = "0.5.0"
```

Or use cargo:
Expand Down Expand Up @@ -96,7 +96,7 @@ DSRs follows a modular architecture with clear separation of concerns:
```
dsrs/
├── core/ # Core abstractions (LM, Module, Signature)
├── adapter/ # LLM provider adapters (OpenAI, etc.)
├── adapter/ # LM provider adapters (OpenAI, etc.)
├── data/ # Data structures (Example, Prediction)
├── predictors/ # Built-in predictors (Predict, Chain, etc.)
├── evaluate/ # Evaluation framework and metrics
Expand Down Expand Up @@ -137,13 +137,13 @@ impl Module for CustomModule {
}
```

#### 3. **Predictors** - Pre-built LLM Interaction Patterns
#### 3. **Predictors** - Pre-built LM Interaction Patterns
```rust
// Get prediction
let predict = Predict::new(MySignature::new());
```

#### 4. **Language Models** - Configurable LLM Backends
#### 4. **Language Models** - Configurable LM Backends
```rust
// Configure with OpenAI
let lm = LM::builder()
Expand Down
14 changes: 7 additions & 7 deletions crates/dspy-rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<img width="768" alt="logo" src="https://github.com/user-attachments/assets/bdb80520-216e-4742-b016-b71ca6eaac03" />

# DSRs
<em>A high-performance DSPy rewrite in Rust for building LLM-powered applications</em>
<em>A high-performance DSPy rewrite in Rust for building LM-powered applications</em>

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
[![Rust](https://img.shields.io/badge/rust-1.70+-orange.svg)](https://www.rust-lang.org)
Expand All @@ -18,7 +18,7 @@

## 🚀 Overview

**DSRs** (DSPy Rust) is a ground-up rewrite of the [DSPy framework](https://github.com/stanfordnlp/dspy) 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.
**DSRs** (DSPy Rust) is a ground-up rewrite of the [DSPy framework](https://github.com/stanfordnlp/dspy) 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

Expand All @@ -27,10 +27,10 @@ Add DSRs to your `Cargo.toml`:
```toml
[dependencies]
# Option 1: Use the shorter alias (recommended)
dsrs = { package = "dspy-rs", version = "0.0.2-beta" }
dsrs = { package = "dspy-rs", version = "0.5.0" }

# Option 2: Use the full name
dspy-rs = "0.0.2-beta"
dspy-rs = "0.5.0"
```

Or use cargo:
Expand Down Expand Up @@ -96,7 +96,7 @@ DSRs follows a modular architecture with clear separation of concerns:
```
dsrs/
├── core/ # Core abstractions (LM, Module, Signature)
├── adapter/ # LLM provider adapters (OpenAI, etc.)
├── adapter/ # LM provider adapters (OpenAI, etc.)
├── data/ # Data structures (Example, Prediction)
├── predictors/ # Built-in predictors (Predict, Chain, etc.)
├── evaluate/ # Evaluation framework and metrics
Expand Down Expand Up @@ -137,13 +137,13 @@ impl Module for CustomModule {
}
```

#### 3. **Predictors** - Pre-built LLM Interaction Patterns
#### 3. **Predictors** - Pre-built LM Interaction Patterns
```rust
// Get prediction
let predict = Predict::new(MySignature::new());
```

#### 4. **Language Models** - Configurable LLM Backends
#### 4. **Language Models** - Configurable LM Backends
```rust
// Configure with OpenAI
let lm = LM::builder()
Expand Down
11 changes: 11 additions & 0 deletions docs/community.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: "Community"
description: "Join the conversation and get help"
icon: "users"
---

Stay connected with the DSRs community.

- Discord: https://discord.com/invite/ZAEGgxjPUe
- GitHub: https://github.com/krypticmouse/DSRs

43 changes: 23 additions & 20 deletions docs/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,45 @@
"navigation": {
"tabs": [
{
"tab": "Documentation",
"tab": "Learn DSRs",
"groups": [
{
"group": "Getting started",
"group": "Get Started",
"pages": [
"index",
"docs/getting-started/quickstart"
]
},
{
"group": "Building Blocks",
"pages": [
"docs/building-blocks/signature",
"docs/building-blocks/lm",
"docs/building-blocks/predictors",
"docs/building-blocks/module"
]
},
{
"group": "Data",
"pages": [
"docs/data/examples",
"docs/data/prediction"
]
},
}
]
},
{
"tab": "Tutorials",
"groups": [
{
"group": "Building blocks",
"group": "Overview",
"pages": [
"docs/building-blocks/signature",
"docs/building-blocks/lm",
"docs/building-blocks/predictors",
"docs/building-blocks/module"
"docs/tutorials/overview"
]
}
]
}
],
"global": {
"anchors": [
{
"anchor": "DSPy Discord",
"href": "https://discord.com/invite/ZAEGgxjPUe",
"icon": "discord"
}
]
}
]
},
"logo": {
"light": "/logo/main.png",
Expand All @@ -65,7 +67,8 @@
"footer": {
"socials": {
"x": "https://x.com/krypticmouse",
"github": "https://github.com/krypticmouse/DSRs"
"github": "https://github.com/krypticmouse/DSRs",
"discord": "https://discord.com/invite/ZAEGgxjPUe"
}
}
}
}
91 changes: 89 additions & 2 deletions docs/docs/building-blocks/lm.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,92 @@
---
title: 'Language Model'
title: 'Language Models'
description: 'Configure and integrate language models in your pipelines'
icon: 'arrow-down-a-z'
---
---

DSRs treats the Language Model (`LM`) as a first–class, configurable client for chat-style inference. This page explains what an LM is in DSRs, how it’s structured in Rust terms, and how it cooperates with other building blocks.

## What is an LM?

- **Purpose:** Encapsulates a provider client (e.g., OpenAI) and model-level settings, and executes chat completions.
- **Rust shape:** `LM` is a clonable struct built via a builder (`LM::builder()`), holding `LMConfig` and an internal client.
- **Async-first:** `LM::call(...)` is `async` and returns a `(Message, LmUsage)` pair.
- **History:** Each call is recorded; inspect recent calls via `lm.inspect_history(n)`.

## Key types

- **`LM` (struct):** Holds `api_key`, `base_url`, `config`, and a `history` of calls. Implements `Clone`.
- **`LMConfig` (struct):** Builder-driven config (e.g., `model`, `temperature`, `max_tokens`).
- **`Chat`, `Message` (structs):** Internal chat abstraction used by adapters to format/parse messages.
- **`Adapter` (trait) and `ChatAdapter` (impl):** Orchestrate how a `Signature` + `Example` become a `Chat`, and parse the model’s response back to a `Prediction`.

## Construction and configuration

```rust
use dsrs::{LM, LMConfig};

let lm = LM::builder()
.api_key(std::env::var("OPENAI_API_KEY")?.into())
.config(
LMConfig::builder()
.model("gpt-4o-mini".to_string())
.temperature(0.7)
.max_tokens(512)
.build()
)
.build();
```

- **Builder pattern:** Idiomatic in Rust; keeps construction explicit and typed.
- **Clone semantics:** `LM` implements `Clone`—cloning copies config and history for that instance.

## Global vs explicit usage

- **Global:** `configure(lm.clone(), ChatAdapter::default())` sets the process-wide default used by predictors.
- **Explicit:** Some APIs accept a mutable `&mut LM` if you prefer local control: `predictor.forward_with_config(inputs, &mut lm).await`.

## Async execution and sync entry

- **Async:** Calls are `async`; prefer using an async runtime (Tokio).
- **Sync-style:** If you need a plain `fn main`, create a runtime and `block_on` the async work.

<Tabs>
<Tab title="Async (Tokio)">

```rust
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// build + use LM here
Ok(())
}
```

</Tab>
<Tab title="Sync">

```rust
fn main() -> anyhow::Result<()> {
let rt = tokio::runtime::Runtime::new()?;
rt.block_on(async move {
// build + use LM here
Ok(())
})
}
```

</Tab>
</Tabs>

## Inspecting history

```rust
let history = lm.inspect_history(3);
for entry in history {
println!("Model: {} | Output: {}", entry.config.model, entry.output.content());
}
```

## Where it fits

- You rarely call `LM` directly; instead, a `Predictor` uses an `Adapter` to format a `Signature` and call the LM.
- This keeps business logic (your task) separate from transport (the model client), matching Rust’s trait-driven composition style.
40 changes: 37 additions & 3 deletions docs/docs/building-blocks/module.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
---
title: 'Module'
description: 'Building flows for complex LLM pipelines'
title: 'Modules'
description: 'Building flows for complex LM pipelines'
icon: 'circle-nodes'
---
---

Modules let you compose multiple predictors and arbitrary Rust logic into reusable units. In DSRs, a module is any type that implements the `Module` trait.

## What is a module?

- **Purpose:** Encapsulate a multi-step workflow (e.g., analysis then answer) as a single callable unit.
- **Rust shape:** Any type implementing `Module` with an `async fn forward(&self, inputs: Example)`.
- **Composition:** Typically holds one or more `Predict` fields and sequences their calls.

## Minimal example

```rust
use dsrs::{Example, Module, Predict, Prediction, Signature};

#[Signature]
struct QA { #[input] question: String, #[output] answer: String }

struct Answerer { predict: Predict }

impl Default for Answerer { fn default() -> Self { Self { predict: Predict::new(QA::new()) } } }

#[allow(async_fn_in_trait)]
impl Module for Answerer {
async fn forward(&self, inputs: Example) -> anyhow::Result<Prediction> {
self.predict.forward(inputs).await
}
}
```

## Design notes

- **Traits and async:** `Module` is a trait with an async method; implement it where your composition lives.
- **State:** Keep structured state as fields (other predictors, config, toggles). Prefer builders for complex modules.
- **Testability:** You can inject a `DummyLM` via `forward_with_config` in tests to avoid network calls.
2 changes: 1 addition & 1 deletion docs/docs/building-blocks/predictors.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: 'Predictors'
description: 'Learn how to create and use predictors for LLM inference'
description: 'Learn how to create and use predictors for LM inference'
icon: 'robot'
---
Loading