mem0_rust/memory/
prompts.rs

1//! LLM prompts for memory operations.
2
3/// System prompt for fact extraction
4pub const FACT_EXTRACTION_PROMPT: &str = r#"You are a Personal Information Organizer, specialized in accurately storing facts, user memories, and preferences.
5
6Your task is to extract relevant facts, user preferences, and personal information from the given conversation and organize them into distinct, manageable facts.
7
8Guidelines:
91. Extract only facts, preferences, and personal information explicitly mentioned
102. Each fact should be atomic (contain one piece of information)
113. Use first person (I, me, my) when storing user information
124. Use third person (user, they, their) when storing observations about the user
135. Be concise but complete
146. Don't make assumptions beyond what's stated
157. Don't include temporary or context-specific information
16
17Return a JSON object with a "facts" array containing the extracted facts as strings.
18
19Example response format:
20{
21  "facts": [
22    "I prefer dark mode",
23    "My favorite programming language is Rust",
24    "I work as a software engineer"
25  ]
26}
27
28If no relevant facts are found, return:
29{
30  "facts": []
31}"#;
32
33/// System prompt for memory updates
34pub const MEMORY_UPDATE_PROMPT: &str = r#"You are a memory management system. Your task is to analyze new facts and existing memories to determine the appropriate action for each new fact.
35
36For each new fact, you must decide:
371. ADD - Add as a new memory (no similar existing memory)
382. UPDATE - Update an existing memory with new/corrected information
393. DELETE - Mark an existing memory for deletion (contradicted or outdated)
404. NOOP - No action needed (duplicate or already captured)
41
42Guidelines:
43- Compare each new fact with existing memories for semantic similarity
44- If updating, merge information appropriately
45- Preserve important historical context when updating
46- Only delete if clearly contradicted
47
48Return a JSON object with a "memory" array, where each item has:
49- "event": "ADD" | "UPDATE" | "DELETE" | "NOOP"
50- "text": the memory text (for ADD/UPDATE)
51- "id": the existing memory ID (for UPDATE/DELETE, as a string number)
52
53Example:
54{
55  "memory": [
56    {"event": "ADD", "text": "User prefers dark mode"},
57    {"event": "UPDATE", "id": "2", "text": "User works at Google as a senior engineer"},
58    {"event": "DELETE", "id": "5"}
59  ]
60}"#;
61
62/// Format messages for fact extraction
63pub fn format_fact_extraction_input(messages: &str) -> String {
64    format!(
65        "Extract facts from the following conversation:\n\n{}",
66        messages
67    )
68}
69
70/// Format messages for memory update
71pub fn format_memory_update_input(
72    existing_memories: &[(String, String)], // (id, text)
73    new_facts: &[String],
74) -> String {
75    let mut prompt = String::new();
76
77    prompt.push_str("Existing memories:\n");
78    if existing_memories.is_empty() {
79        prompt.push_str("None\n");
80    } else {
81        for (id, text) in existing_memories {
82            prompt.push_str(&format!("[{}] {}\n", id, text));
83        }
84    }
85
86    prompt.push_str("\nNew facts to process:\n");
87    for fact in new_facts {
88        prompt.push_str(&format!("- {}\n", fact));
89    }
90
91    prompt
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[test]
99    fn test_format_fact_extraction() {
100        let input = format_fact_extraction_input("Hello, I like pizza");
101        assert!(input.contains("pizza"));
102    }
103
104    #[test]
105    fn test_format_memory_update() {
106        let existing = vec![("0".to_string(), "User likes coffee".to_string())];
107        let new_facts = vec!["User also likes tea".to_string()];
108
109        let output = format_memory_update_input(&existing, &new_facts);
110        assert!(output.contains("[0] User likes coffee"));
111        assert!(output.contains("User also likes tea"));
112    }
113}