Skip to content

Commit

Permalink
Add batch add api (to rust) (#45)
Browse files Browse the repository at this point in the history
* Add batch add api (to rust)

* Update readme and add docs

* Add docs

* Add in_memory example

* fix CI

* add more docs
  • Loading branch information
anchpop authored Dec 26, 2024
1 parent 70e3a63 commit c0b35e6
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 93 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ jobs:
- name: Check formatting
run: cargo fmt --all -- --check

- name: Run clippy
run: cargo clippy -- -D warnings
- name: Run clippy (wasm)
run: cargo clippy --all-targets -- -D warnings

- name: Run tests
run: cargo test

- name: Run examples
run: cargo test --examples

- name: Check semver
uses: obi1kenobi/cargo-semver-checks-action@v2

Expand Down
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,22 @@ victor.clear_db().await.unwrap();

victor
.add_many(
vec!["Pinapple", "Rocks"], // documents
vec!["PizzaToppings"], // tags (only used for filtering)
vec!["Pineapple", "Rocks"], // documents
vec!["PizzaToppings"], // tags (only used for filtering)
)
.await;

victor.add("Cheese pizza", vec![]).await; // Add another entry with no tags

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

Expand Down
28 changes: 28 additions & 0 deletions examples/in_memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[tokio::main(flavor = "current_thread")]
async fn main() {
use victor_db::memory::{Db, DirectoryHandle};

let mut victor = Db::new(DirectoryHandle::default());

victor.clear_db().await.unwrap();

victor
.add_many(
vec!["Pineapple", "Rocks"], // documents
vec!["PizzaToppings"], // tags (only used for filtering)
)
.await;

victor.add("Cheese pizza", vec![]).await; // Add another entry with no tags

// read the 10 closest results from victor that are tagged with "tags"
// (only 2 will be returned because we only inserted two embeddings)
let nearest = victor
.search("Hawaiian pizza", vec!["PizzaToppings"], 10)
.await
.first()
.unwrap()
.content
.clone();
assert_eq!(nearest, "Pineapple".to_string());
}
28 changes: 11 additions & 17 deletions examples/native_filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
use std::path::PathBuf;

use victor_db::native::Db;

#[tokio::main(flavor = "current_thread")]
async fn main() {
use std::path::PathBuf;

use victor_db::native::Db;

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

victor.clear_db().await.unwrap();

victor
.add_embedding(
"Test Vector 1",
vec![1.0, 0.0, 0.0],
vec!["Test".to_string()],
)
.await;
victor
.add_embedding(
"Test Vector 2",
vec![0.0, 1.0, 0.0],
vec!["Test".to_string()],
.add_many(
vec!["Pineapple", "Rocks"], // documents
vec!["PizzaToppings"], // tags (only used for filtering)
)
.await;

victor.add("Cheese pizza", vec![]).await; // Add another entry with no tags

// read the 10 closest results from victor that are tagged with "tags"
// (only 2 will be returned because we only inserted two embeddings)
let nearest = victor
.search_embedding(vec![0.9, 0.0, 0.0], vec!["Test".to_string()], 10)
.search("Hawaiian pizza", vec!["PizzaToppings"], 10)
.await
.first()
.unwrap()
.content
.clone();
assert_eq!(nearest, "Test Vector 1".to_string());
assert_eq!(nearest, "Pineapple".to_string());
}
Loading

0 comments on commit c0b35e6

Please sign in to comment.