Database

Storage backends for agent data in PraisonAI Rust SDK

Database integration provides persistent and in-memory storage for agent knowledge and sessions.

Diagram

Storage Options

🤖 Agent

🧠 InMemoryVectorStore

📁 FileSessionStore

🔍 Search

💾 Persist

Quick Start

  1. In-Memory Vector Store
    rust
    use praisonai::{Agent, InMemoryVectorStore, VectorRecord};
    
    // Create vector store for semantic search
    let mut store = InMemoryVectorStore::new();
    
    // Add knowledge
    let record = VectorRecord::new("doc-1", "AI agents are autonomous systems")
        .with_embedding(vec![0.1, 0.2, 0.3]);
    store.add(record).await?;
    
    // Create agent with knowledge
    let agent = Agent::new()
        .name("Researcher")
        .knowledge(store)
        .build()?;
    
    agent.start("What are AI agents?").await?;
  2. File-Based Sessions
    rust
    use praisonai::{Agent, FileSessionStore};
    
    // Persist conversations to disk
    let store = FileSessionStore::new();
    
    let agent = Agent::new()
        .name("Assistant")
        .session_store(store)
        .build()?;
    
    // Conversations persist across restarts
    agent.start("Remember this context").await?;

User Interaction Flow

Diagram
SessionStoreVectorStoreAgentUserSessionStoreVectorStoreAgentUser“What do you know about X?”search(embedding, limit)Relevant documentsGenerate responsesave(session_id, messages)“Based on my knowledge…”

InMemoryVectorStore

Fast, in-memory vector storage for development and small knowledge bases.

rust
pub struct InMemoryVectorStore {
    records: Vec<VectorRecord>,
}

Methods

MethodSignatureDescription
new()fn new() -> SelfCreate empty store
add(record)`async fn add(&mut self, VectorRecord) -> Result

| Add a record | | search(embedding, limit) | async fn search(&self, &[f32], usize) -> Result<Vec

` | Search by embedding |

| get(id) | `async fn get(&self, &str) -> Result<Option

` | Get by ID |

| delete(id) | async fn delete(&mut self, &str) -> Result<bool> | Delete by ID |

Example

rust
use praisonai::{InMemoryVectorStore, VectorRecord, VectorStoreProtocol};

let mut store = InMemoryVectorStore::new();

// Add records with embeddings
let record = VectorRecord::new("id-1", "Document content")
    .with_embedding(vec![0.1, 0.2, 0.3, 0.4]);

store.add(record).await?;

// Search for similar content
let results = store.search(&[0.1, 0.2, 0.3, 0.4], 5).await?;
for item in results {
    println!("Found: {} (score: {})", item.text, item.score);
}

FileSessionStore

Persistent file-based storage for session data.

rust
pub struct FileSessionStore {
    session_dir: PathBuf,
    max_messages: usize,
}

Configuration Options

OptionTypeDefaultDescription
session_dirPathBuf~/.praisonai/sessions/Storage directory
max_messagesusize100Max messages per session

Methods

MethodSignatureDescription
new()fn new() -> SelfDefault directory
with_dir(dir)`fn with_dir(impl Into

) -> Self | Custom directory | | max_messages(n) | fn max_messages(self, usize) -> Self` | Set limit |


Storage Comparison

StorePersistenceUse CasePerformance
InMemoryVectorStoreMemory onlyDevelopment, testing⚡ Fastest
FileSessionStoreDiskProduction sessions💾 Durable

Best Practices

Use InMemoryVectorStore for development

Fast iteration without database setup. Switch to persistent store for production.

Set message limits for long-running agents

Use FileSessionStore::new().max_messages(100) to prevent unbounded growth.

Index by semantic meaning, not keywords

Vector stores search by embedding similarity – structure your documents for semantic retrieval.


Related