mojentic 2.1.0

An LLM integration framework for Rust
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
419
420
# LLM Broker Guide

The `Broker` is the central interface for interacting with Large Language Models in Mojentic. It provides a consistent, type-safe API across different LLM providers.

## Overview

```
Your App → Broker → Gateway → LLM Provider
                ↓
            Tool Execution
```

## Creating a Broker

```rust
use mojentic::llm::{Broker, gateways::OllamaGateway};

// Basic broker
let gateway = OllamaGateway::default();
let broker = Broker::new("qwen3:32b", gateway);

// With correlation ID for request tracking
let broker = Broker::with_correlation_id(
    "qwen3:32b",
    gateway,
    "request-abc-123"
);
```

### Broker Structure

```rust
pub struct Broker<G: Gateway> {
    model: String,
    gateway: G,
    correlation_id: Option<String>,
}
```

The broker is generic over the gateway type, ensuring type safety at compile time.

## Text Generation

Basic text generation:

```rust
use mojentic::llm::Message;

let messages = vec![
    Message::user("Explain Rust's ownership in one sentence")
];

let response = broker.generate(&messages, None).await?;
println!("{}", response);
```

### With Configuration

```rust
use mojentic::llm::CompletionConfig;

let config = CompletionConfig {
    temperature: 0.3,  // Lower = more focused
    max_tokens: Some(500),
    ..Default::default()
};

let response = broker
    .generate(&messages, Some(config))
    .await?;
```

### With Tools

```rust
use mojentic::llm::Tool;

let tools: Vec<Box<dyn Tool>> = vec![
    Box::new(DateResolver),
    Box::new(CurrentDateTime),
];

let response = broker
    .generate_with_tools(&messages, &tools, Some(config))
    .await?;
```

## Structured Output

Generate responses conforming to JSON schemas:

```rust
use serde::{Deserialize, Serialize};
use serde_json::json;

#[derive(Debug, Deserialize, Serialize)]
struct Analysis {
    title: String,
    summary: String,
    keywords: Vec<String>,
}

let schema = json!({
    "type": "object",
    "properties": {
        "title": {"type": "string"},
        "summary": {"type": "string"},
        "keywords": {
            "type": "array",
            "items": {"type": "string"}
        }
    },
    "required": ["title", "summary"]
});

let messages = vec![
    Message::user("Analyze: Rust is a systems programming language")
];

let analysis: Analysis = broker
    .generate_object(&messages, &schema, None)
    .await?;

println!("{:#?}", analysis);
```

## Tool Execution Flow

When tools are provided, the Broker handles a recursive loop:

1. Send messages to LLM
2. If LLM requests tools:
   - Execute each tool concurrently
   - Add tool results to conversation
   - Return to step 1
3. Return final text response

```rust
// The broker automatically:
// 1. Calls LLM with the question
// 2. LLM responds with tool call request
// 3. Broker executes DateResolver::run(args)
// 4. Broker adds result to conversation
// 5. Calls LLM again with tool result
// 6. LLM responds with final answer

let messages = vec![Message::user("What's the date next Monday?")];
let tools: Vec<Box<dyn Tool>> = vec![Box::new(DateResolver)];

let response = broker
    .generate_with_tools(&messages, &tools, None)
    .await?;
```

## Message Management

```rust
use mojentic::llm::Message;

let messages = vec![
    Message::system("You are a helpful coding assistant"),
    Message::user("How do I read a file in Rust?"),
    Message::assistant("You can use std::fs::read_to_string()..."),
    Message::user("What about async?"),
];

let response = broker.generate(&messages, None).await?;
```

### Message Types

- `Message::system(content)` - Set LLM behavior
- `Message::user(content)` - User input  
- `Message::assistant(content)` - LLM responses (for history)
- Tool-related messages for tool execution flow

## Error Handling

The Broker returns `Result<T, Error>`:

```rust
use mojentic::error::Error;

match broker.generate(&messages, None).await {
    Ok(response) => {
        // Success
        println!("{}", response);
    }
    Err(Error::Timeout) => {
        // Request timed out
        eprintln!("Timeout");
    }
    Err(Error::InvalidResponse) => {
        // LLM returned invalid format
        eprintln!("Invalid response");
    }
    Err(Error::HttpError(429)) => {
        // Rate limited
        eprintln!("Rate limited");
    }
    Err(Error::HttpError(status)) => {
        // Other HTTP errors
        eprintln!("HTTP {}", status);
    }
    Err(Error::GatewayError(msg)) => {
        // Gateway-specific error
        eprintln!("Gateway: {}", msg);
    }
    Err(e) => {
        // Other errors
        eprintln!("Error: {}", e);
    }
}
```

