-
Notifications
You must be signed in to change notification settings - Fork 588
feat: LSH for in memory vector store #922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
07c65e6
add: random projection lsh
Dev79844 9ff2277
Merge remote-tracking branch 'origin/main' into feat-lsh
Dev79844 3619a1a
fixed: all the linting issues
Dev79844 06b6b4f
revert: features in Cargo.toml
Dev79844 92a61e8
Merge remote-tracking branch 'origin/main' into feat-lsh
Dev79844 09977ee
Merge remote-tracking branch 'origin/main' into feat-lsh
Dev79844 d07df74
added: builder for in_memory_vector_store
Dev79844 662a8be
fixed: examples and linting issues
Dev79844 ede0ec3
Merge remote-tracking branch 'origin/main' into feat-lsh
Dev79844 016bec6
Merge remote-tracking branch 'origin/main' into feat-lsh
Dev79844 461c5c2
added: comments and full syntax
Dev79844 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| use serde::Serialize; | ||
| use std::collections::HashMap; | ||
|
|
||
| use crate::{OneOrMany, embeddings::Embedding}; | ||
|
|
||
| use super::{IndexStrategy, in_memory_store::InMemoryVectorStore}; | ||
|
|
||
| /// Builder for creating an [InMemoryVectorStore] with custom configuration. | ||
| pub struct InMemoryVectorStoreBuilder<D: Serialize> { | ||
| embeddings: HashMap<String, (D, OneOrMany<Embedding>)>, | ||
| index_strategy: IndexStrategy, | ||
| } | ||
|
|
||
| impl<D: Serialize + Eq> Default for InMemoryVectorStoreBuilder<D> { | ||
Dev79844 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl<D: Serialize + Eq> InMemoryVectorStoreBuilder<D> { | ||
Dev79844 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// Create a new builder with default settings. | ||
| /// Default index strategy is BruteForce. | ||
| pub fn new() -> Self { | ||
| Self { | ||
| embeddings: HashMap::new(), | ||
| index_strategy: IndexStrategy::default(), | ||
| } | ||
| } | ||
|
|
||
| /// Set the index strategy for the vector store. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ```ignore | ||
| /// use rig::vector_store::{InMemoryVectorStoreBuilder, IndexStrategy}; | ||
| /// | ||
| /// let store = InMemoryVectorStoreBuilder::<String>::new() | ||
| /// .index_strategy(IndexStrategy::LSH { | ||
| /// num_tables: 5, | ||
| /// num_hyperplanes: 10, | ||
| /// }) | ||
| /// .build(); | ||
| /// ``` | ||
| pub fn index_strategy(mut self, index_strategy: IndexStrategy) -> Self { | ||
| self.index_strategy = index_strategy; | ||
| self | ||
| } | ||
|
|
||
| /// Add documents with auto-generated IDs. | ||
| /// IDs will have the form `"doc{n}"` where `n` is the index. | ||
| pub fn documents( | ||
| mut self, | ||
| documents: impl IntoIterator<Item = (D, OneOrMany<Embedding>)>, | ||
| ) -> Self { | ||
| let current_index = self.embeddings.len(); | ||
| documents | ||
| .into_iter() | ||
| .enumerate() | ||
| .for_each(|(i, (doc, embeddings))| { | ||
| self.embeddings | ||
| .insert(format!("doc{}", i + current_index), (doc, embeddings)); | ||
| }); | ||
| self | ||
| } | ||
|
|
||
| /// Add documents with explicit IDs. | ||
| pub fn documents_with_ids( | ||
| mut self, | ||
| documents: impl IntoIterator<Item = (impl ToString, D, OneOrMany<Embedding>)>, | ||
| ) -> Self { | ||
| documents.into_iter().for_each(|(id, doc, embeddings)| { | ||
| self.embeddings.insert(id.to_string(), (doc, embeddings)); | ||
| }); | ||
| self | ||
| } | ||
|
|
||
| /// Add documents with IDs generated by a function. | ||
| pub fn documents_with_id_f( | ||
| mut self, | ||
| documents: impl IntoIterator<Item = (D, OneOrMany<Embedding>)>, | ||
| f: fn(&D) -> String, | ||
| ) -> Self { | ||
| documents.into_iter().for_each(|(doc, embeddings)| { | ||
| let id = f(&doc); | ||
| self.embeddings.insert(id, (doc, embeddings)); | ||
| }); | ||
| self | ||
| } | ||
|
|
||
| /// Build the [InMemoryVectorStore] with the configured settings. | ||
| pub fn build(self) -> InMemoryVectorStore<D> { | ||
| InMemoryVectorStore::from_builder(self.embeddings, self.index_strategy) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.