brainwires-provider 0.11.0

LLM provider implementations (Anthropic, OpenAI Chat + Responses, Google Gemini, Ollama, Bedrock, Vertex AI, local llama.cpp) for the Brainwires Agent Framework. Speech (TTS/STT) providers live in `brainwires-provider-speech`.
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
//! Configuration types for local LLM inference
//!
//! Defines settings for local model loading and inference parameters.

use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Default batch size for local LLM prompt processing.
const DEFAULT_LOCAL_LLM_BATCH_SIZE: usize = 512;

/// Configuration for a local LLM model
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LocalLlmConfig {
    /// Unique identifier for this model configuration
    pub id: String,

    /// Human-readable name for the model
    pub name: String,

    /// Path to the GGUF model file
    pub model_path: PathBuf,

    /// Context window size (default: 4096)
    #[serde(default = "default_context_size")]
    pub context_size: u32,

    /// Number of CPU threads to use (default: auto-detect)
    #[serde(default)]
    pub num_threads: Option<u32>,

    /// Batch size for prompt processing (default: 512)
    #[serde(default = "default_batch_size")]
    pub batch_size: u32,

    /// GPU layers to offload (0 = CPU only, default: 0)
    #[serde(default)]
    pub gpu_layers: u32,

    /// Enable memory mapping for faster loading (default: true)
    #[serde(default = "default_true")]
    pub use_mmap: bool,

    /// Enable memory locking to prevent swapping (default: false)
    #[serde(default)]
    pub use_mlock: bool,

    /// Maximum tokens to generate per response (default: 2048)
    #[serde(default = "default_max_tokens")]
    pub max_tokens: u32,

    /// Model family/type for proper prompt formatting
    #[serde(default)]
    pub model_type: LocalModelType,

    /// Optional system prompt template override
    #[serde(default)]
    pub system_template: Option<String>,

    /// Whether this model supports tool/function calling
    #[serde(default)]
    pub supports_tools: bool,

    /// Estimated RAM usage in MB (for display purposes)
    #[serde(default)]
    pub estimated_ram_mb: Option<u32>,
}

fn default_context_size() -> u32 {
    4096
}

fn default_batch_size() -> u32 {
    DEFAULT_LOCAL_LLM_BATCH_SIZE as u32
}

fn default_max_tokens() -> u32 {
    2048
}

fn default_true() -> bool {
    true
}

impl Default for LocalLlmConfig {
    fn default() -> Self {
        Self {
            id: "local-model".to_string(),
            name: "Local Model".to_string(),
            model_path: PathBuf::new(),
            context_size: default_context_size(),
            num_threads: None,
            batch_size: default_batch_size(),
            gpu_layers: 0,
            use_mmap: true,
            use_mlock: false,
            max_tokens: default_max_tokens(),
            model_type: LocalModelType::default(),
            system_template: None,
            supports_tools: false,
            estimated_ram_mb: None,
        }
    }
}

impl LocalLlmConfig {
    /// Create a new configuration for an LFM2 model
    pub fn lfm2_350m(model_path: PathBuf) -> Self {
        Self {
            id: "lfm2-350m".to_string(),
            name: "LFM2 350M".to_string(),
            model_path,
            context_size: 32768,
            batch_size: 512,
            max_tokens: 2048,
            model_type: LocalModelType::Lfm2,
            supports_tools: false,
            estimated_ram_mb: Some(220),
            ..Default::default()
        }
    }

    /// Create a new configuration for LFM2-1.2B model
    pub fn lfm2_1_2b(model_path: PathBuf) -> Self {
        Self {
            id: "lfm2-1.2b".to_string(),
            name: "LFM2 1.2B".to_string(),
            model_path,
            context_size: 32768,
            batch_size: 512,
            max_tokens: 2048,
            model_type: LocalModelType::Lfm2,
            supports_tools: false,
            estimated_ram_mb: Some(700),
            ..Default::default()
        }
    }

    /// Create a new configuration for LFM2-2.6B-Exp (agentic) model
    pub fn lfm2_2_6b_exp(model_path: PathBuf) -> Self {
        Self {
            id: "lfm2-2.6b-exp".to_string(),
            name: "LFM2 2.6B Experimental".to_string(),
            model_path,
            context_size: 32768,
            batch_size: 512,
            max_tokens: 4096,
            model_type: LocalModelType::Lfm2Agentic,
            supports_tools: true,
            estimated_ram_mb: Some(1500),
            ..Default::default()
        }
    }

