mini-langchain 0.1.0

Minimal Rust LangChain implementation for text-only interactions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

# Mini-LangChain Design Document

> Minimal Rust LangChain implementation - focus on core features, text-only, type-safe, and easy to use.

## Project Goals

### Core Principles
- **Minimalism**: Only essential features, avoid over-engineering
- **Type Safety**: Leverage Rust's type system
- **Zero-cost Abstraction**: Performance first, avoid unnecessary runtime overhead
- **Personal Use First**: Designed for personal/small projects

### Supported Features

- Text input/output (text only)
- Multiple LLM support (OpenAI, Anthropic, Qwen, Deepseek, Ollama)
- Tool/Function Calling
- Agent mode (ReAct)
- Config file driven

### Not Supported

- Multimodal (image/audio/video)
- Complex chains (only basic supported)
- Vector DB/document loaders (future optional)

---

## Architecture Overview

### Main Modules

```
mini-langchain/
├── src/
│   ├── lib.rs            # Library entry point
│   ├── llm/              # LLM abstraction and implementations
│   ├── tools/            # Tool system
│   ├── agent/            # Agent implementation
│   ├── message.rs        # Message types
│   ├── config.rs         # Config management
│   ├── error.rs          # Error types
│   └── prelude.rs        # Convenient imports
├── Cargo.toml
├── DESIGN.md
└── README.md
```


## Core Type Design

### LLM Trait

```rust
// src/llm/traits.rs (simplified)
#[async_trait]
pub trait LLM: Send + Sync {
    async fn generate(&self, messages: &[Message]) -> LLMResult<GenerateResult>;
    // ...
}
```

### Message Type

```rust
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    pub role: MessageRole,
    pub content: String,
    pub name: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MessageRole {
    System,
    User,
    Assistant,
    ToolResponce,
    Tool,
    Developer,
}
```

### Tool System


```rust
// src/tools/schema.rs (simplified)
pub struct ToolSchema {
    pub name: String,
    pub description: String,
    pub parameters: serde_json::Value,
}

pub trait Tool: Send + Sync {
    fn name(&self) -> &str;
    fn description(&self) -> &str;
    fn schema(&self) -> ToolSchema;
    async fn run(&self, input: serde_json::Value) -> Result<String, ToolError>;
}

// You can define custom tools using the proc-macro attribute:
#[tool(
    name = "get_weather",
    description = "Get weather for a given city",
    params(city = "City name, e.g. 'San Francisco'")
)]
fn get_weather(city: String) -> String {
    format!("It's always sunny in {}!", city)
}
```

### Agent

```rust
// src/agent/types.rs (simplified)
pub struct Agent {
    pub name: String,
    pub llm: Arc<dyn LLM>,
    pub tools: HashMap<String, Arc<dyn Tool>>,
    pub memory: Vec<Message>,
    pub system_prompt: Option<String>,
    pub max_iterations: usize,
}

impl Agent {
    pub fn new(name: impl Into<String>, llm: Arc<dyn LLM>, max_iterations: Option<usize>) -> Self { ... }
    pub fn register_tool(&mut self, name: Option<&str>, tool: Arc<dyn Tool>) -> &mut Self { ... }
    pub fn run_task(&mut self, task: &str) -> ... { ... }
}
```
```


---

## Config

### config.toml


```toml
[llm]
provider = "openai"     # openai | anthropic | qwen | deepseek | ollama
model = "gpt-4"
api_key = "sk-..."       # Optional, read from environment variables
base_url = "https://..." # Optional

[agent]
max_iterations = 5
temperature = 0.7

[tools]
enabled = ["calculator", "search"]

# Optional: Tool-specific configurations
[tools.search]
api_key = "search-api-key"
max_results = 5
```

### Configuration Loading


```rust
#[derive(Debug, Deserialize)]
pub struct Config {
    pub llm: LLMConfig,
    pub agent: AgentConfig,
    pub tools: ToolsConfig,
}

impl Config {
    pub fn from_file(path: &str) -> Result<Self, ConfigError> {
        let content = std::fs::read_to_string(path)?;
        toml::from_str(&content).map_err(Into::into)
    }
    
