Skip to content

Commit

Permalink
delete_docs (chatchat-space#499)
Browse files Browse the repository at this point in the history
删除文档的时候重新生成知识库(FAISS)内的数据
  • Loading branch information
yihuaxiang authored May 30, 2023
1 parent 8d669cd commit 8477368
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ async def list_docs(

async def delete_docs(
knowledge_base_id: str = Query(...,
description="Knowledge Base Name(注意此方法仅删除上传的文件并不会删除知识库(FAISS)内数据)",
description="Knowledge Base Name",
example="kb1"),
doc_name: Optional[str] = Query(
None, description="doc name", example="doc_name_1.pdf"
Expand All @@ -188,18 +188,20 @@ async def delete_docs(
doc_path = get_file_path(knowledge_base_id, doc_name)
if os.path.exists(doc_path):
os.remove(doc_path)

# 删除上传的文件后重新生成知识库(FAISS)内的数据
remain_docs = await list_docs(knowledge_base_id)
if len(remain_docs.data) == 0:
shutil.rmtree(get_folder_path(knowledge_base_id), ignore_errors=True)
else:
local_doc_qa.init_knowledge_vector_store(
get_folder_path(knowledge_base_id), get_vs_path(knowledge_base_id)
)

return BaseResponse(code=200, msg=f"document {doc_name} delete success")
else:
return BaseResponse(code=1, msg=f"document {doc_name} not found")

remain_docs = await list_docs(knowledge_base_id)
remain_docs = remain_docs.json()
if len(remain_docs["data"]) == 0:
shutil.rmtree(get_folder_path(knowledge_base_id), ignore_errors=True)
else:
local_doc_qa.init_knowledge_vector_store(
get_folder_path(knowledge_base_id), get_vs_path(knowledge_base_id)
)
else:
shutil.rmtree(get_folder_path(knowledge_base_id))
return BaseResponse(code=200, msg=f"Knowledge Base {knowledge_base_id} delete success")
Expand Down

0 comments on commit 8477368

Please sign in to comment.