llama-rs 0.16.0

A high-performance Rust implementation of llama.cpp - LLM inference engine with full GGUF support
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Council configuration — TOML/JSON deserializable, validated.

use std::path::PathBuf;

use serde::Deserialize;

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CouncilConfig {
    #[serde(default = "default_min_rounds")]
    pub min_rounds: u32,

    #[serde(default = "default_max_rounds")]
    pub max_rounds: u32,

    #[serde(default = "default_threshold")]
    pub convergence_threshold: f32,

    pub embedder: EmbedderConfig,

    #[serde(rename = "agent")]
    pub agents: Vec<AgentConfig>,

    #[serde(default)]
    pub failure: FailureConfig,

    #[serde(default)]
    pub sampling: SamplingConfig,

    /// Optional shared system prompt sent to every expert and the synthesizer.
    /// When `None`, no system message is sent.
    #[serde(default)]
    pub system_prompt: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum EmbedderConfig {
    /// Local GGUF embedding model loaded by the orchestrator.
    LocalGguf {
        path: PathBuf,
    },
}

#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AgentConfig {
    pub role: AgentRole,
    pub endpoint: String,
    pub model: String,
    pub timeout_ms: u64,

    /// Environment variable name to read the bearer token from
    /// (only honored for HTTP endpoints).
    #[serde(default)]
    pub api_key_env: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AgentRole {
    Expert,
    Synthesizer,
}

#[derive(Debug, Clone, Deserialize, Default)]
#[serde(deny_unknown_fields)]
pub struct FailureConfig {
    /// Absolute count of experts required per round. When `None`,
    /// `floor(N_experts / 2) + 1` is used (simple majority).
    #[serde(default)]
    pub min_quorum: Option<u32>,
}

#[derive(Debug, Clone, Default, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SamplingConfig {
    #[serde(default)]
    pub temperature: Option<f32>,
    #[serde(default)]
    pub top_p: Option<f32>,
    #[serde(default)]
    pub max_tokens: Option<u32>,
    #[serde(default)]
    pub seed: Option<u64>,
}

#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
    #[error("failed to parse council config: {0}")]
    Parse(String),
    #[error("at least 2 experts are required; got {0}")]
    TooFewExperts(usize),
    #[error("exactly one synthesizer is required; got {0}")]
    SynthesizerCount(usize),
    #[error("min_rounds ({min}) must be >= 1 and <= max_rounds ({max})")]
    InvalidRounds { min: u32, max: u32 },
    #[error("convergence_threshold ({0}) must be in [0.0, 1.0]")]
    InvalidThreshold(f32),
    #[error("min_quorum ({quorum}) must be in [1, {experts}] (count of experts)")]
    InvalidQuorum { quorum: u32, experts: u32 },
    #[error("unsupported endpoint scheme in `{0}`; expected grpc://, http://, or https://")]
    UnsupportedScheme(String),
    #[error("agent timeout_ms must be > 0; got 0 for endpoint {0}")]
    ZeroTimeout(String),
}

fn default_min_rounds() -> u32 {
    2
}
fn default_max_rounds() -> u32 {
    4
}
fn default_threshold() -> f32 {
    0.92
}

impl CouncilConfig {
    /// Parse a TOML string and validate it.
    pub fn from_toml_str(s: &str) -> Result<Self, ConfigError> {
        let cfg: Self = toml::from_str(s).map_err(|e| ConfigError::Parse(e.to_string()))?;
        cfg.validate()?;
        Ok(cfg)
    }

    /// Number of expert-role agents.
    pub fn expert_count(&self) -> u32 {
        self.agents
            .iter()
            .filter(|a| a.role == AgentRole::Expert)
            .count() as u32
    }

    /// Effective `min_quorum` — either the explicit config value or the
    /// simple-majority default.
    pub fn effective_min_quorum(&self) -> u32 {
        match self.failure.min_quorum {
            Some(q) => q,
            None => self.expert_count() / 2 + 1,
        }
    }