    /// Create a new configuration for Granite 4.0 Nano 350M
    pub fn granite_nano_350m(model_path: PathBuf) -> Self {
        Self {
            id: "granite-nano-350m".to_string(),
            name: "Granite 4.0 Nano 350M".to_string(),
            model_path,
            context_size: 8192,
            batch_size: 512,
            max_tokens: 2048,
            model_type: LocalModelType::Granite,
            supports_tools: false,
            estimated_ram_mb: Some(250),
            ..Default::default()
        }
    }

    /// Create a new configuration for Granite 4.0 Nano 1.5B
    pub fn granite_nano_1_5b(model_path: PathBuf) -> Self {
        Self {
            id: "granite-nano-1.5b".to_string(),
            name: "Granite 4.0 Nano 1.5B".to_string(),
            model_path,
            context_size: 8192,
            batch_size: 512,
            max_tokens: 2048,
            model_type: LocalModelType::Granite,
            supports_tools: false,
            estimated_ram_mb: Some(900),
            ..Default::default()
        }
    }

    /// Validate the configuration
    pub fn validate(&self) -> Result<(), LocalLlmConfigError> {
        if self.model_path.as_os_str().is_empty() {
            return Err(LocalLlmConfigError::MissingModelPath);
        }
        if !self.model_path.exists() {
            return Err(LocalLlmConfigError::ModelNotFound(self.model_path.clone()));
        }
        if self.context_size == 0 {
            return Err(LocalLlmConfigError::InvalidContextSize);
        }
        if self.batch_size == 0 {
            return Err(LocalLlmConfigError::InvalidBatchSize);
        }
        Ok(())
    }
}

/// Model type/family for proper prompt formatting
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "lowercase")]
pub enum LocalModelType {
    /// LFM2 (Liquid Foundation Model 2) - hybrid architecture
    #[default]
    Lfm2,
    /// LFM2 Experimental variant optimized for agentic tasks
    Lfm2Agentic,
    /// Granite family models (IBM)
    Granite,
    /// Qwen family models (Alibaba)
    Qwen,
    /// Llama family models (Meta)
    Llama,
    /// Phi family models (Microsoft)
    Phi,
    /// Generic/unknown model type
    Generic,
}

impl LocalModelType {
    /// Get the chat template for this model type
    pub fn chat_template(&self) -> &'static str {
        match self {
            Self::Lfm2 | Self::Lfm2Agentic => {
                "<|system|>\n{system}<|end|>\n<|user|>\n{user}<|end|>\n<|assistant|>\n"
            }
            Self::Granite => "<|system|>\n{system}\n<|user|>\n{user}\n<|assistant|>\n",
            Self::Qwen => {
                "<|im_start|>system\n{system}<|im_end|>\n<|im_start|>user\n{user}<|im_end|>\n<|im_start|>assistant\n"
            }
            Self::Llama => {
                "<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{system}<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n{user}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
            }
            Self::Phi => "<|system|>\n{system}<|end|>\n<|user|>\n{user}<|end|>\n<|assistant|>\n",
            Self::Generic => "### System:\n{system}\n\n### User:\n{user}\n\n### Assistant:\n",
        }
    }

    /// Get the stop tokens for this model type
    pub fn stop_tokens(&self) -> Vec<&'static str> {
        match self {
            Self::Lfm2 | Self::Lfm2Agentic => vec!["<|end|>", "<|user|>"],
            Self::Granite => vec!["<|user|>", "<|system|>"],
            Self::Qwen => vec!["<|im_end|>", "<|im_start|>"],
            Self::Llama => vec!["<|eot_id|>", "<|start_header_id|>"],
            Self::Phi => vec!["<|end|>", "<|user|>"],
            Self::Generic => vec!["### User:", "### System:"],
        }
    }
}

/// Configuration errors for local LLM
#[derive(Debug, thiserror::Error)]
pub enum LocalLlmConfigError {
    /// Model path was not provided.
    #[error("Model path is required")]
    MissingModelPath,

    /// Model file does not exist at the given path.
    #[error("Model file not found: {0}")]
    ModelNotFound(PathBuf),

    /// Context size was set to zero.
    #[error("Context size must be greater than 0")]
    InvalidContextSize,

    /// Batch size was set to zero.
    #[error("Batch size must be greater than 0")]
    InvalidBatchSize,

