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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Local LLM Provider Implementation
//!
//! Implements the Provider trait for local LLM inference using llama.cpp.
//! Optimized for CPU inference with efficient memory usage.

use super::config::{LocalInferenceParams, LocalLlmConfig};
use anyhow::{Result, anyhow};
use brainwires_core::message::{Message, Role};
use std::sync::Arc;

// The `Provider` trait impl below is gated on `feature = "native"` because it
// uses `async_stream` and `tokio::time`. These imports are only needed there.
#[cfg(feature = "native")]
use async_trait::async_trait;
#[cfg(feature = "native")]
use brainwires_core::message::{ChatResponse, StreamChunk, Usage};
#[cfg(feature = "native")]
use brainwires_core::provider::{ChatOptions, Provider};
#[cfg(feature = "native")]
use brainwires_core::tool::Tool;
#[cfg(feature = "native")]
use futures::stream::BoxStream;

#[cfg(feature = "llama-cpp-2")]
use llama_cpp_2::{
    context::params::LlamaContextParams, llama_backend::LlamaBackend, llama_batch::LlamaBatch,
    model::AddBos, model::LlamaModel, model::params::LlamaModelParams, sampling::LlamaSampler,
};

/// Local LLM Provider using llama.cpp for CPU-optimized inference
///
/// This provider is designed for high-throughput, low-latency local inference
/// without requiring a GPU. Ideal for:
/// - Query routing and classification
/// - Context processing and summarization
/// - Semantic analysis
/// - Agentic decision making (with LFM2-2.6B-Exp)
pub struct LocalLlmProvider {
    /// Model configuration
    config: LocalLlmConfig,
    /// Backend instance (shared) - wrapped in mutex for thread safety
    #[cfg(feature = "llama-cpp-2")]
    backend: std::sync::Mutex<Option<LlamaBackend>>,
    /// Model instance (lazy loaded) - wrapped in mutex for thread safety
    #[cfg(feature = "llama-cpp-2")]
    model: std::sync::Mutex<Option<LlamaModel>>,
    /// Inference state (without the actual llama.cpp when feature disabled)
    #[cfg(not(feature = "llama-cpp-2"))]
    _placeholder: std::marker::PhantomData<()>,
}

impl LocalLlmProvider {
    /// Create a new local LLM provider with the given configuration
    ///
    /// The model is not loaded until the first inference call (lazy loading).
    #[cfg(feature = "llama-cpp-2")]
    pub fn new(config: LocalLlmConfig) -> Result<Self> {
        config.validate().map_err(|e| anyhow!(e))?;

        Ok(Self {
            config,
            backend: std::sync::Mutex::new(None),
            model: std::sync::Mutex::new(None),
        })
    }

    /// Create a new local LLM provider (fallback when `llama-cpp-2` feature is disabled).
    #[cfg(not(feature = "llama-cpp-2"))]
    pub fn new(config: LocalLlmConfig) -> Result<Self> {
        config.validate().map_err(|e| anyhow!(e))?;

        Ok(Self {
            config,
            _placeholder: std::marker::PhantomData,
        })
    }

    /// Create a provider for an LFM2-350M model (fastest, ~220MB RAM)
    pub fn lfm2_350m(model_path: std::path::PathBuf) -> Result<Self> {
        Self::new(LocalLlmConfig::lfm2_350m(model_path))
    }

    /// Create a provider for an LFM2-1.2B model (sweet spot, ~700MB RAM)
    pub fn lfm2_1_2b(model_path: std::path::PathBuf) -> Result<Self> {
        Self::new(LocalLlmConfig::lfm2_1_2b(model_path))
    }

    /// Get the model configuration
    pub fn config(&self) -> &LocalLlmConfig {
        &self.config
    }

    /// Check if the model is loaded
    #[cfg(feature = "llama-cpp-2")]
    pub async fn is_loaded(&self) -> bool {
        self.model.lock().map(|g| g.is_some()).unwrap_or(false)
    }