    fn validate(&self) -> Result<(), ConfigError> {
        let expert_count = self.expert_count();
        if expert_count < 2 {
            return Err(ConfigError::TooFewExperts(expert_count as usize));
        }

        let synth_count = self
            .agents
            .iter()
            .filter(|a| a.role == AgentRole::Synthesizer)
            .count();
        if synth_count != 1 {
            return Err(ConfigError::SynthesizerCount(synth_count));
        }

        if self.min_rounds < 1 || self.max_rounds < self.min_rounds {
            return Err(ConfigError::InvalidRounds {
                min: self.min_rounds,
                max: self.max_rounds,
            });
        }

        if !(0.0..=1.0).contains(&self.convergence_threshold) {
            return Err(ConfigError::InvalidThreshold(self.convergence_threshold));
        }

        if let Some(q) = self.failure.min_quorum
            && (q < 1 || q > expert_count)
        {
            return Err(ConfigError::InvalidQuorum {
                quorum: q,
                experts: expert_count,
            });
        }

        for agent in &self.agents {
            if !is_supported_scheme(&agent.endpoint) {
                return Err(ConfigError::UnsupportedScheme(agent.endpoint.clone()));
            }
            if agent.timeout_ms == 0 {
                return Err(ConfigError::ZeroTimeout(agent.endpoint.clone()));
            }
        }

        Ok(())
    }
}

fn is_supported_scheme(endpoint: &str) -> bool {
    endpoint.starts_with("grpc://")
        || endpoint.starts_with("http://")
        || endpoint.starts_with("https://")
}

#[cfg(test)]
mod tests {
    use super::*;

    fn good_config() -> &'static str {
        r#"
            min_rounds = 2
            max_rounds = 4
            convergence_threshold = 0.92

            [embedder]
            kind = "local_gguf"
            path = "/tmp/embedder.gguf"

            [[agent]]
            role = "expert"
            endpoint = "grpc://node1:50051"
            model = "qwen3-32b"
            timeout_ms = 30000

            [[agent]]
            role = "expert"
            endpoint = "http://node2:8080"
            model = "llama-3.3-70b"
            api_key_env = "OPENAI_API_KEY"
            timeout_ms = 30000

            [[agent]]
            role = "synthesizer"
            endpoint = "grpc://node3:50051"
            model = "deepseek-v3"
            timeout_ms = 60000

