mnemefusion-core 0.1.4

Unified memory engine for AI applications - Core library
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
use super::config::SlmConfig;
/// SLM-based intent classifier using llama.cpp via Python subprocess
///
/// This module implements semantic intent classification using a Small Language Model
/// (Qwen 2.5 0.5B) via llama-cpp-python. It provides significant accuracy improvements
/// over pattern-based classification:
///
/// - Entity queries: 11% → 85%+ accuracy
/// - Causal queries: 30% → 85%+ accuracy
/// - Overall: 35% → 85%+ accuracy
///
/// The classifier integrates with QueryPlanner and falls back to pattern-based
/// classification on any error, ensuring zero regression.
use crate::error::{Error, Result};
use crate::query::intent::IntentClassification;

#[cfg(feature = "slm")]
use std::io::{BufRead, BufReader, Write};
#[cfg(feature = "slm")]
use std::path::PathBuf;
#[cfg(feature = "slm")]
use std::process::{Child, Command, Stdio};

/// SLM-based intent classifier
///
/// Uses Qwen 2.5 0.5B model via persistent Python server for semantic understanding of query intent.
/// The Python process stays running with the model loaded in memory for fast inference.
/// Falls back to pattern-based classification on any error.
#[cfg(feature = "slm")]
pub struct SlmClassifier {
    config: SlmConfig,
    script_path: PathBuf,
    process: Option<Child>,
    stdin: Option<std::process::ChildStdin>,
    stdout: Option<BufReader<std::process::ChildStdout>>,
}

#[cfg(feature = "slm")]
impl SlmClassifier {
    /// Create a new SLM classifier
    ///
    /// Spawns a persistent Python server process that loads the model once and keeps it in memory.
    ///
    /// # Arguments
    ///
    /// * `config` - SLM configuration including model path
    ///
    /// # Returns
    ///
    /// Returns a new classifier instance with running Python server.
    pub fn new(config: SlmConfig) -> Result<Self> {
        tracing::info!(
            "Initializing SLM classifier with model: {}",
            config.model_id
        );

        // Find the Python server script
        let script_path = Self::find_script_path()?;

        tracing::info!("Using SLM classification script: {}", script_path.display());

        // Get model path
        let mut classifier = Self {
            config: config.clone(),
            script_path,
            process: None,
            stdin: None,
            stdout: None,
        };

        let model_path = classifier.get_model_path()?;

        // Spawn Python server process
        let mut child = Command::new("python")
            .arg(&classifier.script_path)
            .arg(model_path.to_string_lossy().as_ref())
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::inherit()) // Show server logs in stderr
            .spawn()
            .map_err(|e| {
                Error::SlmInitialization(format!("Failed to spawn Python server: {}", e))
            })?;

        // Get handles
        let stdin = child
            .stdin
            .take()
            .ok_or_else(|| Error::SlmInitialization("Failed to get stdin handle".to_string()))?;
        let stdout = child
            .stdout
            .take()
            .ok_or_else(|| Error::SlmInitialization("Failed to get stdout handle".to_string()))?;
        let mut stdout_reader = BufReader::new(stdout);

        // Wait for READY signal
        let mut ready_line = String::new();
        std::io::BufRead::read_line(&mut stdout_reader, &mut ready_line)
            .map_err(|e| Error::SlmInitialization(format!("Failed to read READY signal: {}", e)))?;

        if ready_line.trim() != "READY" {
            return Err(Error::SlmInitialization(format!(
                "Expected READY signal, got: {}",
                ready_line.trim()
            )));
        }

        tracing::info!("SLM server initialized and ready");

        classifier.process = Some(child);
        classifier.stdin = Some(stdin);
        classifier.stdout = Some(stdout_reader);

