From 2311fb007d5d8c4673f633f1cb18e38bbb7b12f7 Mon Sep 17 00:00:00 2001 From: "bright.python" Date: Fri, 16 Jun 2023 23:47:31 +0800 Subject: [PATCH] Update MyFAISS.py (#648) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 防止i不存在时候错误, langchain-ChatGLM/vectorstores/MyFAISS.py", line 55, in similarity_search_with_score_by_vector _id = self.index_to_docstore_id[i] KeyError: 4 --- vectorstores/MyFAISS.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/vectorstores/MyFAISS.py b/vectorstores/MyFAISS.py index 0441789..bc8b85e 100644 --- a/vectorstores/MyFAISS.py +++ b/vectorstores/MyFAISS.py @@ -52,7 +52,11 @@ class MyFAISS(FAISS, VectorStore): if i == -1 or 0 < self.score_threshold < scores[0][j]: # This happens when not enough docs are returned. continue - _id = self.index_to_docstore_id[i] + if i in self.index_to_docstore_id: + _id = self.index_to_docstore_id[i] + # 执行接下来的操作 + else: + continue doc = self.docstore.search(_id) if (not self.chunk_conent) or ("context_expand" in doc.metadata and not doc.metadata["context_expand"]): # 匹配出的文本如果不需要扩展上下文则执行如下代码