            [failure]
            min_quorum = 2
        "#
    }

    #[test]
    fn parses_valid_config() {
        let cfg = CouncilConfig::from_toml_str(good_config()).expect("config should parse");
        assert_eq!(cfg.min_rounds, 2);
        assert_eq!(cfg.max_rounds, 4);
        assert!((cfg.convergence_threshold - 0.92).abs() < 1e-6);
        assert_eq!(cfg.agents.len(), 3);
        assert_eq!(cfg.expert_count(), 2);
        assert_eq!(cfg.effective_min_quorum(), 2);
        assert!(matches!(cfg.embedder, EmbedderConfig::LocalGguf { .. }));
    }

    #[test]
    fn defaults_apply_when_omitted() {
        let toml = r#"
            [embedder]
            kind = "local_gguf"
            path = "/x.gguf"

            [[agent]]
            role = "expert"
            endpoint = "grpc://a:1"
            model = "m1"
            timeout_ms = 1000

            [[agent]]
            role = "expert"
            endpoint = "grpc://b:1"
            model = "m2"
            timeout_ms = 1000

            [[agent]]
            role = "expert"
            endpoint = "grpc://c:1"
            model = "m3"
            timeout_ms = 1000

            [[agent]]
            role = "synthesizer"
            endpoint = "grpc://s:1"
            model = "ms"
            timeout_ms = 1000
        "#;
        let cfg = CouncilConfig::from_toml_str(toml).expect("defaults config should parse");
        assert_eq!(cfg.min_rounds, 2, "min_rounds default");
        assert_eq!(cfg.max_rounds, 4, "max_rounds default");
        assert_eq!(cfg.convergence_threshold, 0.92);
        // 3 experts → floor(3/2)+1 = 2
        assert_eq!(cfg.effective_min_quorum(), 2);
        assert!(cfg.system_prompt.is_none());
    }

    #[test]
    fn rejects_too_few_experts() {
        let toml = r#"
            [embedder]
            kind = "local_gguf"
            path = "/x.gguf"

            [[agent]]
            role = "expert"
            endpoint = "grpc://a:1"
            model = "m1"
            timeout_ms = 1000

            [[agent]]
            role = "synthesizer"
            endpoint = "grpc://s:1"
            model = "ms"
            timeout_ms = 1000
        "#;
        let err = CouncilConfig::from_toml_str(toml).unwrap_err();
        assert!(matches!(err, ConfigError::TooFewExperts(1)), "got {err:?}");
    }

    #[test]
    fn rejects_no_synthesizer() {
        let toml = r#"
            [embedder]
            kind = "local_gguf"
            path = "/x.gguf"

            [[agent]]
            role = "expert"
            endpoint = "grpc://a:1"
            model = "m1"
            timeout_ms = 1000

            [[agent]]
            role = "expert"
            endpoint = "grpc://b:1"
            model = "m2"
            timeout_ms = 1000
        "#;
        let err = CouncilConfig::from_toml_str(toml).unwrap_err();
        assert!(matches!(err, ConfigError::SynthesizerCount(0)), "got {err:?}");
    }

    #[test]
    fn rejects_two_synthesizers() {
        let toml = r#"
            [embedder]
            kind = "local_gguf"
            path = "/x.gguf"

            [[agent]]
            role = "expert"
            endpoint = "grpc://a:1"
            model = "m1"
            timeout_ms = 1000

            [[agent]]
            role = "expert"
            endpoint = "grpc://b:1"
            model = "m2"
            timeout_ms = 1000

            [[agent]]
            role = "synthesizer"
            endpoint = "grpc://s1:1"
            model = "ms"
            timeout_ms = 1000

            [[agent]]
            role = "synthesizer"
            endpoint = "grpc://s2:1"
            model = "ms2"
            timeout_ms = 1000
        "#;
        let err = CouncilConfig::from_toml_str(toml).unwrap_err();
        assert!(matches!(err, ConfigError::SynthesizerCount(2)), "got {err:?}");
    }

    #[test]
    fn rejects_invalid_threshold() {
        let bad = good_config().replace("convergence_threshold = 0.92", "convergence_threshold = 1.5");
        let err = CouncilConfig::from_toml_str(&bad).unwrap_err();
        assert!(matches!(err, ConfigError::InvalidThreshold(_)));
    }

    #[test]
    fn rejects_invalid_rounds() {
        let bad = good_config()
            .replace("min_rounds = 2", "min_rounds = 5")
            .replace("max_rounds = 4", "max_rounds = 4");
        let err = CouncilConfig::from_toml_str(&bad).unwrap_err();
        assert!(
            matches!(err, ConfigError::InvalidRounds { min: 5, max: 4 }),
            "got {err:?}"
        );
    }

    #[test]
    fn rejects_zero_timeout() {
        let bad = good_config().replacen("timeout_ms = 30000", "timeout_ms = 0", 1);
        let err = CouncilConfig::from_toml_str(&bad).unwrap_err();
        assert!(matches!(err, ConfigError::ZeroTimeout(_)));
    }

    #[test]
    fn rejects_unsupported_scheme() {
        let bad = good_config().replace("grpc://node1:50051", "ftp://node1:50051");
        let err = CouncilConfig::from_toml_str(&bad).unwrap_err();
        assert!(matches!(err, ConfigError::UnsupportedScheme(_)));
    }

    #[test]
    fn rejects_quorum_above_expert_count() {
        let bad = good_config().replace("min_quorum = 2", "min_quorum = 5");
        let err = CouncilConfig::from_toml_str(&bad).unwrap_err();
        assert!(
            matches!(err, ConfigError::InvalidQuorum { quorum: 5, experts: 2 }),
            "got {err:?}"
        );
    }

    #[test]
    fn min_quorum_default_is_simple_majority() {
        // 4 experts should default to floor(4/2)+1 = 3
        let toml = r#"
            [embedder]
            kind = "local_gguf"
            path = "/x.gguf"

            [[agent]]
            role = "expert"
            endpoint = "grpc://a:1"
            model = "m1"
            timeout_ms = 1000

            [[agent]]
            role = "expert"
            endpoint = "grpc://b:1"
            model = "m2"
            timeout_ms = 1000

            [[agent]]
            role = "expert"
            endpoint = "grpc://c:1"
            model = "m3"
            timeout_ms = 1000

            [[agent]]
            role = "expert"
            endpoint = "grpc://d:1"
            model = "m4"
            timeout_ms = 1000

            [[agent]]
            role = "synthesizer"
            endpoint = "grpc://s:1"
            model = "ms"
            timeout_ms = 1000
        "#;
        let cfg = CouncilConfig::from_toml_str(toml).expect("valid");
        assert_eq!(cfg.expert_count(), 4);
        assert_eq!(cfg.effective_min_quorum(), 3);
    }

    #[test]
    fn rejects_unknown_top_level_field() {
        let bad = format!("{}\nrandom_field = 42\n", good_config());
        let err = CouncilConfig::from_toml_str(&bad).unwrap_err();
        assert!(matches!(err, ConfigError::Parse(_)));
    }
}