Skip to content

Commit ba5b5e9

Browse files
authored
Merge branch 'main' into patch-2
2 parents 17fce12 + cce6df2 commit ba5b5e9

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

libs/langchain-mongodb/langchain_mongodb/vectorstores.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,3 +874,36 @@ def create_vector_search_index(
874874
wait_until_complete=wait_until_complete,
875875
**kwargs,
876876
) # type: ignore [operator]
877+
878+
def similarity_search_by_vector(
879+
self,
880+
embedding: list[float],
881+
k: int = 4,
882+
**kwargs: Any,
883+
) -> list[Document]:
884+
"""Return MongoDB documents most similar to the given query vector.
885+
886+
Atlas Vector Search eliminates the need to run a separate
887+
search system alongside your database.
888+
889+
Args:
890+
embedding: Embedding vector to search for.
891+
k: (Optional) number of documents to return. Defaults to 4.
892+
pre_filter: List of MQL match expressions comparing an indexed field
893+
post_filter_pipeline: (Optional) Pipeline of MongoDB aggregation stages
894+
to filter/process results after $vectorSearch.
895+
oversampling_factor: Multiple of k used when generating number of candidates
896+
at each step in the HNSW Vector Search.
897+
include_embeddings: If True, the embedding vector of each result
898+
will be included in metadata.
899+
kwargs: Additional arguments are specific to the search_type
900+
901+
Returns:
902+
List of documents most similar to the query vector.
903+
"""
904+
tuple_list = self._similarity_search_with_score(
905+
embedding,
906+
k=k,
907+
**kwargs,
908+
)
909+
return [doc for doc, _ in tuple_list]

libs/langchain-mongodb/tests/integration_tests/test_vectorstore_from_texts.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,41 @@ def test_search_pre_filter(
117117
"Sandwich", k=3, pre_filter={"c": {"$gt": 0}}
118118
)
119119
assert len(matches_filter) == 1
120+
121+
122+
def test_similarity_search_by_vector(
123+
vectorstore: PatchedMongoDBAtlasVectorSearch,
124+
embeddings: Embeddings,
125+
texts: List[str],
126+
) -> None:
127+
# Test similarity_search_by_vector method
128+
# First, embed a query text to get a vector
129+
query_text = "Sandwich"
130+
query_vector = embeddings.embed_query(query_text)
131+
132+
# Perform search by vector
133+
output = vectorstore.similarity_search_by_vector(query_vector, k=2)
134+
135+
# Should return results
136+
assert len(output) == 2
137+
# Results should be Document objects
138+
assert all(hasattr(doc, "page_content") for doc in output)
139+
assert all(hasattr(doc, "metadata") for doc in output)
140+
141+
142+
def test_similarity_search_by_vector_with_filter(
143+
vectorstore: PatchedMongoDBAtlasVectorSearch,
144+
embeddings: Embeddings,
145+
) -> None:
146+
# Test similarity_search_by_vector with pre_filter
147+
query_text = "Sandwich"
148+
query_vector = embeddings.embed_query(query_text)
149+
150+
# Search with filter
151+
filtered_output = vectorstore.similarity_search_by_vector(
152+
query_vector, k=3, pre_filter={"c": {"$gt": 0}}
153+
)
154+
155+
# Should only return documents matching the filter
156+
assert len(filtered_output) == 1
157+
assert "c" in filtered_output[0].metadata

0 commit comments

Comments
 (0)