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

使用RetrievalQA构建求职ChatBot遇query输入键缺失错误求助

问题分析与解决方案

错误根源

出现ValueError: Missing some input keys: {'query'}的核心原因是:RetrievalQA链默认要求输入参数必须包含query键,当前配置和调用方式未满足该要求,同时PromptTemplate与Memory的参数配置也存在不匹配问题。

具体修复步骤

1. 调整PromptTemplate的输入变量与模板

RetrievalQA的stuff链会自动将检索到的context和用户输入的query传入prompt,需把求职者的偏好和简历整合到query中,并同步调整模板变量:

system_template="""You are a job applying assistance, an automated service to help applicants apply to listed jobs.

{context}

Human: {query}
AI:"""

prompt = PromptTemplate(
    input_variables=['context', 'query'],
    template=system_template
)

2. 修正Memory配置(可选,若需对话记忆)

如果需要保留对话记忆,需将ConversationBufferMemory的input_key设为query(RetrievalQA默认以query作为用户输入标识),同时指定memory_key区分记忆内容与当前输入:

memory = ConversationBufferMemory(
    llm=llm,
    input_key='query',
    memory_key='chat_history',
    return_messages=True
)

注:若使用记忆,需在system_template中加入{chat_history}占位符;若不需要记忆,可直接删除所有memory相关代码。

3. 正确调用RetrievalQA链

构造包含偏好和简历的query字符串,以query为键传入链:

try:
    if resume:
        query = f"Job preference: {preference}\nResume content:\n{resume}"
    else:
        query = f"Job preference: {preference}\nResume content:\n{text}"
        
    llm_response = qa({"query": query})
    answer = llm_response["result"]
except OpenAIError as e:
    st.error(e._message)

完整修改后的代码

system_template="""You are a job applying assistance, an automated service to help applicants apply to listed jobs.

{context}

Human: {query}
AI:"""

prompt = PromptTemplate(
    input_variables=['context', 'query'],
    template=system_template
)

llm = ChatOpenAI(model_name='gpt-3.5-turbo', openai_api_key=st.session_state.get("OPENAI_API_KEY"), temperature=0)
# 不需要记忆可删除以下3行memory配置
memory = ConversationBufferMemory(
    llm=llm, input_key='query', memory_key='chat_history', return_messages=True
)
chain_type_kwargs = {"prompt": prompt, "memory": memory} # 无记忆则改为{"prompt": prompt}
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", verbose=True, retriever=retriever, chain_type_kwargs=chain_type_kwargs)

try:
    if resume:
        query = f"Job preference: {preference}\nResume content:\n{resume}"
    else:
        query = f"Job preference: {preference}\nResume content:\n{text}"
        
    llm_response = qa({"query": query})
    answer = llm_response["result"]
except OpenAIError as e:
    st.error(e._message)

额外说明

  • 若无需对话记忆,直接删除memory相关配置即可简化代码
  • 使用qa.run(query)同样可行,run方法会自动将传入的字符串作为query参数传入链

内容的提问来源于stack exchange,提问作者Kevin Yu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 16:25:03