memvid-cli 2.0.140

Command-line interface for Memvid v2 - AI memory with crash-safe, single-file storage
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
//! OpenAI-based enrichment engine using GPT-4o-mini.
//!
//! This engine uses the OpenAI API to extract structured memory cards
//! from text content. Supports parallel batch processing for speed.

use anyhow::{anyhow, Result};
use memvid_core::enrich::{EnrichmentContext, EnrichmentEngine, EnrichmentResult};
use memvid_core::types::{MemoryCard, MemoryCardBuilder, MemoryKind, Polarity};
use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator};
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;
use tracing::{debug, info, warn};

/// The extraction prompt for GPT-4o-mini (single frame)
const EXTRACTION_PROMPT: &str = r#"You are a memory extraction assistant. Extract structured facts from the text.

For each distinct fact, preference, event, or relationship mentioned, output a memory card in this exact format:
MEMORY_START
kind: <Fact|Preference|Event|Profile|Relationship|Other>
entity: <the main entity this memory is about, use "user" for the human in the conversation>
slot: <a short key describing what aspect of the entity>
value: <the actual information>
polarity: <Positive|Negative|Neutral>
MEMORY_END

Only extract information that is explicitly stated. Do not infer or guess.
If there are no clear facts to extract, output MEMORY_NONE.

Extract memories from this text:
"#;

/// The extraction prompt for batched frames (multiple frames per API call)
const BATCH_EXTRACTION_PROMPT: &str = r#"You are a memory extraction assistant. Extract structured facts from multiple text blocks.

Each text block is labeled with a FRAME_ID. For each distinct fact in each block, output a memory card with the frame_id field:

MEMORY_START
frame_id: <the FRAME_ID of the source text>
kind: <Fact|Preference|Event|Profile|Relationship|Other>
entity: <the main entity this memory is about, use "user" for the human in the conversation>
slot: <a short key describing what aspect of the entity>
value: <the actual information>
polarity: <Positive|Negative|Neutral>
MEMORY_END

Only extract information that is explicitly stated. Do not infer or guess.
If a text block has no facts, output MEMORY_NONE with its frame_id.

Process these text blocks:
"#;

/// OpenAI API request message
#[derive(Debug, Serialize, Clone)]
struct ChatMessage {
    role: String,
    content: String,
}

/// OpenAI API request
#[derive(Debug, Serialize)]
struct ChatRequest {
    model: String,
    messages: Vec<ChatMessage>,
    max_tokens: u32,
    temperature: f32,
}

/// OpenAI API response
#[derive(Debug, Deserialize)]
struct ChatResponse {
    choices: Vec<ChatChoice>,
}

#[derive(Debug, Deserialize)]
struct ChatChoice {
    message: ChatMessageResponse,
}

#[derive(Debug, Deserialize)]
struct ChatMessageResponse {
    content: String,
}

/// OpenAI enrichment engine using GPT-4o-mini with parallel processing.
pub struct OpenAiEngine {
    /// API key
    api_key: String,
    /// Model to use
    model: String,
    /// Whether the engine is initialized
    ready: bool,
    /// Number of parallel workers (default: 100)
    parallelism: usize,
    /// Number of frames to batch per API call (default: 10)
    batch_size: usize,
    /// Shared HTTP client (built in `init`)
    client: Option<Client>,
}

impl OpenAiEngine {
    /// Create a new OpenAI engine.
    pub fn new() -> Self {
        let api_key = std::env::var("OPENAI_API_KEY").unwrap_or_default();
        Self {
            api_key,
            model: "gpt-4o-mini".to_string(),
            ready: false,
            parallelism: 20, // 20 concurrent requests (balanced for API rate limits)
            batch_size: 10,  // 10 frames per API call
            client: None,
        }
    }

    /// Create with a specific model.
    pub fn with_model(model: &str) -> Self {
        let api_key = std::env::var("OPENAI_API_KEY").unwrap_or_default();
        Self {
            api_key,
            model: model.to_string(),
            ready: false,
            parallelism: 20,
            batch_size: 10,
            client: None,
        }
    }

    /// Set parallelism level.
    pub fn with_parallelism(mut self, n: usize) -> Self {
        self.parallelism = n;
        self
    }

    /// Set batch size (number of frames per API call).
    pub fn with_batch_size(mut self, n: usize) -> Self {
        self.batch_size = n.max(1); // At least 1
        self
    }