    /// Check if the model is loaded (always false without `llama-cpp-2` feature).
    #[cfg(not(feature = "llama-cpp-2"))]
    pub async fn is_loaded(&self) -> bool {
        false
    }

    /// Load the model into memory
    #[cfg(feature = "llama-cpp-2")]
    pub async fn load(&self) -> Result<()> {
        // First ensure backend is initialized
        {
            let mut backend_guard = self
                .backend
                .lock()
                .map_err(|e| anyhow!("Lock poisoned: {}", e))?;
            if backend_guard.is_none() {
                let backend = LlamaBackend::init()
                    .map_err(|e| anyhow!("Failed to initialize llama backend: {:?}", e))?;
                *backend_guard = Some(backend);
            }
        }

        // Now load model
        let mut model_guard = self
            .model
            .lock()
            .map_err(|e| anyhow!("Lock poisoned: {}", e))?;
        if model_guard.is_some() {
            return Ok(()); // Already loaded
        }

        tracing::info!("Loading local model: {}", self.config.name);

        // Configure model parameters
        let model_params = LlamaModelParams::default().with_n_gpu_layers(self.config.gpu_layers);

        // Get backend reference
        let backend_guard = self
            .backend
            .lock()
            .map_err(|e| anyhow!("Lock poisoned: {}", e))?;
        let backend = backend_guard
            .as_ref()
            .ok_or_else(|| anyhow!("Backend not initialized"))?;

        // Load the model
        let model = LlamaModel::load_from_file(backend, &self.config.model_path, &model_params)
            .map_err(|e| anyhow!("Failed to load model: {:?}", e))?;

        *model_guard = Some(model);

        tracing::info!("Local model loaded successfully: {}", self.config.name);
        Ok(())
    }

    /// Load the model (errors without `llama-cpp-2` feature).
    #[cfg(not(feature = "llama-cpp-2"))]
    pub async fn load(&self) -> Result<()> {
        Err(anyhow!(
            "Local LLM support is not enabled. Build with --features llama-cpp-2"
        ))
    }

    /// Unload the model from memory
    #[cfg(feature = "llama-cpp-2")]
    pub async fn unload(&self) {
        if let Ok(mut model_guard) = self.model.lock() {
            *model_guard = None;
        }
        tracing::info!("Local model unloaded: {}", self.config.name);
    }

    /// Unload the model (no-op without `llama-cpp-2` feature).
    #[cfg(not(feature = "llama-cpp-2"))]
    pub async fn unload(&self) {
        // No-op when feature not enabled
    }

    /// Format messages into a prompt string using the model's chat template
    #[cfg_attr(not(feature = "native"), allow(dead_code))]
    fn format_prompt(&self, messages: &[Message], system: Option<&str>) -> String {
        let template = self.config.model_type.chat_template();

        // Extract system message
        let system_msg = system.map(String::from).or_else(|| {
            messages.iter().find_map(|m| {
                if m.role == Role::System {
                    m.text().map(String::from)
                } else {
                    None
                }
            })
        });

        // Build the prompt
        let mut prompt = String::new();

        // Add system message if present
        if let Some(sys) = &system_msg
            && template.contains("{system}")
        {
            // Template has system placeholder
            let sys_part = template
                .split("{user}")
                .next()
                .unwrap_or("")
                .replace("{system}", sys);
            prompt.push_str(&sys_part);
        }

        // Add conversation turns
        for msg in messages {
            match msg.role {
                Role::System => continue, // Already handled
                Role::User => {
                    if let Some(text) = msg.text() {
                        // Find the user part of the template
                        let user_template = if template.contains("{user}") {
                            template
                                .split("{user}")
                                .nth(1)
                                .and_then(|s| s.split("{").next())
                                .unwrap_or("\n")
                        } else {
                            "\n"
                        };
                        prompt.push_str(text);
                        prompt.push_str(user_template);
                    }
                }
                Role::Assistant => {
                    if let Some(text) = msg.text() {
                        prompt.push_str(text);
                        prompt.push('\n');
                    }
                }
                Role::Tool => {
                    // Format tool results
                    if let Some(text) = msg.text() {
                        prompt.push_str("[Tool Result]: ");
                        prompt.push_str(text);
                        prompt.push('\n');
                    }
                }
            }
        }

        prompt
    }

