langchain
langchain copied to clipboard
RetrievalQA Prompt Error: Missing some input keys
I have created a RetrievalQA Chain, but facing an issue. When calling the Chain, I get the following error: ValueError: Missing some input keys: {'query', 'typescript_string'}
My code looks as follows:
from langchain_community.embeddings import HuggingFaceEmbeddings
embeddings = HuggingFaceEmbeddings(model_name="intfloat/multilingual-e5-large",
model_kwargs={'device': 'mps'}, encode_kwargs={'device': 'mps', 'batch_size': 32})
vectorstore = FAISS.load_local("vectorstore/db_faiss_bkb", embeddings)
retriever = vectorstore.as_retriever(search_kwargs={'k': 1, 'score_treshold': 0.75}, search_type="similarity")
llm = build_llm("modelle/mixtral-8x7b-instruct-v0.1.Q5_K_M.gguf")
def build_retrieval_qa(llm, prompt, vectordb):
chain_type_kwargs={ "prompt": prompt,
"verbose": False
}
dbqa = RetrievalQA.from_chain_type(llm=llm,
chain_type='stuff',
retriever=vectordb,
return_source_documents=True,
chain_type_kwargs=chain_type_kwargs,
verbose=True
)
return dbqa
prompt = """
<s> [INST] You are getting a task and a User input. If there is relevant information in the context, please add this information to your answer.
### Here the Task: ###
{typescript_string}
### Here the context: ###
{context}
### Here the User Input: ###
{query}
Answer: [/INST]
"""
prompt_temp = PromptTemplate(template=prompt, input_variables=["typescript_string", "context", "query"])
dbqa1 = build_retrieval_qa(llm=llm,prompt=prompt_temp,vectordb=retriever)
question = "What is IGB?"
types = "Answer shortly!"
dbqa1({"query": question, "typescript_string": types})
With this code the error from above occurs in the last line here. The weird thing is, that it is working with a LLM-Chain from Langchain without Retrieval:
from langchain.chains import LLMChain
llm_chain = LLMChain(
llm=llm,
prompt= prompt_temp,
verbose=True,
)
test = llm_chain({"type_string": types, "input": question})
test
This works and I am getting a correct response. I am using
Langchain == 0.1.0
So is there something wrong with my PromptTemplate or is this a bug?
🤖
Based on the code and error message you've provided, it seems like the RetrievalQA
chain is expecting a dictionary with the keys 'query' and 'typescript_string'. The error ValueError: Missing some input keys: {'query', 'typescript_string'}
is raised when these keys are not present in the input dictionary.
In the BaseRetrievalQA
class of LangChain, the 'query' key is used to extract the question from the inputs:
class BaseRetrievalQA(Chain):
"""Base class for question-answering chains."""
input_key: str = "query" #: :meta private:
In the _call
and _acall
methods of the BaseRetrievalQA
class, the 'query' key is used:
def _call(
self,
inputs: Dict[str, Any],
run_manager: Optional[CallbackManagerForChainRun] = None,
) -> Dict[str, Any]:
"""Run get_relevant_text and llm on input query."""
question = inputs[self.input_key]
async def _acall(
self,
inputs: Dict[str, Any],
run_manager: Optional[AsyncCallbackManagerForChainRun] = None,
) -> Dict[str, Any]:
"""Run get_relevant_text and llm on input query."""
question = inputs[self.input_key]
If the 'query' key is not present in the input dictionary, a ValueError
will be raised.
The 'typescript_string' key is not mentioned in the provided context, so it's unclear why this key is expected. It's possible that this key is required elsewhere in your code or in another part of the LangChain framework not shown in the provided context.
To resolve this issue, ensure that the input dictionary you're passing to the RetrievalQA
chain includes a 'query' key. For example:
retrievalQA = RetrievalQA.from_llm(llm=OpenAI(), retriever=retriever)
res = retrievalQA({'query': 'This is my query'})
If the 'typescript_string' key is indeed required, you'll need to include this key in the input dictionary as well.
As for the 'typescript_string' key, I wasn't able to find specific information about its role in the LangChain framework within the repository. It might be used in a different part of your code or in another part of the LangChain framework. If you could provide more information about where this key is used, it would be helpful in diagnosing the issue.
You mentioned that you're using LangChain version 0.1.0. The latest version of LangChain is 0.1.1. It might be worth updating to the latest version to see if this resolves your issue.
I hope this helps! If you have any other questions or need further clarification, feel free to ask.
Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.