chatchat-space / Langchain-Chatchat

Langchain-Chatchat(原Langchain-ChatGLM)基于 Langchain 与 ChatGLM, Qwen 与 Llama 等语言模型的 RAG 与 Agent 应用 | Langchain-Chatchat (formerly langchain-ChatGLM), local knowledge based LLM (like ChatGLM, Qwen and Llama) RAG and Agent app with langchain
Apache License 2.0
31.45k stars 5.48k forks source link

研究了知识库模块的代码,未找到faiss_kb_service.py中similarity_search_with_score_by_vector方法的代码 #2764

Closed Harrold-Lee closed 8 months ago

Harrold-Lee commented 8 months ago

问题描述 / Problem Description 未找到faiss_kb_service.py中similarity_search_with_score_by_vector方法的代码

复现问题的步骤 / Steps to Reproduce

  1. 执行 'python startup.py -a'
  2. 点击 '知识库问答'
  3. 滚动到 '...' / Scroll to '...'
  4. 问题出现 能够完成相关文本匹配,但在研究代码时,未找到vs.similarity_search_with_score_by_vector方法的内容,不知道使用的是哪种相似度度量方法。 image

预期的结果 / Expected Result 能够打开similarity_search_with_score_by_vector方法的代码块

实际结果 / Actual Result 提示无相关代码 image

环境信息 / Environment Information

附加信息 / Additional Information 添加与问题相关的任何其他信息 / Add any other information related to the issue.

hydai99 commented 8 months ago

这是langchain的函数 参考:https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/vectorstores/faiss.py

Harrold-Lee commented 8 months ago

这是langchain的函数 参考:https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/vectorstores/faiss.py

非常感谢您,我是小白,我想请教一下,在chatchat项目中,使用vs.similarity_search_with_score_by_vector来查询相关文档时,无需在项目中包含这个方法的定义吗?

hydai99 commented 8 months ago

这是langchain的函数 参考:https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/vectorstores/faiss.py

非常感谢您,我是小白,我想请教一下,在chatchat项目中,使用vs.similarity_search_with_score_by_vector来查询相关文档时,无需在项目中包含这个方法的定义吗?

前面的代码有应该已经调用了。你说的定义是?

lurenyi4 commented 8 months ago

我记得是L2

lurenyi4 commented 8 months ago

server/knowledge_base/kb_cache/faiss_cache.py

class KBFaissPool(_FaissPool): def load_vector_store( self, kb_name: str, vector_name: str = None, create: bool = True, embed_model: str = EMBEDDING_MODEL, embed_device: str = embedding_device(), ) -> ThreadSafeFaiss: self.atomic.acquire() vector_name = vector_name or embed_model cache = self.get((kb_name, vector_name)) # 用元组比拼接字符串好一些 if cache is None: item = ThreadSafeFaiss((kb_name, vector_name), pool=self) self.set((kb_name, vector_name), item) with item.acquire(msg="初始化"): self.atomic.release() logger.info(f"loading vector store in '{kb_name}/vector_store/{vector_name}' from disk.") vs_path = get_vs_path(kb_name, vector_name)

            if os.path.isfile(os.path.join(vs_path, "index.faiss")):
                embeddings = self.load_kb_embeddings(kb_name=kb_name, embed_device=embed_device, default_embed_model=embed_model)
                vector_store = FAISS.load_local(vs_path, embeddings, normalize_L2=True,distance_strategy="METRIC_INNER_PRODUCT")
            elif create:
                # create an empty vector store
                if not os.path.exists(vs_path):
                    os.makedirs(vs_path)
                vector_store = self.new_vector_store(embed_model=embed_model, embed_device=embed_device)
                vector_store.save_local(vs_path)
            else:
                raise RuntimeError(f"knowledge base {kb_name} not exist.")
            item.obj = vector_store
            item.finish_loading()
    else:
        self.atomic.release()
    return self.get((kb_name, vector_name))
Harrold-Lee commented 8 months ago

这是langchain的函数 参考:https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/vectorstores/faiss.py

非常感谢您,我是小白,我想请教一下,在chatchat项目中,使用vs.similarity_search_with_score_by_vector来查询相关文档时,无需在项目中包含这个方法的定义吗?

前面的代码有应该已经调用了。你说的定义是?

因为我在整个项目文件中未找到similarity_search_with_score_by_vector这个方法的代码,并且仅在我问题中给出的位置进行了一次调用,所以我比较疑惑为什么项目中没有这个方法的相关代码,但却可以调用这个方法来获得候选文档。

hydai99 commented 8 months ago

这是langchain的函数 参考:https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/vectorstores/faiss.py

非常感谢您,我是小白,我想请教一下,在chatchat项目中,使用vs.similarity_search_with_score_by_vector来查询相关文档时,无需在项目中包含这个方法的定义吗?

前面的代码有应该已经调用了。你说的定义是?

因为我在整个项目文件中未找到similarity_search_with_score_by_vector这个方法的代码,并且仅在我问题中给出的位置进行了一次调用,所以我比较疑惑为什么项目中没有这个方法的相关代码,但却可以调用这个方法来获得候选文档。

因为它调用的faiss是来自langchain的, from langchain.vectorstores.faiss import FAISS。这个库里面有定义这个方法,这里直接调用就好了 1705996704968