    /// Perform inference without loading (assumes model is loaded)
    #[cfg(feature = "llama-cpp-2")]
    fn generate_impl_sync(&self, prompt: &str, params: &LocalInferenceParams) -> Result<String> {
        let model_guard = self
            .model
            .lock()
            .map_err(|e| anyhow!("Lock poisoned: {}", e))?;
        let model = model_guard
            .as_ref()
            .ok_or_else(|| anyhow!("Model not loaded"))?;

        let backend_guard = self
            .backend
            .lock()
            .map_err(|e| anyhow!("Lock poisoned: {}", e))?;
        let backend = backend_guard
            .as_ref()
            .ok_or_else(|| anyhow!("Backend not initialized"))?;

        // Configure context parameters
        let mut ctx_params = LlamaContextParams::default();
        ctx_params = ctx_params.with_n_ctx(std::num::NonZeroU32::new(self.config.context_size));
        ctx_params = ctx_params.with_n_batch(self.config.batch_size);
        if let Some(threads) = self.config.num_threads {
            ctx_params = ctx_params.with_n_threads(threads as i32);
        }

        // Create a context for this generation
        let mut ctx = model
            .new_context(backend, ctx_params)
            .map_err(|e| anyhow!("Failed to create context: {:?}", e))?;

        // Tokenize the prompt
        let tokens = model
            .str_to_token(prompt, AddBos::Always)
            .map_err(|e| anyhow!("Tokenization failed: {:?}", e))?;

        // Create batch and add tokens
        let mut batch = LlamaBatch::new(self.config.batch_size as usize, 1);

        for (i, token) in tokens.iter().enumerate() {
            let is_last = i == tokens.len() - 1;
            batch
                .add(*token, i as i32, &[0], is_last)
                .map_err(|e| anyhow!("Failed to add token to batch: {:?}", e))?;
        }

        // Process the prompt
        ctx.decode(&mut batch)
            .map_err(|e| anyhow!("Prompt processing failed: {:?}", e))?;

        // Build sampler chain
        let mut sampler = LlamaSampler::chain_simple([
            LlamaSampler::temp(params.temperature),
            LlamaSampler::top_p(params.top_p, 1),
            LlamaSampler::top_k(params.top_k as i32),
            LlamaSampler::penalties(64, params.repeat_penalty, 0.0, 0.0),
            LlamaSampler::dist(42),
        ]);

        // Generate tokens
        let mut decoder = encoding_rs::UTF_8.new_decoder();
        let mut output = String::new();
        let stop_tokens = self.config.model_type.stop_tokens();
        let mut generated = 0u32;
        let mut cur_pos = tokens.len() as i32;

        while generated < params.max_tokens {
            // Sample next token
            let token = sampler.sample(&ctx, -1);

            // Check for EOS
            if model.is_eog_token(token) {
                break;
            }

            // Decode token to string
            let piece = model
                .token_to_piece(token, &mut decoder, false, None)
                .map_err(|e| anyhow!("Token decode failed: {:?}", e))?;

            // Check for stop sequences
            let should_stop = stop_tokens.iter().any(|s| output.ends_with(s));
            if should_stop {
                // Remove the stop sequence from output
                for stop in &stop_tokens {
                    if output.ends_with(stop) {
                        output.truncate(output.len() - stop.len());
                        break;
                    }
                }
                break;
            }

            // Check custom stop sequences
            let custom_stop = params.stop_sequences.iter().any(|s| output.ends_with(s));
            if custom_stop {
                break;
            }

            output.push_str(&piece);

            // Clear batch and add the new token
            batch.clear();
            batch
                .add(token, cur_pos, &[0], true)
                .map_err(|e| anyhow!("Failed to add token: {:?}", e))?;

            // Process the token through the model
            ctx.decode(&mut batch)
                .map_err(|e| anyhow!("Generation failed: {:?}", e))?;

            cur_pos += 1;
            generated += 1;
        }

        Ok(output.trim().to_string())
    }

