修复使用pgvector时get_doc_by_ids方法报错的bug及知识库文件删除后向量仍然存在的bug (#3407)

* 修复使用pgvector无法查询到document

* 修复使用pg/es向量库无法知识库删除文档后向量库并未删除对应记录的bug
This commit is contained in:
iimm 2024-03-20 08:41:41 +08:00 committed by GitHub
parent 07b7c966ed
commit 253d4b9b9a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 5 deletions

View File

@ -222,6 +222,20 @@ class KBService(ABC):
pass
return docs
def get_relative_source_path(self,filepath: str):
'''
将文件路径转化为相对路径保证查询时一致
'''
relative_path = filepath
if os.path.isabs(relative_path):
try:
relative_path = Path(filepath).relative_to(self.doc_path)
except Exception as e:
print(f"cannot convert absolute path ({source}) to relative path. error is : {e}")
relative_path = str(relative_path.as_posix().strip("/"))
return relative_path
@abstractmethod
def do_create_kb(self):
"""

View File

@ -184,7 +184,7 @@ class ESKBService(KBService):
query = {
"query": {
"term": {
"metadata.source.keyword": kb_file.filepath
"metadata.source.keyword": self.get_relative_source_path(kb_file.filepath)
}
}
}

View File

@ -28,9 +28,9 @@ class PGKBService(KBService):
def get_doc_by_ids(self, ids: List[str]) -> List[Document]:
with Session(PGKBService.engine) as session:
stmt = text("SELECT document, cmetadata FROM langchain_pg_embedding WHERE collection_id in :ids")
stmt = text("SELECT document, cmetadata FROM langchain_pg_embedding WHERE custom_id = ANY(:ids)")
results = [Document(page_content=row[0], metadata=row[1]) for row in
session.execute(stmt, {'ids': ids}).fetchall()]
session.execute(stmt, {'ids': ids}).fetchall()]
return results
def del_doc_by_ids(self, ids: List[str]) -> bool:
return super().del_doc_by_ids(ids)
@ -71,11 +71,10 @@ class PGKBService(KBService):
def do_delete_doc(self, kb_file: KnowledgeFile, **kwargs):
with Session(PGKBService.engine) as session:
filepath = kb_file.filepath.replace('\\', '\\\\')
session.execute(
text(
''' DELETE FROM langchain_pg_embedding WHERE cmetadata::jsonb @> '{"source": "filepath"}'::jsonb;'''.replace(
"filepath", filepath)))
"filepath", self.get_relative_source_path(kb_file.filepath))))
session.commit()
def do_clear_vs(self):