Skip to content

The agent engineering platform.

community[patch]: Include scores in MongoDB Atlas QA chain results (#14666)

Noah Stappe135e5257c4e
diff --git a/libs/community/langchain_community/vectorstores/mongodb_atlas.py b/libs/community/langchain_community/vectorstores/mongodb_atlas.py
index f91071ac86..c105e6f536 100644
--- a/libs/community/langchain_community/vectorstores/mongodb_atlas.py
+++ b/libs/community/langchain_community/vectorstores/mongodb_atlas.py
@@ -209,6 +209,7 @@ class MongoDBAtlasVectorSearch(VectorStore):
for res in cursor:
text = res.pop(self._text_key)
score = res.pop("score")
+ del res["embedding"]
docs.append((Document(page_content=text, metadata=res), score))
return docs
@@ -221,11 +222,8 @@ class MongoDBAtlasVectorSearch(VectorStore):
) -> List[Tuple[Document, float]]:
"""Return MongoDB documents most similar to the given query and their scores.
- Uses the $vectorSearch stage
- performs aNN search on a vector in the specified field.
- Index the field as "vector" using Atlas Vector Search "vectorSearch" index type
-
- For more info : https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/
+ Uses the vectorSearch operator available in MongoDB Atlas Search.
+ For more: https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/
Args:
query: Text to look up documents similar to.
@@ -233,7 +231,7 @@ class MongoDBAtlasVectorSearch(VectorStore):
pre_filter: (Optional) dictionary of argument(s) to prefilter document
fields on.
post_filter_pipeline: (Optional) Pipeline of MongoDB aggregation stages
- following the vector Search.
+ following the vectorSearch stage.
Returns:
List of documents most similar to the query and their scores.
@@ -257,11 +255,8 @@ class MongoDBAtlasVectorSearch(VectorStore):
) -> List[Document]:
"""Return MongoDB documents most similar to the given query.
- Uses the $vectorSearch stage
- performs aNN search on a vector in the specified field.
- Index the field as "vector" using Atlas Vector Search "vectorSearch" index type
-
- For more info : https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/
+ Uses the vectorSearch operator available in MongoDB Atlas Search.
+ For more: https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/
Args:
query: Text to look up documents similar to.
@@ -269,17 +264,22 @@ class MongoDBAtlasVectorSearch(VectorStore):
pre_filter: (Optional) dictionary of argument(s) to prefilter document
fields on.
post_filter_pipeline: (Optional) Pipeline of MongoDB aggregation stages
- following the vector search.
+ following the vectorSearch stage.
Returns:
List of documents most similar to the query and their scores.
"""
+ additional = kwargs.get("additional")
docs_and_scores = self.similarity_search_with_score(
query,
k=k,
pre_filter=pre_filter,
post_filter_pipeline=post_filter_pipeline,
)
+
+ if additional and "similarity_score" in additional:
+ for doc, score in docs_and_scores:
+ doc.metadata["score"] = score
return [doc for doc, _ in docs_and_scores]
def max_marginal_relevance_search(
@@ -309,7 +309,7 @@ class MongoDBAtlasVectorSearch(VectorStore):
pre_filter: (Optional) dictionary of argument(s) to prefilter on document
fields.
post_filter_pipeline: (Optional) pipeline of MongoDB aggregation stages
- following the vector search.
+ following the vectorSearch stage.
Returns:
List of documents selected by maximal marginal relevance.
"""