        Ok(classifier)
    }

    /// Find the slm_classify_server.py script
    fn find_script_path() -> Result<PathBuf> {
        // Check environment variable first
        if let Ok(script_path) = std::env::var("SLM_SCRIPT_PATH") {
            let path = PathBuf::from(script_path);
            if path.exists() {
                tracing::info!("Using SLM script from SLM_SCRIPT_PATH: {}", path.display());
                return Ok(path);
            }
        }

        // Try several possible locations
        let possible_paths = vec![
            PathBuf::from("scripts/slm_classify_server.py"),
            PathBuf::from("../scripts/slm_classify_server.py"),
            PathBuf::from("../../scripts/slm_classify_server.py"),
            PathBuf::from("../../../scripts/slm_classify_server.py"), // For deeper test directories
        ];

        for path in possible_paths {
            if path.exists() {
                tracing::info!("Found SLM script at: {}", path.display());
                return Ok(path);
            } else {
                tracing::debug!("Script not found at: {}", path.display());
            }
        }

        Err(Error::SlmInitialization(
            "Could not find scripts/slm_classify_server.py. Make sure it exists in the project root, or set SLM_SCRIPT_PATH environment variable.".to_string()
        ))
    }

    /// Get the GGUF model path
    ///
    /// Returns the path to the .gguf file, either from explicit config
    /// or derived from model_id.
    fn get_model_path(&self) -> Result<PathBuf> {
        if let Some(model_path) = &self.config.model_path {
            // Check if it's a directory or file
            if model_path.is_dir() {
                // Look for .gguf file in directory
                let gguf_files: Vec<_> = std::fs::read_dir(model_path)
                    .map_err(|e| {
                        Error::SlmInitialization(format!("Failed to read model directory: {}", e))
                    })?
                    .filter_map(|e| e.ok())
                    .map(|e| e.path())
                    .filter(|p| p.extension().and_then(|s| s.to_str()) == Some("gguf"))
                    .collect();

                if gguf_files.is_empty() {
                    return Err(Error::SlmInitialization(format!(
                        "No .gguf file found in directory: {}\n\
                         Please ensure the model has been converted to GGUF format.",
                        model_path.display()
                    )));
                }

                if gguf_files.len() > 1 {
                    tracing::warn!(
                        "Multiple .gguf files found, using first: {:?}",
                        gguf_files[0]
                    );
                }

                Ok(gguf_files[0].clone())
            } else {
                // Direct path to .gguf file
                if !model_path.exists() {
                    return Err(Error::SlmInitialization(format!(
                        "Model file not found: {}",
                        model_path.display()
                    )));
                }
                Ok(model_path.clone())
            }
        } else {
            // Try to derive path from model_id
            // e.g., "Qwen/Qwen2.5-0.5B-Instruct" -> "opt/models/qwen2.5-0.5b-instruct.gguf"
            let model_filename = self
                .config
                .model_id
                .split('/')
                .last()
                .unwrap_or(&self.config.model_id)
                .to_lowercase()
                .replace('.', "-")
                .replace("_", "-");

            // Try both opt/models and cache_dir
            let possible_paths = vec![
                PathBuf::from(format!("opt/models/{}.gguf", model_filename)),
                self.config
                    .cache_dir
                    .join(format!("{}.gguf", model_filename)),
            ];

            for path in possible_paths {
                if path.exists() {
                    return Ok(path);
                }
            }

            Err(Error::SlmInitialization(format!(
                "Model file not found for: {}\n\
                 Tried: opt/models/{}.gguf\n\
                 Please either:\n\
                 1. Use config.with_model_path(\"/path/to/model.gguf\"), or\n\
                 2. Place the GGUF file at opt/models/{}.gguf",
                self.config.model_id, model_filename, model_filename
            )))
        }
    }

    /// Classify query intent using SLM via persistent Python server
    ///
    /// # Arguments
    ///
    /// * `query` - The query text to classify
    ///
    /// # Returns
    ///
    /// Returns structured intent classification with confidence score.
    /// On any error, the caller should fall back to pattern-based classification.
    ///
    /// # Errors
    ///
    /// Returns error if communication with server or parsing fails.
    /// Callers should catch errors and fall back to pattern classifier.
    pub fn classify_intent(&mut self, query: &str) -> Result<IntentClassification> {
        let start = std::time::Instant::now();

        tracing::debug!("Starting SLM inference for query: '{}'", query);

        // Get handles (should always be Some after successful initialization)
        let stdin = self.stdin.as_mut().ok_or_else(|| {
            Error::SlmInference("Python server not initialized (no stdin)".to_string())
        })?;
        let stdout = self.stdout.as_mut().ok_or_else(|| {
            Error::SlmInference("Python server not initialized (no stdout)".to_string())
        })?;

        // Send request as JSON
        let request = serde_json::json!({ "query": query });
        let request_str = format!("{}\n", request.to_string());

        stdin
            .write_all(request_str.as_bytes())
            .map_err(|e| Error::SlmInference(format!("Failed to write to Python server: {}", e)))?;
        stdin
            .flush()
            .map_err(|e| Error::SlmInference(format!("Failed to flush stdin: {}", e)))?;

        // Read response
        let mut response_line = String::new();
        stdout.read_line(&mut response_line).map_err(|e| {
            Error::SlmInference(format!("Failed to read from Python server: {}", e))
        })?;

        let duration = start.elapsed();
        tracing::info!("SLM inference completed in {:?}", duration);

        // Parse JSON output from Python
        self.parse_intent_output(&response_line)
    }

    /// Create classification prompt for the model
    ///
    /// Formats the query with instructions for structured JSON output.
    pub fn create_classification_prompt(&self, query: &str) -> String {
        format!(
            r#"<|im_start|>system
You are a query intent classifier. Classify queries into: Entity, Temporal, Causal, or Factual.<|im_end|>
<|im_start|>user
Classify this query: "{}"

Respond with JSON:
{{
  "intent": "Entity|Temporal|Causal|Factual",
  "confidence": 0.0-1.0,
  "entity_focus": "subject entity if intent=Entity, else null",
  "reasoning": "brief explanation"
}}<|im_end|>
<|im_start|>assistant
"#,
            query
        )
    }

    /// Parse model output into structured classification
    ///
    /// Expects JSON output from the model in the format specified by the prompt.
    pub fn parse_intent_output(&self, output: &str) -> Result<IntentClassification> {
        // Parse JSON directly (Python script already extracted it)
        let parsed: serde_json::Value = serde_json::from_str(output.trim())
            .map_err(|e| Error::SlmInference(format!("JSON parse error: {}", e)))?;

        // Extract intent
        let intent_str = parsed["intent"]
            .as_str()
            .ok_or_else(|| Error::SlmInference("Missing 'intent' field".to_string()))?;

        let intent = match intent_str {
            "Entity" => QueryIntent::Entity,
            "Temporal" => QueryIntent::Temporal,
            "Causal" => QueryIntent::Causal,
            _ => QueryIntent::Factual, // Default fallback
        };

        // Extract confidence
        let confidence = parsed["confidence"].as_f64().unwrap_or(0.5) as f32;

        // Extract entity focus if present
        let entity_focus = parsed["entity_focus"]
            .as_str()
            .filter(|s| *s != "null")
            .map(String::from);

        Ok(IntentClassification {
            intent,
            confidence,
            secondary: Vec::new(), // No secondary intents from SLM yet
            entity_focus,
        })
    }
}