    /// Model loading failed.
    #[error("Failed to load model: {0}")]
    ModelLoadError(String),

    /// Inference failed during generation.
    #[error("Inference error: {0}")]
    InferenceError(String),
}

/// Inference parameters for a single generation request
#[derive(Debug, Clone)]
pub struct LocalInferenceParams {
    /// Temperature for sampling (0.0 = deterministic, 1.0 = random)
    pub temperature: f32,
    /// Top-p (nucleus) sampling parameter
    pub top_p: f32,
    /// Top-k sampling parameter
    pub top_k: u32,
    /// Repetition penalty (1.0 = no penalty)
    pub repeat_penalty: f32,
    /// Maximum tokens to generate
    pub max_tokens: u32,
    /// Stop sequences
    pub stop_sequences: Vec<String>,
}

impl Default for LocalInferenceParams {
    fn default() -> Self {
        Self {
            temperature: 0.7,
            top_p: 0.9,
            top_k: 40,
            repeat_penalty: 1.1,
            max_tokens: 2048,
            stop_sequences: Vec::new(),
        }
    }
}

impl LocalInferenceParams {
    /// Create params optimized for deterministic, factual responses
    pub fn factual() -> Self {
        Self {
            temperature: 0.1,
            top_p: 0.9,
            top_k: 20,
            repeat_penalty: 1.0,
            max_tokens: 1024,
            stop_sequences: Vec::new(),
        }
    }

    /// Create params for creative, varied responses
    pub fn creative() -> Self {
        Self {
            temperature: 0.9,
            top_p: 0.95,
            top_k: 50,
            repeat_penalty: 1.2,
            max_tokens: 2048,
            stop_sequences: Vec::new(),
        }
    }

    /// Create params for routing/classification tasks
    pub fn routing() -> Self {
        Self {
            temperature: 0.0,
            top_p: 1.0,
            top_k: 1,
            repeat_penalty: 1.0,
            max_tokens: 50,
            stop_sequences: Vec::new(),
        }
    }
}

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

    #[test]
    fn test_default_config() {
        let config = LocalLlmConfig::default();
        assert_eq!(config.context_size, 4096);
        assert_eq!(config.batch_size, 512);
        assert!(config.use_mmap);
        assert!(!config.use_mlock);
    }

    #[test]
    fn test_lfm2_350m_config() {
        let config = LocalLlmConfig::lfm2_350m(PathBuf::from("/models/lfm2-350m.gguf"));
        assert_eq!(config.id, "lfm2-350m");
        assert_eq!(config.context_size, 32768);
        assert_eq!(config.estimated_ram_mb, Some(220));
        assert!(!config.supports_tools);
    }

    #[test]
    fn test_lfm2_2_6b_exp_config() {
        let config = LocalLlmConfig::lfm2_2_6b_exp(PathBuf::from("/models/lfm2-2.6b-exp.gguf"));
        assert_eq!(config.id, "lfm2-2.6b-exp");
        assert!(config.supports_tools);
        assert_eq!(config.model_type, LocalModelType::Lfm2Agentic);
    }

    #[test]
    fn test_model_type_chat_templates() {
        let lfm2 = LocalModelType::Lfm2;
        assert!(lfm2.chat_template().contains("<|system|>"));
        assert!(lfm2.chat_template().contains("<|user|>"));

        let qwen = LocalModelType::Qwen;
        assert!(qwen.chat_template().contains("<|im_start|>"));
    }

    #[test]
    fn test_model_type_stop_tokens() {
        let lfm2 = LocalModelType::Lfm2;
        assert!(lfm2.stop_tokens().contains(&"<|end|>"));

        let llama = LocalModelType::Llama;
        assert!(llama.stop_tokens().contains(&"<|eot_id|>"));
    }

    #[test]
    fn test_inference_params_presets() {
        let factual = LocalInferenceParams::factual();
        assert!(factual.temperature < 0.5);

        let creative = LocalInferenceParams::creative();
        assert!(creative.temperature > 0.7);

        let routing = LocalInferenceParams::routing();
        assert_eq!(routing.temperature, 0.0);
        assert_eq!(routing.top_k, 1);
    }

    #[test]
    fn test_config_validation_missing_path() {
        let config = LocalLlmConfig::default();
        let result = config.validate();
        assert!(matches!(result, Err(LocalLlmConfigError::MissingModelPath)));
    }
}