### Error Recovery

```rust
async fn generate_with_retry(
    broker: &Broker<impl Gateway>,
    messages: &[Message],
    max_retries: u32
) -> Result<String, Error> {
    let mut attempts = 0;
    
    loop {
        match broker.generate(messages, None).await {
            Ok(response) => return Ok(response),
            Err(Error::Timeout) if attempts < max_retries => {
                attempts += 1;
                eprintln!("Timeout, retrying {}/{}...", attempts, max_retries);
                tokio::time::sleep(Duration::from_secs(1)).await;
            }
            Err(e) => return Err(e),
        }
    }
}
```

## Correlation IDs

Track requests across your system:

```rust
// Generate unique ID
let correlation_id = uuid::Uuid::new_v4().to_string();
let broker = Broker::with_correlation_id(
    "qwen3:32b",
    gateway,
    &correlation_id
);

// ID flows through all operations
let response = broker.generate(&messages, None).await?;

// Find logs/events by correlation_id
```

## Configuration Options

```rust
use mojentic::llm::CompletionConfig;

// Creative writing
let config = CompletionConfig {
    temperature: 1.5,
    max_tokens: Some(2000),
    ..Default::default()
};

// Factual responses
let config = CompletionConfig {
    temperature: 0.1,
    max_tokens: Some(500),
    ..Default::default()
};

// Long context
let config = CompletionConfig {
    num_ctx: Some(32768),
    ..Default::default()
};

// Constrained generation
let config = CompletionConfig {
    temperature: 0.7,
    num_predict: Some(256),
    ..Default::default()
};
```

## Best Practices

### 1. Use System Messages

```rust
let messages = vec![
    Message::system(
        "You are a helpful assistant that provides concise answers. \
         Always format code examples with proper syntax highlighting."
    ),
    Message::user("How do I create a struct?"),
];
```

### 2. Handle Errors Properly

```rust
use mojentic::error::Error;

let result = broker.generate(&messages, None).await;

match result {
    Ok(response) => process(response),
    Err(Error::Timeout) => retry(),
    Err(Error::HttpError(429)) => backoff_and_retry(),
    Err(e) => {
        log::error!("LLM error: {}", e);
        return fallback_response();
    }
}
```

### 3. Manage Context Windows

```rust
fn keep_recent_messages(messages: &[Message], max: usize) -> Vec<Message> {
    if messages.len() <= max {
        messages.to_vec()
    } else {
        messages[messages.len() - max..].to_vec()
    }
}

let messages = keep_recent_messages(&conversation_history, 10);
```

### 4. Use Appropriate Timeouts

```rust
use std::time::Duration;

let gateway = OllamaGateway::new(
    "http://localhost:11434",
    Duration::from_secs(600)  // 10 minutes for large models
);
```

## Advanced Usage

### Concurrent Requests

```rust
use futures::future::try_join_all;

let tasks: Vec<_> = inputs
    .iter()
    .map(|input| {
        let messages = vec![Message::user(input)];
        broker.generate(&messages, None)
    })
    .collect();

let responses = try_join_all(tasks).await?;
```

### Multiple Models

```rust
// Different models for different tasks
let fast_broker = Broker::new("qwen3:7b", gateway.clone());
let smart_broker = Broker::new("qwen3:32b", gateway);

// Quick classification
let category = fast_broker.generate(&classify_msgs, None).await?;

// Deep analysis
let analysis = smart_broker.generate(&analysis_msgs, None).await?;
```

### Streaming Responses

```rust
use futures::StreamExt;

let mut stream = broker.generate_stream(&messages, None).await?;

while let Some(chunk) = stream.next().await {
    match chunk {
        Ok(text) => print!("{}", text),
        Err(e) => eprintln!("Stream error: {}", e),
    }
}
```

## Type Safety Benefits

Rust's type system prevents common errors:

```rust
// Cannot create invalid message roles
let msg = Message::user("text");  // ✓ Valid
// let msg = Message("invalid");  // ✗ Compile error

// Gateway trait ensures consistent API
fn process<G: Gateway>(broker: &Broker<G>) {
    // Works with any gateway implementation
}

// Result types force error handling
let response = broker.generate(&messages, None).await;
// Must handle Result - cannot ignore errors
```

## See Also

- [Getting Started]./getting-started.md
- [Tool Usage]./tool-usage.md
- [Structured Output]./structured-output.md
- [Error Handling]./error-handling.md