提交 ed7c5485 作者: imClumsyPanda

add delete knowledge base and delete files from knowledge base to webui

上级 331f39cd
...@@ -167,6 +167,7 @@ log/* ...@@ -167,6 +167,7 @@ log/*
vector_store/* vector_store/*
content/* content/*
api_content/* api_content/*
knowledge_base/*
llm/* llm/*
embedding/* embedding/*
......
...@@ -297,6 +297,16 @@ class LocalDocQA: ...@@ -297,6 +297,16 @@ class LocalDocQA:
status = vector_store.update_doc(filepath, docs) status = vector_store.update_doc(filepath, docs)
return status return status
def list_file_from_vector_store(self,
vs_path,
fullpath=False):
vector_store = load_vector_store(vs_path, self.embeddings)
docs = vector_store.list_docs()
if fullpath:
return docs
else:
return [os.path.split(doc)[-1] for doc in docs]
if __name__ == "__main__": if __name__ == "__main__":
# 初始化消息 # 初始化消息
......
from langchain.vectorstores import FAISS from langchain.vectorstores import FAISS
from langchain.vectorstores.base import VectorStore from langchain.vectorstores.base import VectorStore
from langchain.vectorstores.faiss import dependable_faiss_import from langchain.vectorstores.faiss import dependable_faiss_import
from typing import Any, Callable, List, Tuple, Dict from typing import Any, Callable, List, Dict
from langchain.docstore.base import Docstore from langchain.docstore.base import Docstore
from langchain.docstore.document import Document from langchain.docstore.document import Document
import numpy as np import numpy as np
...@@ -109,15 +109,22 @@ class MyFAISS(FAISS, VectorStore): ...@@ -109,15 +109,22 @@ class MyFAISS(FAISS, VectorStore):
docs.append(doc) docs.append(doc)
return docs return docs
def delete_doc(self, source): def delete_doc(self, source: str or List[str]):
try: try:
ids = [k for k, v in self.docstore._dict.items() if v.metadata["source"] == source] if isinstance(source, str):
for id in ids: ids = [k for k, v in self.docstore._dict.items() if v.metadata["source"] == source]
index = list(self.index_to_docstore_id.keys())[list(self.index_to_docstore_id.values()).index(id)] else:
self.index_to_docstore_id.pop(index) ids = [k for k, v in self.docstore._dict.items() if v.metadata["source"] in source]
self.docstore._dict.pop(id) if len(ids) == 0:
return f"docs delete success" return f"docs delete fail"
except: else:
for id in ids:
index = list(self.index_to_docstore_id.keys())[list(self.index_to_docstore_id.values()).index(id)]
self.index_to_docstore_id.pop(index)
self.docstore._dict.pop(id)
return f"docs delete success"
except Exception as e:
print(e)
return f"docs delete fail" return f"docs delete fail"
def update_doc(self, source, new_docs): def update_doc(self, source, new_docs):
...@@ -125,5 +132,9 @@ class MyFAISS(FAISS, VectorStore): ...@@ -125,5 +132,9 @@ class MyFAISS(FAISS, VectorStore):
delete_len = self.delete_doc(source) delete_len = self.delete_doc(source)
ls = self.add_documents(new_docs) ls = self.add_documents(new_docs)
return f"docs update success" return f"docs update success"
except: except Exception as e:
print(e)
return f"docs update fail" return f"docs update fail"
def list_docs(self):
return list(set(v.metadata["source"] for v in self.docstore._dict.values()))
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论