Skip to content
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

fix(mongodb): remove embeddings from top_n lookup #115

Merged
merged 6 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rig-mongodb/examples/vector_search_mongodb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rig::{
providers::openai::{Client, TEXT_EMBEDDING_ADA_002},
vector_store::VectorStoreIndex,
};
use rig_mongodb::{MongoDbVectorStore, SearchParams};
use rig_mongodb::{DocumentResponse, MongoDbVectorStore, SearchParams};
use std::env;

#[tokio::main]
Expand Down Expand Up @@ -53,7 +53,7 @@ async fn main() -> Result<(), anyhow::Error> {

// Query the index
let results = index
.top_n::<DocumentEmbeddings>("What is a linglingdong?", 1)
.top_n::<DocumentResponse>("What is a linglingdong?", 1)
.await?
.into_iter()
.map(|(score, id, doc)| (score, id, doc.document))
Expand Down
20 changes: 17 additions & 3 deletions rig-mongodb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use rig::{
embeddings::{DocumentEmbeddings, Embedding, EmbeddingModel},
vector_store::{VectorStore, VectorStoreError, VectorStoreIndex},
};
use serde::Deserialize;
use serde::{Deserialize, Serialize};

const EMBEDDINGS_VECTOR_FIELD: &str = "embeddings.vec";

/// A MongoDB vector store.
pub struct MongoDbVectorStore {
Expand Down Expand Up @@ -118,7 +120,7 @@ impl<M: EmbeddingModel> MongoDbVectorIndex<M> {
doc! {
"$vectorSearch": {
"index": &self.index_name,
"path": "embeddings.vec",
"path": EMBEDDINGS_VECTOR_FIELD,
"queryVector": &prompt_embedding.vec,
"numCandidates": num_candidates.unwrap_or((n * 10) as u32),
"limit": n as u32,
Expand Down Expand Up @@ -155,7 +157,7 @@ impl<M: EmbeddingModel> MongoDbVectorIndex<M> {
}
}

/// See [MongoDB Vector Search](https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/) for more information
/// See [MongoDB Vector Search](`https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/`) for more information
/// on each of the fields
pub struct SearchParams {
filter: mongodb::bson::Document,
Expand Down Expand Up @@ -219,6 +221,11 @@ impl<M: EmbeddingModel + std::marker::Sync + Send> VectorStoreIndex for MongoDbV
[
self.pipeline_search_stage(&prompt_embedding, n),
self.pipeline_score_stage(),
doc! {
"$project": {
EMBEDDINGS_VECTOR_FIELD: 0,
},
},
],
None,
)
Expand Down Expand Up @@ -291,3 +298,10 @@ impl<M: EmbeddingModel + std::marker::Sync + Send> VectorStoreIndex for MongoDbV
Ok(results)
}
}

#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Debug)]
pub struct DocumentResponse {
#[serde(rename = "_id")]
pub id: String,
pub document: serde_json::Value,
}
Loading