data-modelling-core 2.4.0

Core SDK library for model operations across platforms
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
//! llama.cpp client for offline LLM inference
//!
//! This module provides an embedded llama.cpp inference engine for
//! offline schema refinement using local GGUF model files.
//!
//! # Example
//!
//! ```ignore
//! use data_modelling_core::llm::llamacpp::LlamaCppClient;
//!
//! let client = LlamaCppClient::new("/models/codellama-7b.gguf")?
//!     .with_gpu_layers(35)
//!     .with_context_size(4096);
//!
//! let response = client.complete("Analyze this schema...").await?;
//! ```

use std::path::PathBuf;
use std::sync::Arc;

use async_trait::async_trait;

use super::client::LlmClient;
use super::error::{LlmError, LlmResult};

#[cfg(feature = "llm-offline")]
use llama_cpp_2::{
    context::params::LlamaContextParams,
    llama_backend::LlamaBackend,
    llama_batch::LlamaBatch,
    model::{AddBos, LlamaModel, Special, params::LlamaModelParams},
    sampling::{LlamaSampler, LlamaSamplerChainParams},
    token::LlamaToken,
};

#[cfg(feature = "llm-offline")]
use std::sync::Mutex;

/// llama.cpp client for offline LLM inference
#[derive(Debug)]
pub struct LlamaCppClient {
    /// Path to the GGUF model file
    model_path: PathBuf,
    /// Model name (derived from filename)
    model_name: String,
    /// Number of GPU layers to offload
    gpu_layers: u32,
    /// Context size in tokens
    context_size: usize,
    /// Temperature for sampling
    temperature: f32,
    /// Top-p (nucleus) sampling
    top_p: f32,
    /// Top-k sampling
    top_k: i32,
    /// Repeat penalty
    repeat_penalty: f32,
    /// Maximum tokens to generate
    max_gen_tokens: usize,
    /// Loaded model (lazy initialization)
    #[cfg(feature = "llm-offline")]
    inner: Arc<Mutex<Option<LoadedModel>>>,
}

/// Holds the loaded model and backend
#[cfg(feature = "llm-offline")]
struct LoadedModel {
    backend: LlamaBackend,
    model: LlamaModel,
}

#[cfg(feature = "llm-offline")]
impl std::fmt::Debug for LoadedModel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LoadedModel")
            .field("n_params", &self.model.n_params())
            .field("n_vocab", &self.model.n_vocab())
            .finish()
    }
}

impl LlamaCppClient {
    /// Create a new llama.cpp client
    ///
    /// # Arguments
    /// * `model_path` - Path to the GGUF model file
    ///
    /// # Returns
    /// A new client instance (model is loaded lazily on first use)
    pub fn new(model_path: impl Into<PathBuf>) -> LlmResult<Self> {
        let path: PathBuf = model_path.into();

        // Extract model name from filename
        let model_name = path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("unknown")
            .to_string();

        // Verify the file exists
        if !path.exists() {
            return Err(LlmError::ModelError(format!(
                "Model file not found: {}",
                path.display()
            )));
        }

        // Verify it's a GGUF file
        if path.extension().and_then(|s| s.to_str()) != Some("gguf") {
            return Err(LlmError::ModelError(format!(
                "Model file must be a GGUF file: {}",
                path.display()
            )));
        }

        Ok(Self {
            model_path: path,
            model_name,
            gpu_layers: 0,
            context_size: 4096,
            temperature: 0.1,
            top_p: 0.9,
            top_k: 40,
            repeat_penalty: 1.1,
            max_gen_tokens: 2048,
            #[cfg(feature = "llm-offline")]
            inner: Arc::new(Mutex::new(None)),
        })
    }

    /// Set the number of GPU layers to offload
    ///
    /// Set to 0 for CPU-only inference, or higher values to offload
    /// more layers to the GPU for faster inference.
    pub fn with_gpu_layers(mut self, layers: u32) -> Self {
        self.gpu_layers = layers;
        self
    }

