You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LangChain模板传递context等参数遇Missing input keys错误求解

ConversationalRetrievalChain二次调用报错Missing context键的解决方法

问题重现

使用ConversationalRetrievalChain结合RAG实现问答功能时,首次调用rag_pipeline可正常运行,但二次调用触发以下错误:

ValueError: Missing some input keys: {'context'}

相关实现代码:

llm = HuggingFacePipeline(pipeline=generate_text)
self.vectorstore = Pinecone(self.index, self.embed_model.embed_query, "text")
self.vectorstore.similarity_search(topic, k=6)

PROMPT = None
if self.template is not None:
    PROMPT = PromptTemplate(
        template=self.template, input_variables=["chat_history", "context", "question"]
    )

chat_history = ConversationBufferMemory(output_key='answer', context_key='context',
memory_key='chat_history', return_messages=True)

self.rag_pipeline = ConversationalRetrievalChain.from_llm(
    llm=llm,
    chain_type="stuff",
    retriever=self.vectorstore.as_retriever(),
    condense_question_prompt=PROMPT,
    verbose=False,
    return_source_documents=True,
    memory=chat_history,
    get_chat_history=lambda h : h,
)

使用的模板:

You help everyone by answering questions, and improve your answers from previous answer in History.
Don't try to make up an answer, if you don't know just say that you don't know.
Answer in the same language the question was asked.
Answer in a way that is easy to understand.
Do not say "Based on the information you provided, ..." or "I think the answer is...". Just answer the question directly in detail.
Use only the following pieces of context to answer the question at the end.

History: {chat_history}

Context: {context}

Question: {question}
Answer:

错误原因

  1. Prompt参数传递错误:你的模板是用于基于上下文生成最终回答的提示,却错误传给了condense_question_prompt参数。condense_question_prompt的作用是将历史对话与当前问题浓缩为单个独立问题,它不需要context变量,因此二次调用时会因为找不到context键报错。
  2. Memory配置错误:ConversationBufferMemory设置了context_key='context',但ConversationalRetrievalChain默认不会将检索到的上下文存入对话内存,内存会期望每次调用都传入context,导致二次调用时触发缺失键的错误。

修复方案

1. 修正Prompt的传递方式

将用于生成回答的PROMPT通过chain_type_kwargs传入,而不是condense_question_prompt。如果需要自定义浓缩问题的prompt,应该单独创建一个不含context的模板。

2. 调整Memory配置

去掉context_key='context',因为对话内存不需要存储检索到的上下文;保持memory_key与模板中的chat_history变量一致即可。

修改后的完整代码

llm = HuggingFacePipeline(pipeline=generate_text)
self.vectorstore = Pinecone(self.index, self.embed_model.embed_query, "text")
self.vectorstore.similarity_search(topic, k=6)

PROMPT = None
if self.template is not None:
    PROMPT = PromptTemplate(
        template=self.template, input_variables=["chat_history", "context", "question"]
    )

# 移除context_key配置,仅保留必要参数
chat_history = ConversationBufferMemory(
    output_key='answer',
    memory_key='chat_history',
    return_messages=True
)

self.rag_pipeline = ConversationalRetrievalChain.from_llm(
    llm=llm,
    chain_type="stuff",
    retriever=self.vectorstore.as_retriever(),
    # 将PROMPT传入chain_type_kwargs,作为生成回答的提示
    chain_type_kwargs={"prompt": PROMPT},
    verbose=False,
    return_source_documents=True,
    memory=chat_history,
    get_chat_history=lambda h : h,
)

补充说明

  • 如果需要自定义浓缩问题的逻辑,可以单独创建一个仅包含chat_history和question的模板,传给condense_question_prompt参数。
  • get_chat_history函数需要确保返回的格式与模板中的chat_history变量匹配,若模板期望字符串格式的历史,可改为:
    get_chat_history=lambda h: "\n".join([f"{msg.content}" for msg in h])
    

内容的提问来源于stack exchange,提问作者Anshuman Swain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 05:33:21