如何调整LangChain中ConversationalRetrievalQAChain默认返回的文档数量?
调整LangChain中ConversationalRetrievalQAChain返回的源文档数量
你当前使用LangChain结合ChatGPT和Pinecone实现问答功能时,遇到系统始终仅返回4个源文档的问题,核心原因是asRetriever()方法默认的检索文档数量(k参数)为4,调整方法如下:
解决方案
在调用vectorstore.asRetriever()时,传入配置对象指定k值,即可修改检索的文档数量。若需要在返回结果中直观看到源文档,需将returnSourceDocuments设为true。
修改后的代码示例:
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, // 在这里指定k值,示例设为10,可根据需求调整为30或其他合理数值 vectorstore.asRetriever({ k: 10 }), { qaTemplate: QA_PROMPT, questionGeneratorTemplate: CONDENSE_PROMPT, returnSourceDocuments: true, // 设为true可在结果中查看源文档,验证数量调整效果 }, ); return chain; };
关键说明
k参数:控制检索器从Pinecone中获取的相关文档数量,可根据你的业务需求设置为任意合理数值(比如匹配你存储的30个文档,可设置为30或更小的数值)。returnSourceDocuments:默认值为false,开启后返回结果会包含sourceDocuments字段,方便你直观确认检索到的文档数量和内容,验证调整是否生效。
内容的提问来源于stack exchange,提问作者imjesr
相关产品推荐
相关产品推荐

