Langchain-Chatchat icon indicating copy to clipboard operation
Langchain-Chatchat copied to clipboard

单个文件问答的缓存机制

Open ruifengma opened this issue 1 year ago • 1 comments

单个文件问答的时候的文件加载机制是什么样的?是加载到缓存中吗?如果是的话要如何释放缓存?

ruifengma avatar Feb 27 '24 03:02 ruifengma

🤖

嗨,@ruifengma!很高兴再次看到你的问题。

在Langchain-Chatchat中,单个文件问答的文件加载机制并不涉及到缓存,而是将文件加载到数据库中。当加载一个文件时,会调用add_file_to_db函数。这个函数首先检查文件是否已经存在于数据库中。如果存在,它会更新文件的信息和版本号。如果不存在,它会将新文件添加到数据库中。同时,它还会使用add_docs_to_db函数将与文件关联的所有文档添加到数据库中。

要从数据库中释放文件(及其关联的文档),可以使用delete_file_from_db函数。这个函数会从数据库中删除文件及其所有关联的文档。它还会减少与文件关联的知识库的文件计数。

以下是相关的代码:

@with_session
def add_file_to_db(session,
                   kb_file: KnowledgeFile,
                   docs_count: int = 0,
                   custom_docs: bool = False,
                   doc_infos: List[Dict] = [],  # 形式:[{"id": str, "metadata": dict}, ...]
                   ):
    # ... code for adding or updating file in the database ...
    add_docs_to_db(kb_name=kb_file.kb_name, file_name=kb_file.filename, doc_infos=doc_infos)
    return True

@with_session
def delete_file_from_db(session, kb_file: KnowledgeFile):
    existing_file = (session.query(KnowledgeFileModel)
                     .filter(KnowledgeFileModel.file_name.ilike(kb_file.filename),
                             KnowledgeFileModel.kb_name.ilike(kb_file.kb_name))
                     .first())
    if existing_file:
        session.delete(existing_file)
        delete_docs_from_db(kb_name=kb_file.kb_name, file_name=kb_file.filename)
        session.commit()

        kb = session.query(KnowledgeBaseModel).filter(KnowledgeBaseModel.kb_name.ilike(kb_file.kb_name)).first()
        if kb:
            kb.file_count -= 1
            session.commit()
    return True

请注意,这个机制并不涉及到将文件加载到缓存中。相反,它涉及到将文件及其关联的文档加载到数据库中。

关于add_file_to_db函数的具体过程,它首先接收一个会话、一个KnowledgeFile对象、一个默认为0的docs_count、一个默认为False的custom_docs布尔值,以及一个默认为空列表的doc_infos列表。

它首先查询KnowledgeBaseModel,找到一个与KnowledgeFile对象的kb_name属性同名的知识库。

如果这样的知识库存在,它然后检查是否已经存在一个与KnowledgeFileModel中的同名和知识库名相同的文件。

如果这样的文件存在,它更新文件的修改时间、大小、文档计数和自定义文档标志,并增加文件的版本。

如果没有这样的文件,它创建一个新的KnowledgeFileModel对象,使用提供的信息,并将其添加到会话中。它还增加了知识库的文件计数。

在更新现有文件或添加新文件之后,它调用add_docs_to_db函数,将文档信息添加到FileDocModel

最后,它返回True。

关于add_docs_to_db函数的工作方式,我在仓库中没有找到答案,但答案可能在其他地方,或者我可能错过了。

Sources

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

dosubot[bot] avatar Feb 27 '24 03:02 dosubot[bot]

单独创建一个这个知识库给这个文件

zRzRzRzRzRzRzR avatar Mar 01 '24 14:03 zRzRzRzRzRzRzR