    /// Run inference via OpenAI API (blocking, thread-safe).
    fn run_inference_blocking(
        client: &Client,
        api_key: &str,
        model: &str,
        text: &str,
    ) -> Result<String> {
        let prompt = format!("{}\n\n{}", EXTRACTION_PROMPT, text);

        let request = ChatRequest {
            model: model.to_string(),
            messages: vec![ChatMessage {
                role: "user".to_string(),
                content: prompt,
            }],
            max_tokens: 1024,
            temperature: 0.0,
        };

        let response = client
            .post("https://api.openai.com/v1/chat/completions")
            .header("Authorization", format!("Bearer {}", api_key))
            .header("Content-Type", "application/json")
            .json(&request)
            .send()
            .map_err(|e| anyhow!("OpenAI API request failed: {}", e))?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().unwrap_or_default();
            return Err(anyhow!("OpenAI API error {}: {}", status, body));
        }

        let chat_response: ChatResponse = response
            .json()
            .map_err(|e| anyhow!("Failed to parse OpenAI response: {}", e))?;

        chat_response
            .choices
            .first()
            .map(|c| c.message.content.clone())
            .ok_or_else(|| anyhow!("No response from OpenAI"))
    }

    /// Parse the LLM output into memory cards.
    fn parse_output(output: &str, frame_id: u64, uri: &str, timestamp: i64) -> Vec<MemoryCard> {
        let mut cards = Vec::new();

        // Check for "no memories" signal
        if output.contains("MEMORY_NONE") {
            return cards;
        }

        // Parse MEMORY_START...MEMORY_END blocks
        for block in output.split("MEMORY_START") {
            let block = block.trim();
            if block.is_empty() || !block.contains("MEMORY_END") {
                continue;
            }

            let block = block.split("MEMORY_END").next().unwrap_or("").trim();

            // Parse fields
            let mut kind = None;
            let mut entity = None;
            let mut slot = None;
            let mut value = None;
            let mut polarity = Polarity::Neutral;

            for line in block.lines() {
                let line = line.trim();
                if let Some(rest) = line.strip_prefix("kind:") {
                    kind = parse_memory_kind(rest.trim());
                } else if let Some(rest) = line.strip_prefix("entity:") {
                    entity = Some(rest.trim().to_string());
                } else if let Some(rest) = line.strip_prefix("slot:") {
                    slot = Some(rest.trim().to_string());
                } else if let Some(rest) = line.strip_prefix("value:") {
                    value = Some(rest.trim().to_string());
                } else if let Some(rest) = line.strip_prefix("polarity:") {
                    polarity = parse_polarity(rest.trim());
                }
            }

            // Build memory card if we have required fields
            if let (Some(k), Some(e), Some(s), Some(v)) = (kind, entity, slot, value) {
                if !e.is_empty() && !s.is_empty() && !v.is_empty() {
                    match MemoryCardBuilder::new()
                        .kind(k)
                        .entity(&e)
                        .slot(&s)
                        .value(&v)
                        .polarity(polarity)
                        .source(frame_id, Some(uri.to_string()))
                        .document_date(timestamp)
                        .engine("openai:gpt-4o-mini", "1.0.0")
                        .build(0)
                    {
                        Ok(card) => cards.push(card),
                        Err(err) => {
                            warn!("Failed to build memory card: {}", err);
                        }
                    }
                }
            }
        }

        cards
    }

    /// Run batched inference for multiple frames in a single API call.
    fn run_batched_inference_blocking(
        client: &Client,
        api_key: &str,
        model: &str,
        contexts: &[&EnrichmentContext],
    ) -> Result<String> {
        // Build the batched prompt with frame markers
        let mut prompt = BATCH_EXTRACTION_PROMPT.to_string();
        for ctx in contexts {
            prompt.push_str(&format!(
                "\n\n=== FRAME_ID: {} ===\n{}",
                ctx.frame_id, ctx.text
            ));
        }

        // Use larger max_tokens for batched requests
        let max_tokens = 1024 + (contexts.len() as u32 * 512);

        let request = ChatRequest {
            model: model.to_string(),
            messages: vec![ChatMessage {
                role: "user".to_string(),
                content: prompt,
            }],
            max_tokens: max_tokens.min(4096), // Cap at 4096
            temperature: 0.0,
        };

        let response = client
            .post("https://api.openai.com/v1/chat/completions")
            .header("Authorization", format!("Bearer {}", api_key))
            .header("Content-Type", "application/json")
            .json(&request)
            .send()
            .map_err(|e| anyhow!("OpenAI API request failed: {}", e))?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().unwrap_or_default();
            return Err(anyhow!("OpenAI API error {}: {}", status, body));
        }

        let chat_response: ChatResponse = response
            .json()
            .map_err(|e| anyhow!("Failed to parse OpenAI response: {}", e))?;

        chat_response
            .choices
            .first()
            .map(|c| c.message.content.clone())
            .ok_or_else(|| anyhow!("No response from OpenAI"))
    }

    /// Parse batched LLM output into memory cards grouped by frame_id.
    fn parse_batched_output(
        output: &str,
        contexts: &[&EnrichmentContext],
    ) -> std::collections::HashMap<u64, Vec<MemoryCard>> {
        let mut results: std::collections::HashMap<u64, Vec<MemoryCard>> =
            std::collections::HashMap::new();

        // Initialize empty results for all frames
        for ctx in contexts {
            results.insert(ctx.frame_id, Vec::new());
        }

        // Build a lookup for context metadata
        let ctx_lookup: std::collections::HashMap<u64, &EnrichmentContext> =
            contexts.iter().map(|c| (c.frame_id, *c)).collect();

        // Parse MEMORY_START...MEMORY_END blocks
        for block in output.split("MEMORY_START") {
            let block = block.trim();
            if block.is_empty() || !block.contains("MEMORY_END") {
                continue;
            }

            let block = block.split("MEMORY_END").next().unwrap_or("").trim();

            // Parse fields including frame_id
            let mut frame_id: Option<u64> = None;
            let mut kind = None;
            let mut entity = None;
            let mut slot = None;
            let mut value = None;
            let mut polarity = Polarity::Neutral;

            for line in block.lines() {
                let line = line.trim();
                if let Some(rest) = line.strip_prefix("frame_id:") {
                    frame_id = rest.trim().parse().ok();
                } else if let Some(rest) = line.strip_prefix("kind:") {
                    kind = parse_memory_kind(rest.trim());
                } else if let Some(rest) = line.strip_prefix("entity:") {
                    entity = Some(rest.trim().to_string());
                } else if let Some(rest) = line.strip_prefix("slot:") {
                    slot = Some(rest.trim().to_string());
                } else if let Some(rest) = line.strip_prefix("value:") {
                    value = Some(rest.trim().to_string());
                } else if let Some(rest) = line.strip_prefix("polarity:") {
                    polarity = parse_polarity(rest.trim());
                }
            }

            // Build memory card if we have required fields
            if let (Some(fid), Some(k), Some(e), Some(s), Some(v)) =
                (frame_id, kind, entity, slot, value)
            {
                if let Some(ctx) = ctx_lookup.get(&fid) {
                    let uri = &ctx.uri;
                    let timestamp = ctx.timestamp;

                    if !e.is_empty() && !s.is_empty() && !v.is_empty() {
                        match MemoryCardBuilder::new()
                            .kind(k)
                            .entity(&e)
                            .slot(&s)
                            .value(&v)
                            .polarity(polarity)
                            .source(fid, Some(uri.to_string()))
                            .document_date(timestamp)
                            .engine("openai:gpt-4o-mini", "1.0.0")
                            .build(0)
                        {
                            Ok(card) => {
                                results.entry(fid).or_default().push(card);
                            }
                            Err(err) => {
                                warn!("Failed to build memory card: {}", err);
                            }
                        }
                    }
                }
            }
        }

        results
    }

    /// Process multiple frames in parallel and return all cards.
    /// This is the key method for fast enrichment.
    /// Uses batching to reduce API calls: batch_size frames per API call.
    pub fn enrich_batch(
        &self,
        contexts: Vec<EnrichmentContext>,
    ) -> Result<Vec<(u64, Vec<MemoryCard>)>> {
        let client = self
            .client
            .as_ref()
            .ok_or_else(|| anyhow!("OpenAI engine not initialized (init() not called)"))?
            .clone();
        let client = Arc::new(client);
        let api_key = Arc::new(self.api_key.clone());
        let model = Arc::new(self.model.clone());
        let total = contexts.len();
        let batch_size = self.batch_size;

        // Calculate number of batches
        let num_batches = (total + batch_size - 1) / batch_size;

        info!(
            "Starting parallel enrichment of {} frames with {} workers, {} frames per batch ({} batches)",
            total, self.parallelism, batch_size, num_batches
        );

        // Create batches of contexts
        let batches: Vec<Vec<EnrichmentContext>> = contexts
            .into_iter()
            .collect::<Vec<_>>()
            .chunks(batch_size)
            .map(|chunk| chunk.to_vec())
            .collect();

        // Use rayon for parallel processing of batches
        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(self.parallelism)
            .build()
            .map_err(|err| anyhow!("failed to build enrichment thread pool: {err}"))?;

        let batch_results: Vec<std::collections::HashMap<u64, Vec<MemoryCard>>> =
            pool.install(|| {
                batches
                    .into_par_iter()
                    .enumerate()
                    .map(|(batch_idx, batch)| {
                        // Filter out empty texts
                        let non_empty: Vec<&EnrichmentContext> =
                            batch.iter().filter(|ctx| !ctx.text.is_empty()).collect();

                        if non_empty.is_empty() {
                            // Return empty results for all frames in this batch
                            return batch.iter().map(|ctx| (ctx.frame_id, Vec::new())).collect();
                        }

                        // Progress logging every 10 batches
                        if batch_idx > 0 && batch_idx % 10 == 0 {
                            info!("Enrichment progress: {} batches processed", batch_idx);
                        }

                        match Self::run_batched_inference_blocking(
                            &client, &api_key, &model, &non_empty,
                        ) {
                            Ok(output) => {
                                debug!(
                                    "OpenAI batch output (batch {}): {}...",
                                    batch_idx,
                                    &output[..output.len().min(100)]
                                );
                                Self::parse_batched_output(&output, &non_empty)
                            }
                            Err(err) => {
                                warn!(
                                    "OpenAI batch inference failed (batch {}): {}",
                                    batch_idx, err
                                );
                                // Return empty results for all frames in this batch
                                batch.iter().map(|ctx| (ctx.frame_id, Vec::new())).collect()
                            }
                        }
                    })
                    .collect()
            });

        // Flatten batch results into a single vec
        let mut results: Vec<(u64, Vec<MemoryCard>)> = Vec::with_capacity(total);
        for batch_map in batch_results {
            for (frame_id, cards) in batch_map {
                results.push((frame_id, cards));
            }
        }

        info!(
            "Parallel enrichment complete: {} frames processed in {} batches",
            results.len(),
            num_batches
        );
        Ok(results)
    }
}