    pub fn from_env() -> Result<Self, ConfigError> {
        // MINI_LANGCHAIN_PROVIDER -> llm.provider
        // MINI_LANGCHAIN_API_KEY -> llm.api_key
        // MINI_LANGCHAIN_MODEL -> llm.model
        unimplemented!("Load configuration from environment variables")
    }
}
```

---

## Error Handling

### Unified Error Types


```rust
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("LLM error: {0}")]
    LLM(#[from] LLMError),
    
    #[error("Tool error: {0}")]
    Tool(#[from] ToolError),
    
    #[error("Agent error: {0}")]
    Agent(#[from] AgentError),
    
    #[error("Config error: {0}")]
    Config(#[from] ConfigError),
    
    #[error("HTTP error: {0}")]
    Http(#[from] reqwest::Error),
    
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),
}

pub type Result<T> = std::result::Result<T, Error>;
```

---


## Implementation Plan

### Phase 1: Core Foundation (Week 1)
- [ ] Project structure setup
- [ ] Message and Error base types
- [ ] LLM trait definition
- [ ] Basic OpenAI implementation (only generate)
- [ ] Configuration system

### Phase 2: Tool System (Week 2)
- [ ] ToolSchema design
- [ ] Tool trait definition
- [ ] define_tool! macro implementation
- [ ] Built-in tool: Calculator
- [ ] OpenAI Function Calling integration

### Phase 3: Multi-LLM Support (Week 3)
- [ ] Anthropic implementation
- [ ] Qwen implementation
- [ ] Deepseek implementation
- [ ] Ollama implementation
- [ ] From<&ToolSchema> adapter

### Phase 4: Agent (Week 4)
- [ ] SimpleAgent implementation
- [ ] ReAct mode
- [ ] Tool execution loop
- [ ] Error handling and retry

### Phase 5: Optimization & Examples (Week 5)
- [ ] Streaming output support

## Error Handling

### Unified Error Types

```rust
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("LLM error: {0}")]
    LLM(#[from] LLMError),
    #[error("Tool error: {0}")]
    Tool(#[from] ToolError),
    #[error("Agent error: {0}")]
    Agent(#[from] AgentError),
    #[error("Config error: {0}")]
    Config(#[from] ConfigError),
    #[error("HTTP error: {0}")]
    Http(#[from] reqwest::Error),
    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),
}

pub type Result<T> = std::result::Result<T, Error>;
```

---

## Example Usage


### Simple Chat (Config-based)

```rust
use mini_langchain::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    let config = Config::from_file("config.toml")?;
    let llm = create_llm(&config.llm)?;
    let messages = vec![
        Message::system("You are a helpful assistant."),
        Message::user("What is Rust?"),
    ];
    let response = llm.generate(&messages).await?;
    println!("{}", response);
    Ok(())
}
```

### Ollama Direct Usage Example

```rust
use mini_langchain::llm::ollama::Ollama;
use mini_langchain::message::Message;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create Ollama wrapper and select a local model (e.g. qwen3:8b)
    let ollama = Ollama::default().with_model("qwen3:8b");

    let messages = vec![Message::user("Why is the sky blue?")];

    match ollama.generate(&messages).await {
        Ok(res) => {
            println!("generation: {}", res.generation);
            let tokens = res.tokens;
            println!("tokens: prompt={} completion={} total={}", tokens.prompt_tokens, tokens.completion_tokens, tokens.total_tokens);
        }
        Err(e) => eprintln!("error calling Ollama: {:?}", e),
    }
    Ok(())
}
```

### Agent Example (Config-based)

```rust
use mini_langchain::prelude::*;

#[tokio::main]
async fn main() -> Result<()> {
    let config = Config::from_file("config.toml")?;
    let llm = create_llm(&config.llm)?;
    let tools = vec![
        Arc::new(CalculatorTool) as Arc<dyn Tool>,
        Arc::new(SearchTool) as Arc<dyn Tool>,
    ];
    let mut agent = Agent::new("assistant", llm, Some(5));
    for tool in &tools {
        agent.register_tool(None, tool.clone());
    }
    let result = agent.run_task("What's the weather in Beijing today? If the temperature is above 25°C, calculate 25 * 1.8 + 32.").await?;
    println!("Result: {}", result);
    Ok(())
}
```

### Agent Example with Ollama and Custom Tool

```rust
use mini_langchain::{
    *,
    llm::ollama::Ollama,
    agent::{
        types::Agent,
        traits::AgentRunner
    }
};
use std::sync::Arc;

// Use the proc-macro attribute to generate the Tool implementation
#[tool(
    name = "get_weather",
    description = "Get weather for a given city",
    params(city = "City name, e.g. 'San Francisco'")
)]
fn get_weather(city: String) -> String {
    format!("It's always sunny in {}!", city)
}

#[tokio::main]
async fn main() {
    // Adjust model name to one available in your Ollama server.
    let ollama = Ollama::default().with_model("qwen3:8b");
    let llm: Arc<dyn mini_langchain::llm::traits::LLM> = Arc::new(ollama);

    let mut agent = Agent::new("Ollama_qwen3:8b", llm, Some(5));
    agent.register_tool(None, Arc::new(GetWeatherTool));

    agent.set_system_prompt(
        r##"You are a weather forecasting intelligent assistant. You can query tools or answer directly."##);

    let prompt = "What's the weather in Beijing?";

    match agent.call_llm(prompt).await {
        Ok(res) => {
            println!("generation: {:?}", res);
        }
        Err(e) => eprintln!("LLM error: {:?}", e),
    }
}
```

## Design Principles

1. **Simplicity over complexity**: Only implement what is needed, avoid over-abstraction, keep codebase small.
2. **Type safety**: Leverage Rust's type system, prefer Result over unwrap().
3. **Zero-cost abstraction**: Prefer generics over trait objects when possible, avoid unnecessary heap allocations.
4. **Pragmatism**: Use standard crates, don't reinvent the wheel, use serde_json::Value for flexibility.
5. **Extensibility**: Leave room for future extension, but don't code for imaginary needs.

---

## License

MIT OR Apache-2.0

## References

- [LangChain Python]https://github.com/langchain-ai/langchain
- [LangChain.js]https://github.com/langchain-ai/langchainjs
- [langchain-rust]https://github.com/Abraxas-365/langchain-rust
- [OpenAI API Reference]https://platform.openai.com/docs/api-reference
- [Anthropic API Reference]https://docs.anthropic.com/claude/reference