agent_with_memory_db/
agent_with_memory_db.rs

1//! # Example: Agent with MemoryDB Tool
2//!
3//! This example demonstrates how to use the `MemoryDBTool` to provide an agent
4//! with a simple in-memory key-value database. This allows the agent to cache
5//! data, store user preferences, and maintain state across conversations.
6
7use helios_engine::{Agent, Config, MemoryDBTool};
8
9#[tokio::main]
10async fn main() -> helios_engine::Result<()> {
11    println!("šŸš€ Helios Engine - Agent with Memory DB Example");
12    println!("================================================\n");
13
14    // Load configuration from `config.toml` or use default.
15    let config = Config::from_file("config.toml").unwrap_or_else(|_| {
16        println!("⚠ No config.toml found, using default configuration");
17        Config::new_default()
18    });
19
20    // Create an agent named "DataAgent" and equip it with the MemoryDBTool.
21    let mut agent = Agent::builder("DataAgent")
22        .config(config)
23        .system_prompt(
24            "You are a helpful assistant with access to an in-memory database. \
25             You can store and retrieve information using the memory_db tool. \
26             Operations available: set, get, delete, list, clear, exists. \
27             Use this to remember important information across our conversation.",
28        )
29        .tool(Box::new(MemoryDBTool::new()))
30        .max_iterations(10)
31        .build()
32        .await?;
33
34    println!("āœ“ Agent created with memory database tool\n");
35
36    // --- Example 1: Store user preferences ---
37    println!("Example 1: Storing User Preferences");
38    println!("====================================\n");
39
40    let response = agent
41        .chat("Store my name as 'Alice' and my favorite color as 'blue' in the database")
42        .await?;
43    println!("Agent: {}\n", response);
44
45    // --- Example 2: Retrieve stored data ---
46    println!("\nExample 2: Retrieving Stored Data");
47    println!("==================================\n");
48
49    let response = agent.chat("What's my name and favorite color?").await?;
50    println!("Agent: {}\n", response);
51
52    // --- Example 3: Store calculations ---
53    println!("\nExample 3: Caching Calculations");
54    println!("================================\n");
55
56    let response = agent
57        .chat("Calculate 123 * 456 and store the result in the database with key 'calculation_result'")
58        .await?;
59    println!("Agent: {}\n", response);
60
61    // --- Example 4: List all stored data ---
62    println!("\nExample 4: Listing All Data");
63    println!("===========================\n");
64
65    let response = agent
66        .chat("Show me everything stored in the database")
67        .await?;
68    println!("Agent: {}\n", response);
69
70    // --- Example 5: Check if key exists ---
71    println!("\nExample 5: Checking Key Existence");
72    println!("==================================\n");
73
74    let response = agent
75        .chat("Check if 'name' and 'age' exist in the database")
76        .await?;
77    println!("Agent: {}\n", response);
78
79    // --- Example 6: Delete specific data ---
80    println!("\nExample 6: Deleting Data");
81    println!("========================\n");
82
83    let response = agent
84        .chat("Delete the 'calculation_result' from the database")
85        .await?;
86    println!("Agent: {}\n", response);
87
88    // --- Example 7: Final state ---
89    println!("\nExample 7: Final Database State");
90    println!("================================\n");
91
92    let response = agent
93        .chat("List all remaining items in the database")
94        .await?;
95    println!("Agent: {}\n", response);
96
97    println!("\nāœ… Example completed successfully!");
98    println!("\nšŸ’” Key Features Demonstrated:");
99    println!("  • Setting key-value pairs in memory database");
100    println!("  • Retrieving stored values");
101    println!("  • Listing all database contents");
102    println!("  • Checking key existence");
103    println!("  • Deleting specific entries");
104    println!("  • Persistent data across multiple agent interactions");
105    println!("\nšŸ“ Use Cases:");
106    println!("  • Caching expensive computations");
107    println!("  • Storing user preferences during conversation");
108    println!("  • Maintaining context across multiple queries");
109    println!("  • Temporary data storage for complex workflows");
110
111    Ok(())
112}