Skip to content

Commit

Permalink
feat: Add more utility methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cvauclair committed Sep 20, 2024
1 parent 5c98789 commit 601d69b
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion rig-core/src/vector_store/in_memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,36 @@ impl InMemoryVectorStore {
pub async fn from_embeddings(
embeddings: Vec<DocumentEmbeddings>,
) -> Result<Self, VectorStoreError> {
let mut store = InMemoryVectorStore::default();
let mut store = Self::default();
store.add_documents(embeddings).await?;
Ok(store)
}

/// Create an InMemoryVectorStore from a list of documents.
/// The documents are serialized to JSON and embedded using the provided embedding model.
/// The resulting embeddings are stored in an InMemoryVectorStore created by the method.
pub async fn from_documents<M: EmbeddingModel, T: Serialize>(
embedding_model: M,
documents: &[(String, T)],
) -> Result<Self, VectorStoreError> {
let embeddings = documents
.iter()
.fold(
EmbeddingsBuilder::new(embedding_model),
|builder, (id, doc)| {
builder.json_document(
id,
serde_json::to_value(doc).expect("Document should be serializable"),
vec![serde_json::to_string(doc).expect("Document should be serializable")],
)
},
)
.build()
.await?;

let store = Self::from_embeddings(embeddings).await?;
Ok(store)
}
}

pub struct InMemoryVectorIndex<M: EmbeddingModel> {
Expand Down

0 comments on commit 601d69b

Please sign in to comment.