    /// Set the context size in tokens
    pub fn with_context_size(mut self, size: usize) -> Self {
        self.context_size = size;
        self
    }

    /// Set the temperature for sampling
    pub fn with_temperature(mut self, temperature: f32) -> Self {
        self.temperature = temperature.clamp(0.0, 2.0);
        self
    }

    /// Set the top-p (nucleus) sampling parameter
    pub fn with_top_p(mut self, top_p: f32) -> Self {
        self.top_p = top_p.clamp(0.0, 1.0);
        self
    }

    /// Set the top-k sampling parameter
    pub fn with_top_k(mut self, top_k: i32) -> Self {
        self.top_k = top_k.max(1);
        self
    }

    /// Set the repeat penalty
    pub fn with_repeat_penalty(mut self, penalty: f32) -> Self {
        self.repeat_penalty = penalty.clamp(1.0, 2.0);
        self
    }

    /// Set the maximum tokens to generate
    pub fn with_max_gen_tokens(mut self, max_tokens: usize) -> Self {
        self.max_gen_tokens = max_tokens;
        self
    }

    /// Get the model path
    pub fn model_path(&self) -> &PathBuf {
        &self.model_path
    }

    /// Check if GPU acceleration is enabled
    pub fn uses_gpu(&self) -> bool {
        self.gpu_layers > 0
    }

    /// Load the model if not already loaded
    #[cfg(feature = "llm-offline")]
    fn ensure_loaded(&self) -> LlmResult<()> {
        let mut inner = self
            .inner
            .lock()
            .map_err(|e| LlmError::ModelError(format!("Failed to acquire model lock: {}", e)))?;

        if inner.is_some() {
            return Ok(());
        }

        tracing::info!(
            model_path = %self.model_path.display(),
            gpu_layers = self.gpu_layers,
            context_size = self.context_size,
            "Loading llama.cpp model"
        );

        // Initialize backend
        let backend = LlamaBackend::init().map_err(|e| {
            LlmError::ModelError(format!("Failed to initialize llama.cpp backend: {}", e))
        })?;

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

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

        tracing::info!(
            n_params = model.n_params(),
            n_vocab = model.n_vocab(),
            n_embd = model.n_embd(),
            "Model loaded successfully"
        );

        *inner = Some(LoadedModel { backend, model });
        Ok(())
    }

