Skip to content

0xhappyboy/memcontext

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

memcontext

A reliable LLM context management engine.

简体中文 | English

Features

  • Three recall strategies: Time-series, keyword matching, and semantic vector search
  • Two storage backends: Local JSON files and SQLite (with vector support)
  • Embedded and zero-config: No external dependencies, just add to your Cargo.toml
  • Session persistence: Save conversation history across application restarts

Quick Start

use memcontext::{MemContext, MemContextConfig, StorageType, DatabaseType};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a memory context with SQLite storage
    let config = MemContextConfig {
        storage_type: StorageType::DB,
        db_type: Some(DatabaseType::SQLite),
        sqlite_storage_path: Some("./my_memory.db".to_string()),
        ..Default::default()
    };

    let mem = MemContext::new(config).await?;
    let session_id = "user_session_123";

    // Store conversation
    mem.storage_user_chat(session_id.to_string(), "My name is Alice".to_string()).await?;
    mem.storage_llm_chat(session_id.to_string(), "Hello Alice!".to_string()).await?;

    // Recall by keyword
    let result = mem.recall_keywords(session_id, "Alice", 5).await?;
    println!("{}", result.to_context_string());

    // Semantic search
    let semantic = mem.recall_vec_semantic(session_id, "what is my name", 3).await?;
    println!("{}", semantic.to_context_string());

    Ok(())
}

API Overview

Method Description
storage_user_chat() Store user message
storage_llm_chat() Store assistant response
recall_time_series() Get most recent N messages
recall_keywords() Search by keyword matching
recall_vec_semantic() Search by semantic similarity
clear_session() Delete all messages for a session
session_size() Get message count for a session

Configuration

let config = MemContextConfig {
storage_type: StorageType::Local, // Local or DB
sqlite_storage_path: Some("./data.db".to_string()), // for SQLite
local_storage_path: Some("./data".to_string()), // for Local JSON
..Default::default()
};

About

A reliable LLM context management engine.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages