知识库查询 #3454
Replies: 1 comment
-
嘿,@LCR997!很高兴再次看到你的问题。希望你一切都好! 在Langchain-Chatchat仓库中,如果你想在知识库的查询条件中加入对文件的时间限定条件,你需要在 然而,提供的代码并未直接涉及到与文件元数据(如创建或修改日期)的交互,这些元数据是实现时间限制所必需的。为了添加这样的限制,你需要修改文件列表函数,以便根据文件的元数据来过滤文件。 以下是一个概念性的例子,展示了如何修改 import os
import time
from datetime import datetime, timedelta
def list_files_with_time_constraint(
knowledge_base_name: str,
days_ago: int = 7 # 默认列出在过去7天内修改的文件
) -> ListResponse:
if not validate_kb_name(knowledge_base_name):
return ListResponse(code=403, msg="Don't attack me", data=[])
knowledge_base_name = urllib.parse.unquote(knowledge_base_name)
kb = KBServiceFactory.get_service_by_name(knowledge_base_name)
if kb is None:
return ListResponse(code=404, msg=f"未找到知识库 {knowledge_base_name}", data=[])
else:
all_doc_names = kb.list_files()
filtered_doc_names = []
cutoff_date = datetime.now() - timedelta(days=days_ago)
for doc_name in all_doc_names:
file_path = get_file_path(knowledge_base_name=knowledge_base_name, doc_name=doc_name)
if os.path.exists(file_path):
file_mod_time = datetime.fromtimestamp(os.path.getmtime(file_path))
if file_mod_time >= cutoff_date:
filtered_doc_names.append(doc_name)
return ListResponse(data=filtered_doc_names) 在这个例子中, 请记住,这只是一个概念性的例子。你需要在实际的代码库中集成和测试它,确保它满足Langchain-Chatchat项目的具体要求。 参考资料: Sources
|
Beta Was this translation helpful? Give feedback.
-
如果我想对知识库的查询条件进行修改,比如加入对知识库中的文件的时间限定条件,我需要在哪个函数中修改以及怎么修改?
Beta Was this translation helpful? Give feedback.
All reactions