    /// Generate a completion using the loaded model
    #[cfg(feature = "llm-offline")]
    fn generate_completion(&self, prompt: &str) -> LlmResult<String> {
        // Ensure model is loaded
        self.ensure_loaded()?;

        let inner = self
            .inner
            .lock()
            .map_err(|e| LlmError::ModelError(format!("Failed to acquire model lock: {}", e)))?;

        let loaded = inner
            .as_ref()
            .ok_or_else(|| LlmError::ModelError("Model not loaded".to_string()))?;

        // Create context
        let ctx_params = LlamaContextParams::default()
            .with_n_ctx(std::num::NonZeroU32::new(self.context_size as u32).unwrap());

        let mut ctx = loaded
            .model
            .new_context(&loaded.backend, ctx_params)
            .map_err(|e| LlmError::ModelError(format!("Failed to create context: {}", e)))?;

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

        let prompt_len = tokens.len();
        tracing::debug!(prompt_len, "Prompt tokenized");

        if prompt_len > self.context_size {
            return Err(LlmError::ContextTooLarge {
                max: self.context_size,
                actual: prompt_len,
            });
        }

        // Create batch for prompt processing
        let mut batch = LlamaBatch::new(self.context_size, 1);

        // Add prompt tokens to batch
        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| {
                LlmError::ModelError(format!("Failed to add token to batch: {}", e))
            })?;
        }

        // Decode prompt
        ctx.decode(&mut batch)
            .map_err(|e| LlmError::ModelError(format!("Failed to decode prompt: {}", e)))?;

        // Set up sampler chain with temperature, top-k, top-p
        let sampler_params = LlamaSamplerChainParams::default();
        let mut sampler = LlamaSampler::chain(sampler_params);

        // Add sampling stages
        sampler.add_temp(self.temperature);
        sampler.add_top_k(self.top_k);
        sampler.add_top_p(self.top_p, 1);
        sampler.add_dist(42); // Random seed

        // Generate tokens
        let mut output_tokens: Vec<LlamaToken> = Vec::new();
        let mut pos = prompt_len;
        let max_tokens = self.max_gen_tokens.min(self.context_size - prompt_len);

        tracing::debug!(max_tokens, "Starting generation");

        for _ in 0..max_tokens {
            // Sample next token
            let token = sampler.sample(&ctx, -1);
            sampler.accept(token);

            // Check for end of generation
            if loaded.model.is_eog_token(token) {
                tracing::debug!("End of generation token reached");
                break;
            }

            output_tokens.push(token);

            // Prepare batch for next token
            batch.clear();
            batch.add(token, pos as i32, &[0], true).map_err(|e| {
                LlmError::ModelError(format!("Failed to add generated token to batch: {}", e))
            })?;

            // Decode next token
            ctx.decode(&mut batch).map_err(|e| {
                LlmError::ModelError(format!("Failed to decode generated token: {}", e))
            })?;

            pos += 1;
        }

        // Convert output tokens to string
        let output = loaded
            .model
            .tokens_to_str(&output_tokens, Special::Tokenize)
            .map_err(|e| LlmError::ModelError(format!("Failed to decode output tokens: {}", e)))?;

        tracing::debug!(
            output_tokens = output_tokens.len(),
            output_len = output.len(),
            "Generation complete"
        );

        Ok(output)
    }
}

#[cfg(feature = "llm-offline")]
#[async_trait]
impl LlmClient for LlamaCppClient {
    async fn complete(&self, prompt: &str) -> LlmResult<String> {
        // llama.cpp inference is blocking, so we run it in a blocking task
        let client = self.clone();
        let prompt = prompt.to_string();

        tokio::task::spawn_blocking(move || client.generate_completion(&prompt))
            .await
            .map_err(|e| LlmError::ModelError(format!("Task join error: {}", e)))?
    }

    fn model_name(&self) -> &str {
        &self.model_name
    }

    fn max_tokens(&self) -> usize {
        self.context_size
    }

    async fn is_ready(&self) -> bool {
        self.ensure_loaded().is_ok()
    }
}

#[cfg(feature = "llm-offline")]
impl Clone for LlamaCppClient {
    fn clone(&self) -> Self {
        Self {
            model_path: self.model_path.clone(),
            model_name: self.model_name.clone(),
            gpu_layers: self.gpu_layers,
            context_size: self.context_size,
            temperature: self.temperature,
            top_p: self.top_p,
            top_k: self.top_k,
            repeat_penalty: self.repeat_penalty,
            max_gen_tokens: self.max_gen_tokens,
            inner: Arc::clone(&self.inner),
        }
    }
}

#[cfg(not(feature = "llm-offline"))]
#[async_trait]
impl LlmClient for LlamaCppClient {
    async fn complete(&self, _prompt: &str) -> LlmResult<String> {
        Err(LlmError::FeatureNotAvailable(
            "Offline LLM".to_string(),
            "llm-offline".to_string(),
        ))
    }

    fn model_name(&self) -> &str {
        &self.model_name
    }

    fn max_tokens(&self) -> usize {
        self.context_size
    }

    async fn is_ready(&self) -> bool {
        false
    }
}

