Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RetrievalQA ValueError: Missing some input keys: {'query'}

I tried to build a chatbot for job applicants by using the RetrievalQA chain and retrieve info from a csv file. The preference is a string, resume, and text under the try statement are all strings. I don't have any clue why I got the error ValueError: Missing some input keys: {'query'}.

system_template="""You are a job applying assistance, an automated service to help applicats to apply to listed jobs. \
        You are given a resume and a job preference. \
        
        {context}
        
        Human: {resume}
        Ai:"""

prompt = PromptTemplate(
        input_variables = ['context', 'resume'],
        template = system_template
    )
    
llm = ChatOpenAI(model_name='gpt-3.5-turbo', openai_api_key=st.session_state.get("OPENAI_API_KEY"), temperature=0)
memory = ConversationBufferMemory(llm=llm, input_key='resume')
chain_type_kwargs = {"prompt": prompt, "memory": memory}
qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", verbose=True, retriever=retriever, chain_type_kwargs=chain_type_kwargs)

try:
    if resume:
            query = "job_preference:" + preference + "\n The resume is as following:"+ resume
    else:
            query = "job_preference:" + preference + "\n The resume is as following:"+ text
            
    llm_response = qa({"resume":query})
    answer = llm_response["result"]
except OpenAIError as e:
        st.error(e._message)

I also tried to run the chain with qa.run(query) and 'qa({"resume":resume, "query":preference})', and the error 'Missing input keys' error keep showing up.

Am I setting up the PromptTemplate/chain_type_kwargs wrong? Does anyone else met this problem? Thanks!

like image 478
Kevin Yu Avatar asked Nov 17 '25 14:11

Kevin Yu


1 Answers

The solution that is working for me is:

In template, include your question (HumanPrompt) as {question}

For example:

template = """ you are an information extractor. Answer the question.
Context: {context}
Question: {question}
Answer:"""

Then in the qa, put your question as value of key "query" For example

result = qa({"query": "Tell me about water sector?"})

I believe this is due to the fact that the variable names are hard-coded inside RetrievalQA source code

class BaseRetrievalQA(Chain):
    """Base class for question-answering chains."""

    combine_documents_chain: BaseCombineDocumentsChain
    """Chain to use to combine the documents."""
    input_key: str = "query"  #: :meta private:
    output_key: str = "result"  #: :meta private:

=> input_key = "query" seems to be the default key.

I haven't found how the code seems to be able to connect my {question} var and {query} var yet, but the above code generates correct result

like image 181
open doc Avatar answered Nov 20 '25 05:11

open doc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!