如何更改LangChain默认返回的4个文档?

imj*_*esr 5 openai-api gpt-4 langchain

我有以下代码实现 LangChain + ChatGPT 以回答给定数据的问题:

import { PineconeStore } from 'langchain/vectorstores/pinecone';
import { ConversationalRetrievalQAChain } from 'langchain/chains';

const CONDENSE_PROMPT = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.

Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:`;

const QA_PROMPT = `You are a helpful AI assistant. Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say you don't know. DO NOT try to make up an answer.
If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context. Always answer in spanish.

{context}

Question: {question}
Helpful answer in markdown:`;

export const makeChain = (vectorstore: PineconeStore) => {
  const model = new OpenAI({
    temperature: 0.9, // increase temepreature to get more creative answers
    modelName: 'gpt-4', //change this to gpt-4 if you have access
  });

  const chain = ConversationalRetrievalQAChain.fromLLM(
    model,
    vectorstore.asRetriever(),
    {
      qaTemplate: QA_PROMPT,
      questionGeneratorTemplate: CONDENSE_PROMPT,
      returnSourceDocuments: false, //The number of source documents returned is 4 by default
    },
  );
  return chain;
};
Run Code Online (Sandbox Code Playgroud)

我正在处理的问题是它总是只返回 4 个文档(在我的例子中,这些是 json 文件,但我存储了 30 个)。我可以看到,即使在 LangChain 文档页面中,也使用类似的机器人,并且也仅返回源。

我的代码中的注释说“数字......默认为 4”让我认为有一种方法可以增加这个值。

我尝试过 Bard 和 ChatGPT 来寻找解决方案,但他们建议的代码不起作用。

小智 3

我知道我回答晚了,但有人可能会从中受益。在您的代码中,有一个方法被调用,asRetriever()在该方法中您传递一个参数,表示您想要返回多少个文档,例如:asRetriever(30)它将返回 30 个文档。