Crate victor_db

Source
Expand description

A browser-optimized vector database. Backed by the private virtual filesystem API on web.

You’re viewing this on crates.io, so you’re probably interested in the native version. The native version supports running with the native filesystem or in memory.

If you want to use it on the web, check out victor-db on npm.

§In-memory database

Use this if you want to run victor in-memory (all data is lost when the program exits).

The in-memory version is useful for testing and applications where you don’t need to persist data:

// use victor_db::memory for the in-memory implementation
use victor_db::memory::{Db, DirectoryHandle};

// create a new in-memory database
let mut victor = Db::new(DirectoryHandle::default());

// add some embeddings to the database
victor
    .add(
        vec!["Pineapple", "Rocks"], // documents
        vec!["Pizza Toppings"],     // tags (only used for filtering)
    )
    .await;

// add another embedding to the database, this time with no tags
victor.add_single("Cheese pizza", vec!["Pizza Flavors"]).await;

// read the 10 closest results from victor that are tagged with "Pizza Toppings"
// (only 2 will be returned because we only inserted two embeddings)
let nearest = victor
    .search("Hawaiian pizza", vec!["Pizza Toppings"], 10)
    .await
    .first()
    .unwrap()
    .content
    .clone();
assert_eq!(nearest, "Pineapple".to_string());

// Clear the database
victor.clear_db().await.unwrap();

§Native database

Use this if you want to persist your database to disk.

// use victor_db::native for the native filesystem implementation
use victor_db::native::Db;
use std::path::PathBuf;

// create a new native database under "./victor_test_data"
let _ = std::fs::create_dir("./victor_test_data");
let mut victor = Db::new(PathBuf::from("./victor_test_data"));

// add some embeddings to the database
victor
    .add(
        vec!["Pineapple", "Rocks"], // documents
        vec!["Pizza Toppings"],     // tags (only used for filtering)
    )
    .await;

// add another embedding to the database, this time with no tags
victor.add_single("Cheese pizza", vec!["Pizza Flavors"]).await;

// read the 10 closest results from victor that are tagged with "Pizza Toppings"
// (only 2 will be returned because we only inserted two embeddings)
let nearest = victor
    .search("Hawaiian pizza", vec!["Pizza Toppings"], 10)
    .await
    .first()
    .unwrap()
    .content
    .clone();
assert_eq!(nearest, "Pineapple".to_string());

// Clear the database
victor.clear_db().await.unwrap();

See the docs for Victor for more information.

Modules§

memory
Victor’s in-memory implementation.
native
Victor’s native filesystem implementation.

Structs§

Victor
The main database struct. Through this you can Victor::add and Victor::search for embeddings.