#[cfg(feature = "slm")]
impl Drop for SlmClassifier {
    fn drop(&mut self) {
        // Send QUIT command to server
        if let Some(stdin) = self.stdin.as_mut() {
            let _ = stdin.write_all(b"{\"query\":\"QUIT\"}\n");
            let _ = stdin.flush();
        }

        // Wait for process to exit (with timeout)
        if let Some(mut child) = self.process.take() {
            let _ = std::thread::spawn(move || {
                let _ = child.wait();
            });
        }

        tracing::debug!("SLM classifier server shutdown");
    }
}

/// Stub implementation when SLM feature is disabled
///
/// This allows the code to compile without the SLM feature enabled.
/// In production, the QueryPlanner checks if SLM is configured before using it.
#[cfg(not(feature = "slm"))]
pub struct SlmClassifier {
    _private: (),
}

#[cfg(not(feature = "slm"))]
impl SlmClassifier {
    pub fn new(_config: SlmConfig) -> Result<Self> {
        Err(Error::SlmNotAvailable)
    }

    pub fn classify_intent(&mut self, _query: &str) -> Result<IntentClassification> {
        Err(Error::SlmNotAvailable)
    }
}

#[cfg(all(test, feature = "slm"))]
mod tests {
    use super::*;

    #[test]
    fn test_classifier_creation() {
        let config = SlmConfig::default();
        let classifier = SlmClassifier::new(config);
        assert!(classifier.is_ok());
    }

    #[test]
    fn test_prompt_generation() {
        let config = SlmConfig::default();
        let classifier = SlmClassifier::new(config).unwrap();

        let prompt = classifier.create_classification_prompt("Who was the first speaker?");
        assert!(prompt.contains("Who was the first speaker?"));
        assert!(prompt.contains("Entity|Temporal|Causal"));
        assert!(prompt.contains("JSON"));
    }

    #[test]
    fn test_parse_entity_output() {
        let config = SlmConfig::default();
        let classifier = SlmClassifier::new(config).unwrap();

        let output = r#"{
            "intent": "Entity",
            "confidence": 0.95,
            "entity_focus": "first speaker",
            "reasoning": "Query asks 'who' indicating entity focus"
        }"#;

        let result = classifier.parse_intent_output(output).unwrap();
        assert_eq!(result.intent, QueryIntent::Entity);
        assert_eq!(result.confidence, 0.95);
        assert_eq!(result.entity_focus, Some("first speaker".to_string()));
    }

    #[test]
    fn test_parse_temporal_output() {
        let config = SlmConfig::default();
        let classifier = SlmClassifier::new(config).unwrap();

        let output = r#"{
            "intent": "Temporal",
            "confidence": 0.88,
            "entity_focus": null,
            "reasoning": "Query asks about timing"
        }"#;

        let result = classifier.parse_intent_output(output).unwrap();
        assert_eq!(result.intent, QueryIntent::Temporal);
        assert_eq!(result.confidence, 0.88);
        assert_eq!(result.entity_focus, None);
    }

    #[test]
    fn test_parse_with_extra_text() {
        let config = SlmConfig::default();
        let classifier = SlmClassifier::new(config).unwrap();

        // Python script outputs clean JSON, so no extra text expected
        let output = r#"{
            "intent": "Causal",
            "confidence": 0.92,
            "entity_focus": null,
            "reasoning": "Query asks 'why'"
        }"#;

        let result = classifier.parse_intent_output(output);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().intent, QueryIntent::Causal);
    }
}