    #[cfg(not(feature = "llama-cpp-2"))]
    fn generate_impl_sync(&self, _prompt: &str, _params: &LocalInferenceParams) -> Result<String> {
        Err(anyhow!(
            "Local LLM support is not enabled. Build with --features llama-cpp-2"
        ))
    }

    /// Generate a response from a prompt
    pub async fn generate(&self, prompt: &str, params: &LocalInferenceParams) -> Result<String> {
        // Ensure model is loaded
        if !self.is_loaded().await {
            self.load().await?;
        }

        // Run sync inference in blocking task to not block async runtime
        let prompt = prompt.to_string();
        let params = params.clone();

        // Since llama-cpp-2 isn't Send+Sync friendly, we run sync
        self.generate_impl_sync(&prompt, &params)
    }

    /// Simple completion for routing/classification tasks
    ///
    /// Optimized for fast, deterministic responses.
    pub async fn route(&self, prompt: &str) -> Result<String> {
        self.generate(prompt, &LocalInferenceParams::routing())
            .await
    }

    /// Summarize or process text
    pub async fn process(&self, prompt: &str) -> Result<String> {
        self.generate(prompt, &LocalInferenceParams::factual())
            .await
    }
}

// The streaming `Provider` impl uses `async_stream` and `tokio::time`, which are
// only enabled by the `native` feature. On wasm32 builds (or any non-`native`
// build) the `Provider` trait is still implemented via `Self::route` / `generate`
// directly when needed; the LLM types themselves are constructible.
#[cfg(feature = "native")]
#[async_trait]
impl Provider for LocalLlmProvider {
    #[allow(clippy::misnamed_getters)] // Returns config.id intentionally — this is a trait method, not a field getter
    fn name(&self) -> &str {
        &self.config.id
    }

    fn max_output_tokens(&self) -> Option<u32> {
        Some(self.config.max_tokens)
    }

    async fn chat(
        &self,
        messages: &[Message],
        _tools: Option<&[Tool]>,
        options: &ChatOptions,
    ) -> Result<ChatResponse> {
        // Format messages into prompt
        let prompt = self.format_prompt(messages, options.system.as_deref());

        // Build inference params from options
        let params = LocalInferenceParams {
            temperature: options.temperature.unwrap_or(0.7),
            max_tokens: options.max_tokens.unwrap_or(self.config.max_tokens),
            stop_sequences: options.stop.clone().unwrap_or_default(),
            ..Default::default()
        };

        // Generate response
        let response_text = self.generate(&prompt, &params).await?;

        // Estimate token counts (rough approximation: 4 chars per token)
        let prompt_tokens = (prompt.len() / 4) as u32;
        let completion_tokens = (response_text.len() / 4) as u32;

        Ok(ChatResponse {
            message: Message::assistant(response_text),
            usage: Usage::new(prompt_tokens, completion_tokens),
            finish_reason: Some("stop".to_string()),
        })
    }

