Skip to Content

Store

Alith provides a Store feature that allows you to persist and retrieve data across sessions or interactions. This is useful for storing user preferences, session data, or any other information that needs to be retained over time.

Memory Store

use alith::{Agent, Chat, InMemoryStorage, LLM}; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { let model = LLM::from_model_name("gpt-4")?; let embeddings_model = model.embeddings_model("text-embedding-3-small"); let storage = InMemoryStorage::from_multiple_documents::<()>(embeddings_model, vec![]); let agent = Agent::new("simple agent", model) .preamble( r#" You are a dictionary assistant here to assist the user in understanding the meaning of words. You will find additional non-standard word definitions that could be useful below. "#, ) .store_index(1, storage); let response = agent.prompt("What does \"glarb-glarb\" mean?").await?; println!("{}", response); Ok(()) }

Qdrant Vector Database

Note: we need to enable the qdrant feature in the alith crate to use this example.

use alith::store::qdrant::{ CreateCollectionBuilder, Distance, QdrantClient, QdrantStorage, VectorParamsBuilder, DEFAULT_COLLECTION_NAME, }; use alith::{Agent, Chat, EmbeddingsBuilder, LLM}; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { let model = LLM::from_model_name("gpt-4")?; let client = QdrantClient::from_url("http://localhost:6334").build()?; if !client.collection_exists(DEFAULT_COLLECTION_NAME).await? { client .create_collection( CreateCollectionBuilder::new(DEFAULT_COLLECTION_NAME) .vectors_config(VectorParamsBuilder::new(1536, Distance::Cosine)), ) .await?; } let storage = QdrantStorage::from_multiple_documents::<()>(client, embeddings_model, vec![]).await?; let agent = Agent::new("simple agent", model) .preamble( r#" You are a dictionary assistant here to assist the user in understanding the meaning of words. You will find additional non-standard word definitions that could be useful below. "#, ) .store_index(1, storage); let response = agent.prompt("What does \"glarb-glarb\" mean?").await?; println!("{}", response); Ok(()) }

PgVector Vector Database

Note: we need to enable the pgvector feature in the alith crate to use this example.

use alith::store::pgvector::{PgPoolOptions, PgVectorStorage, migrate, sqlx}; use alith::{Agent, Chat, EmbeddingsBuilder, LLM}; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { let model = LLM::from_model_name("gpt-4")?; let embeddings_model = model.embeddings_model("text-embedding-3-small"); let data = EmbeddingsBuilder::new(embeddings_model.clone()) .documents(vec!["doc0", "doc1", "doc2"]) .unwrap() .build() .await?; let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL not set"); let pool = PgPoolOptions::new() .max_connections(50) .idle_timeout(std::time::Duration::from_secs(5)) .connect(&database_url) .await .expect("Failed to create postgres pool"); // Make sure database is setup migrate!("examples/pgvector_migarations").run(&pool).await?; // Delete documents from table to have a clean start (optional, not recommended for production) sqlx::query("TRUNCATE alith").execute(&pool).await?; let storage = PgVectorStorage::from_multiple_documents(pool, embeddings_model, data).await?; let agent = Agent::new("simple agent", model) .preamble( r#" You are a dictionary assistant here to assist the user in understanding the meaning of words. You will find additional non-standard word definitions that could be useful below. "#, ) .store_index(1, storage); let response = agent.prompt("What does \"glarb-glarb\" mean?").await?; println!("{}", response); Ok(()) }

Milvus Vector Storage

Note: we need to enable the milvus feature in the alith crate to use this example.

use alith::store::milvus::{MilvusClient, MilvusStorage}; use alith::{Agent, Chat, EmbeddingsBuilder, LLM}; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { let model = LLM::from_model_name("gpt-4")?; let embeddings_model = model.embeddings_model("text-embedding-3-small"); let data = EmbeddingsBuilder::new(embeddings_model.clone()) .documents(vec!["doc0", "doc1", "doc2"]) .unwrap() .build() .await?; let client = MilvusClient::new("http://localhost:19530").await?; let storage = MilvusStorage::from_multiple_documents(client, embeddings_model, data).await?; let agent = Agent::new("simple agent", model) .preamble( r#" You are a dictionary assistant here to assist the user in understanding the meaning of words. You will find additional non-standard word definitions that could be useful below. "#, ) .store_index(1, storage); let response = agent.prompt("What does \"glarb-glarb\" mean?").await?; println!("{}", response); Ok(()) }

ChromaDB

Note: we need to enable the chromadb feature in the alith crate to use this example.

use alith::store::chromadb::{ChromaClient, ChromaStorage}; use alith::{Agent, Chat, EmbeddingsBuilder, LLM}; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { let model = LLM::from_model_name("gpt-4")?; let embeddings_model = model.embeddings_model("text-embedding-3-small"); let data = EmbeddingsBuilder::new(embeddings_model.clone()) .documents(vec!["doc0", "doc1", "doc2"]) .unwrap() .build() .await?; // With default ChromaClientOptions // Defaults to http://localhost:8000 let client: ChromaClient = ChromaClient::new(Default::default()).await?; let storage = ChromaStorage::from_multiple_documents(client, embeddings_model, data).await?; let agent = Agent::new("simple agent", model) .preamble( r#" You are a dictionary assistant here to assist the user in understanding the meaning of words. You will find additional non-standard word definitions that could be useful below. "#, ) .store_index(1, storage); let response = agent.prompt("What does \"glarb-glarb\" mean?").await?; println!("{}", response); Ok(()) }
Last updated on