03_with_memory/
03_with_memory.rs

1//! Agent with Memory Example
2//!
3//! This example demonstrates how to use the agent's memory system to maintain
4//! conversation history across multiple interactions.
5//!
6//! Run with: cargo run --example 03_with_memory --manifest-path ceylon/Cargo.toml
7
8use ceylon_next::agent::Agent;
9use ceylon_next::tasks::{OutputData, TaskRequest};
10
11#[tokio::main]
12async fn main() {
13    println!("๐Ÿค– Ceylon Agent - Memory Example\n");
14
15    // Step 1: Create an agent
16    let mut agent = Agent::new("MemoryAssistant", "ollama::gemma3:latest");
17
18    agent.with_system_prompt(
19        "You are a helpful assistant with persistent memory. \
20         Remember important details from our previous conversations \
21         and refer back to them when relevant."
22    );
23
24    // Step 2: Run multiple tasks to build conversation history
25    let tasks_and_prompts = vec![
26        ("Tell me about your favorite color", "Learn about preferences"),
27        ("What color did I mention?", "Test memory recall"),
28        ("Can you suggest a hobby based on what you know about me?", "Use memory for suggestions"),
29    ];
30
31    for (prompt, description) in tasks_and_prompts {
32        println!("โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”");
33        println!("๐Ÿ“‹ Task: {}", prompt);
34        println!("๐Ÿ“ Description: {}", description);
35        println!("โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”\n");
36
37        let mut task = TaskRequest::new(prompt);
38        task.with_name(description)
39            .with_priority(5);
40
41        // Run the agent
42        let response = agent.run(task).await;
43
44        // Display response
45        match response.result() {
46            OutputData::Text(answer) => {
47                println!("โœ“ Response received\n");
48            }
49            _ => {
50                println!("โŒ Unexpected response type\n");
51            }
52        }
53    }
54
55    // Step 3: Retrieve conversation history
56    println!("โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”");
57    println!("๐Ÿ“š Retrieving conversation history...\n");
58
59    let history = agent.get_history(Some(5)).await;
60    println!("Found {} recent conversations\n", history.len());
61
62    if !history.is_empty() {
63        println!("๐Ÿ“– Conversation Summary:");
64        for (i, entry) in history.iter().enumerate() {
65            println!("\n--- Conversation {} ---", i + 1);
66            println!("Messages: {}", entry.messages.len());
67
68            // Show first and last message
69            if let Some(first) = entry.messages.first() {
70                println!("First: {} - {}", first.role,
71                    if first.content.len() > 50 {
72                        format!("{}...", &first.content[..50])
73                    } else {
74                        first.content.clone()
75                    });
76            }
77
78            if let Some(last) = entry.messages.last() {
79                println!("Last: {} - {}", last.role,
80                    if last.content.len() > 50 {
81                        format!("{}...", &last.content[..50])
82                    } else {
83                        last.content.clone()
84                    });
85            }
86        }
87    }
88
89    // Step 4: Search memory
90    println!("\nโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”");
91    println!("๐Ÿ” Searching memory for 'color'...\n");
92
93    let search_results = agent.search_memory("color").await;
94    println!("Found {} conversations mentioning 'color'\n", search_results.len());
95
96    // Step 5: Clear memory (optional)
97    println!("โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”");
98    println!("๐Ÿงน Memory management options:");
99    println!("- Use agent.get_history() to retrieve conversation history");
100    println!("- Use agent.search_memory(query) to search conversations");
101    println!("- Use agent.clear_memory() to clear all agent memory\n");
102
103    println!("โœ… Example completed successfully!");
104}