/// Parse a memory kind string into the enum.
fn parse_memory_kind(s: &str) -> Option<MemoryKind> {
    match s.to_lowercase().as_str() {
        "fact" => Some(MemoryKind::Fact),
        "preference" => Some(MemoryKind::Preference),
        "event" => Some(MemoryKind::Event),
        "profile" => Some(MemoryKind::Profile),
        "relationship" => Some(MemoryKind::Relationship),
        "other" => Some(MemoryKind::Other),
        _ => None,
    }
}

/// Parse a polarity string into the enum.
fn parse_polarity(s: &str) -> Polarity {
    match s.to_lowercase().as_str() {
        "positive" => Polarity::Positive,
        "negative" => Polarity::Negative,
        _ => Polarity::Neutral,
    }
}

impl EnrichmentEngine for OpenAiEngine {
    fn kind(&self) -> &str {
        "openai:gpt-4o-mini"
    }

    fn version(&self) -> &str {
        "1.0.0"
    }

    fn init(&mut self) -> memvid_core::Result<()> {
        if self.api_key.is_empty() {
            return Err(memvid_core::MemvidError::EmbeddingFailed {
                reason: "OPENAI_API_KEY environment variable not set".into(),
            });
        }
        // Use longer timeout for batched requests (multiple frames per call)
        let client = crate::http::blocking_client(Duration::from_secs(120)).map_err(|err| {
            memvid_core::MemvidError::EmbeddingFailed {
                reason: format!("Failed to create OpenAI HTTP client: {err}").into(),
            }
        })?;
        self.client = Some(client);
        self.ready = true;
        Ok(())
    }

    fn is_ready(&self) -> bool {
        self.ready
    }

    fn enrich(&self, ctx: &EnrichmentContext) -> EnrichmentResult {
        if ctx.text.is_empty() {
            return EnrichmentResult::empty();
        }

        let client = match self.client.as_ref() {
            Some(client) => client,
            None => {
                return EnrichmentResult::failed(
                    "OpenAI engine not initialized (init() not called)".to_string(),
                )
            }
        };

        match Self::run_inference_blocking(client, &self.api_key, &self.model, &ctx.text) {
            Ok(output) => {
                debug!("OpenAI output for frame {}: {}", ctx.frame_id, output);
                let cards = Self::parse_output(&output, ctx.frame_id, &ctx.uri, ctx.timestamp);
                EnrichmentResult::success(cards)
            }
            Err(err) => EnrichmentResult::failed(format!("OpenAI inference failed: {}", err)),
        }
    }
}

impl Default for OpenAiEngine {
    fn default() -> Self {
        Self::new()
    }
}