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

Creates a new MemoryDBTool.

Examples found in repository?
examples/agent_with_memory_db.rs (line 29)
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}
Source

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

Creates a new MemoryDBTool with a shared database.

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

The name of the tool.
Source§

fn description(&self) -> &str

A description of the tool.
Source§

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

The parameters for the tool.
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,

Executes the tool with the given arguments.
Source§

fn to_definition(&self) -> ToolDefinition

Converts the tool to a 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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