Skip to content

Commit

Permalink
Allow k to be higher than doc size in max_marginal_relevance_search (l…
Browse files Browse the repository at this point in the history
…angchain-ai#1187)

Fixes issue langchain-ai#1186. For some reason, langchain-ai#1117 didn't seem to fix it.
  • Loading branch information
whitead authored and zachschillaci27 committed Mar 8, 2023
1 parent 72bbf0a commit 6256cc9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions langchain/vectorstores/faiss.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ def max_marginal_relevance_search_by_vector(
selected_indices = [indices[0][i] for i in mmr_selected]
docs = []
for i in selected_indices:
_id = self.index_to_docstore_id[i]
if _id == -1:
if i == -1:
# This happens when not enough docs are returned.
continue
_id = self.index_to_docstore_id[i]
doc = self.docstore.search(_id)
if not isinstance(doc, Document):
raise ValueError(f"Could not find document for id {_id}, got {doc}")
Expand Down
4 changes: 4 additions & 0 deletions tests/integration_tests/vectorstores/test_faiss.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def test_faiss_vector_sim() -> None:
output = docsearch.similarity_search_by_vector(query_vec, k=1)
assert output == [Document(page_content="foo")]

# make sure we can have k > docstore size
output = docsearch.max_marginal_relevance_search_by_vector(query_vec, k=10)
assert len(output) == len(texts)


def test_faiss_with_metadatas() -> None:
"""Test end to end construction and search."""
Expand Down

0 comments on commit 6256cc9

Please sign in to comment.