#[cfg(not(feature = "llm-offline"))]
impl Clone for LlamaCppClient {
    fn clone(&self) -> Self {
        Self {
            model_path: self.model_path.clone(),
            model_name: self.model_name.clone(),
            gpu_layers: self.gpu_layers,
            context_size: self.context_size,
            temperature: self.temperature,
            top_p: self.top_p,
            top_k: self.top_k,
            repeat_penalty: self.repeat_penalty,
            max_gen_tokens: self.max_gen_tokens,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_llamacpp_client_new_nonexistent() {
        let result = LlamaCppClient::new("/nonexistent/model.gguf");
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), LlmError::ModelError(_)));
    }

    #[test]
    fn test_llamacpp_client_new_wrong_extension() {
        let mut temp_file = NamedTempFile::with_suffix(".bin").unwrap();
        writeln!(temp_file, "fake model data").unwrap();

        let result = LlamaCppClient::new(temp_file.path());
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, LlmError::ModelError(_)));
        if let LlmError::ModelError(msg) = err {
            assert!(msg.contains("GGUF"));
        }
    }

    #[test]
    fn test_llamacpp_client_new_exists() {
        // Create a temporary file to simulate a model
        let mut temp_file = NamedTempFile::with_suffix(".gguf").unwrap();
        writeln!(temp_file, "fake model data").unwrap();

        let client = LlamaCppClient::new(temp_file.path()).unwrap();
        assert!(client.model_path().exists());
        assert!(!client.model_name.is_empty());
        assert_eq!(client.gpu_layers, 0);
        assert_eq!(client.context_size, 4096);
    }

    #[test]
    fn test_llamacpp_client_builder() {
        let mut temp_file = NamedTempFile::with_suffix(".gguf").unwrap();
        writeln!(temp_file, "fake model data").unwrap();

        let client = LlamaCppClient::new(temp_file.path())
            .unwrap()
            .with_gpu_layers(35)
            .with_context_size(8192)
            .with_temperature(0.3)
            .with_top_p(0.95)
            .with_top_k(50)
            .with_repeat_penalty(1.2)
            .with_max_gen_tokens(4096);

        assert_eq!(client.gpu_layers, 35);
        assert_eq!(client.context_size, 8192);
        assert!((client.temperature - 0.3).abs() < f32::EPSILON);
        assert!((client.top_p - 0.95).abs() < f32::EPSILON);
        assert_eq!(client.top_k, 50);
        assert!((client.repeat_penalty - 1.2).abs() < f32::EPSILON);
        assert_eq!(client.max_gen_tokens, 4096);
        assert!(client.uses_gpu());
    }

    #[test]
    fn test_parameter_clamping() {
        let mut temp_file = NamedTempFile::with_suffix(".gguf").unwrap();
        writeln!(temp_file, "fake model data").unwrap();

        let client = LlamaCppClient::new(temp_file.path())
            .unwrap()
            .with_temperature(5.0)
            .with_top_p(1.5)
            .with_top_k(0)
            .with_repeat_penalty(0.5);

        assert!((client.temperature - 2.0).abs() < f32::EPSILON);
        assert!((client.top_p - 1.0).abs() < f32::EPSILON);
        assert_eq!(client.top_k, 1); // Minimum of 1
        assert!((client.repeat_penalty - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_model_name_extraction() {
        let mut temp_file = NamedTempFile::with_suffix(".gguf").unwrap();
        writeln!(temp_file, "fake model data").unwrap();

        let client = LlamaCppClient::new(temp_file.path()).unwrap();
        // The model name should be derived from the temp file name (without .gguf)
        assert!(!client.model_name.is_empty());
        assert!(!client.model_name.contains(".gguf"));
    }

    #[test]
    fn test_clone() {
        let mut temp_file = NamedTempFile::with_suffix(".gguf").unwrap();
        writeln!(temp_file, "fake model data").unwrap();

        let client = LlamaCppClient::new(temp_file.path())
            .unwrap()
            .with_gpu_layers(10)
            .with_context_size(2048);

        let cloned = client.clone();
        assert_eq!(cloned.model_path, client.model_path);
        assert_eq!(cloned.gpu_layers, client.gpu_layers);
        assert_eq!(cloned.context_size, client.context_size);
    }
}