MemoryDBTool

Struct MemoryDBTool 

Source
pub struct MemoryDBTool { /* private fields */ }
Expand description

In-Memory Database Tool

Provides a simple key-value store for agents to cache data during conversations. Supports set, get, delete, list keys, and clear operations.

Implementations§

Source§

impl MemoryDBTool

Source

pub fn new() -> Self

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

pub fn with_shared_db(db: Arc<Mutex<HashMap<String, String>>>) -> Self

Trait Implementations§

Source§

impl Default for MemoryDBTool

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Tool for MemoryDBTool

Source§

fn name(&self) -> &str

Source§

fn description(&self) -> &str

Source§

fn parameters(&self) -> HashMap<String, ToolParameter>

Source§

fn execute<'life0, 'async_trait>( &'life0 self, args: Value, ) -> Pin<Box<dyn Future<Output = Result<ToolResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn to_definition(&self) -> ToolDefinition

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more