Skip to content

Commit 47c3221

Browse files
authored
Max marginal relecance search fails if there are not enough docs (#1117)
Implementation fails if there are not enough documents. Added the same check as used for similarity search. Current implementation raises ``` File ".venv/lib/python3.9/site-packages/langchain/vectorstores/faiss.py", line 160, in max_marginal_relevance_search _id = self.index_to_docstore_id[i] KeyError: -1 ```
1 parent 511d411 commit 47c3221

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

langchain/vectorstores/faiss.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ def max_marginal_relevance_search_by_vector(
189189
docs = []
190190
for i in selected_indices:
191191
_id = self.index_to_docstore_id[i]
192+
if _id == -1:
193+
# This happens when not enough docs are returned.
194+
continue
192195
doc = self.docstore.search(_id)
193196
if not isinstance(doc, Document):
194197
raise ValueError(f"Could not find document for id {_id}, got {doc}")

0 commit comments

Comments
 (0)