    fn stream_chat<'a>(
        &'a self,
        messages: &'a [Message],
        _tools: Option<&'a [Tool]>,
        options: &'a ChatOptions,
    ) -> BoxStream<'a, Result<StreamChunk>> {
        let prompt = self.format_prompt(messages, options.system.as_deref());

        let params = LocalInferenceParams {
            temperature: options.temperature.unwrap_or(0.7),
            max_tokens: options.max_tokens.unwrap_or(self.config.max_tokens),
            stop_sequences: options.stop.clone().unwrap_or_default(),
            ..Default::default()
        };

        // For now, we generate the full response and stream it in chunks
        // A true streaming implementation would require changes to the underlying llama.cpp bindings
        Box::pin(async_stream::stream! {
            match self.generate(&prompt, &params).await {
                Ok(response) => {
                    // Stream the response in chunks
                    const CHUNK_SIZE: usize = 10; // characters per chunk
                    for chunk in response.chars().collect::<Vec<_>>().chunks(CHUNK_SIZE) {
                        let text: String = chunk.iter().collect();
                        yield Ok(StreamChunk::Text(text));
                        // Small delay to simulate streaming
                        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
                    }

                    // Send usage info
                    let prompt_tokens = (prompt.len() / 4) as u32;
                    let completion_tokens = (response.len() / 4) as u32;
                    yield Ok(StreamChunk::Usage(Usage::new(prompt_tokens, completion_tokens)));
                    yield Ok(StreamChunk::Done);
                }
                Err(e) => {
                    yield Err(e);
                }
            }
        })
    }
}

impl std::fmt::Debug for LocalLlmProvider {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LocalLlmProvider")
            .field("config", &self.config)
            .finish()
    }
}

/// Pool of local LLM providers for parallel inference
///
/// Manages multiple model instances for high-throughput scenarios.
/// Each instance uses separate memory, allowing true parallel inference.
pub struct LocalLlmPool {
    /// Available providers
    providers: Vec<Arc<LocalLlmProvider>>,
    /// Current index for round-robin selection
    current: std::sync::atomic::AtomicUsize,
}

impl LocalLlmPool {
    /// Create a new pool with the specified number of instances
    pub fn new(config: LocalLlmConfig, instances: usize) -> Result<Self> {
        let mut providers = Vec::with_capacity(instances);
        for _ in 0..instances {
            providers.push(Arc::new(LocalLlmProvider::new(config.clone())?));
        }

        Ok(Self {
            providers,
            current: std::sync::atomic::AtomicUsize::new(0),
        })
    }

    /// Get the next available provider (round-robin)
    pub fn next(&self) -> Arc<LocalLlmProvider> {
        let idx = self
            .current
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
            % self.providers.len();
        self.providers[idx].clone()
    }

    /// Load all models in the pool
    pub async fn load_all(&self) -> Result<()> {
        for provider in &self.providers {
            provider.load().await?;
        }
        Ok(())
    }

    /// Unload all models in the pool
    pub async fn unload_all(&self) {
        for provider in &self.providers {
            provider.unload().await;
        }
    }

    /// Get the number of instances in the pool
    pub fn size(&self) -> usize {
        self.providers.len()
    }

    /// Estimate total RAM usage for the pool
    pub fn estimated_ram_mb(&self) -> Option<u32> {
        self.providers
            .first()
            .and_then(|p| p.config.estimated_ram_mb)
            .map(|ram| ram * self.providers.len() as u32)
    }
}

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

    #[test]
    fn test_provider_creation() {
        let config = LocalLlmConfig::lfm2_350m(PathBuf::from("/tmp/test.gguf"));
        // This will fail validation since the file doesn't exist
        let result = LocalLlmProvider::new(config);
        assert!(result.is_err());
    }

    #[test]
    fn test_inference_params_defaults() {
        let params = LocalInferenceParams::default();
        assert_eq!(params.temperature, 0.7);
        assert_eq!(params.max_tokens, 2048);
    }

    #[test]
    fn test_pool_estimated_ram() {
        // Create a mock pool structure for testing
        let _config = LocalLlmConfig {
            model_path: PathBuf::from("."), // Use current dir to pass validation
            estimated_ram_mb: Some(220),
            ..Default::default()
        };

        // For 4 instances of a 220MB model
        let expected_ram = 220 * 4;
        assert_eq!(expected_ram, 880);
    }
}