03_with_memory/
03_with_memory.rs1use 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 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 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 let response = agent.run(task).await;
43
44 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 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 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 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 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}