langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Refine Chain Error

Open reletreby opened this issue 1 year ago • 1 comments

import os, pdb
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter, TokenTextSplitter
from langchain.vectorstores import Milvus
from langchain.document_loaders import TextLoader
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI, OpenAIChat


os.environ["TOKENIZERS_PARALLELISM"] = "false"

f_path = "doc.txt"

loader = TextLoader(f_path)
documents = loader.load()
text_splitter = TokenTextSplitter(chunk_size=512, chunk_overlap=20)
docs = text_splitter.split_documents(documents)

embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")


vector_db = Milvus.from_documents(
    documents,
    embeddings,
    connection_args={"host": "localhost", "port": "19530"},
)

query = "Who is the author of this study?"

docs = vector_db.similarity_search(query, 2)

chain = load_qa_chain(
    OpenAIChat(model_name="gpt-3.5-turbo", temperature=0.1), chain_type="refine"
)
print(chain.run(input_documents=docs, question=query))

Gives ValueError: Missing some input keys: {'existing_answer'}

Not sure what's going wrong. I suppose for refine chain the first call should not be looking for existing_answer, right?

reletreby avatar Mar 17 '23 02:03 reletreby

import os, pdb
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import CharacterTextSplitter, TokenTextSplitter
from langchain.vectorstores import Milvus
from langchain.document_loaders import TextLoader
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI, OpenAIChat


os.environ["TOKENIZERS_PARALLELISM"] = "false"

f_path = "doc.txt"

loader = TextLoader(f_path)
documents = loader.load()
text_splitter = TokenTextSplitter(chunk_size=512, chunk_overlap=20)
docs = text_splitter.split_documents(documents)

embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")


vector_db = Milvus.from_documents(
    documents,
    embeddings,
    connection_args={"host": "localhost", "port": "19530"},
)

query = "Who is the author of this study?"

docs = vector_db.similarity_search(query, 2)

chain = load_qa_chain(
    OpenAIChat(model_name="gpt-3.5-turbo", temperature=0.1), chain_type="refine"
)
print(chain.run(input_documents=docs, question=query))

Gives ValueError: Missing some input keys: {'existing_answer'}

Not sure what's going wrong. I suppose for refine chain the first call should not be looking for existing_answer, right?

Hi, I think this because the refine chain use the default Prompt Template DEFAULT_REFINE_PROMPT = PromptTemplate( input_variables=["question", "existing_answer", "context_str"], template=DEFAULT_REFINE_PROMPT_TMPL, ), so you can solve this problem by define your own refine prompt template

rchanggogogo avatar Apr 15 '23 05:04 rchanggogogo

I've had a similar issue using chain_type: refine and openAI with Pinecone. My error was related to context being empty. What I've found so far is this error happens only when the vector search in pinecone returns empty, making the context empty. I believe the default value of context needs to be changed to avoid this issue.

If Pinecone returns some vectors, I don't have this issue. I have not tried to create my own template. I need to research a little bit more on this.


ValueError Traceback (most recent call last) Cell In[14], line 2 1 query = "What did the president say about Ketanji Brown Jackson" ----> 2 result = qa({"question": query}) 3 print(result)

File ~\AppData\Roaming\Python\Python311\site-packages\langchain\chains\base.py:140, in Chain.call(self, inputs, return_only_outputs, callbacks) 138 except (KeyboardInterrupt, Exception) as e: 139 run_manager.on_chain_error(e) --> 140 raise e 141 run_manager.on_chain_end(outputs) 142 return self.prep_outputs(inputs, outputs, return_only_outputs)

File ~\AppData\Roaming\Python\Python311\site-packages\langchain\chains\base.py:134, in Chain.call(self, inputs, return_only_outputs, callbacks) 128 run_manager = callback_manager.on_chain_start( 129 {"name": self.class.name}, 130 inputs, 131 ) 132 try: 133 outputs = ( --> 134 self._call(inputs, run_manager=run_manager) 135 if new_arg_supported 136 else self._call(inputs) 137 ) ... 81 missing_keys = set(self.input_keys).difference(inputs) 82 if missing_keys: ---> 83 raise ValueError(f"Missing some input keys: {missing_keys}")

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

jsandlerus avatar Jun 05 '23 17:06 jsandlerus

same issue

lp380490944 avatar Jun 23 '23 18:06 lp380490944

I am using FewShot learning template example_template = """ User: {question} AI: {answer} """ example_prompt = PromptTemplate( input_variables=["question", "answer"], template=example_template ) few_shot_prompt_template = FewShotPromptTemplate( examples=examples, example_prompt=example_prompt, prefix=prefix, suffix=suffix, input_variables=["question"], example_separator="\n\n" ) memory = ConversationBufferMemory( memory_key='chat_history', return_messages=True ) chain = LLMChain(llm=chat, prompt=few_shot_prompt_template,verbose=True,memory=memory) I am doing chain.predict and it is giving me a valueerror, saying that

Missing some input keys: {'question'}

Could you please let me know where I might be going wrong?

adveatkarnik1 avatar Jul 26 '23 06:07 adveatkarnik1

Hi, @reletreby! I'm Dosu, and I'm helping the LangChain team manage their backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, the issue you reported is about a ValueError being raised with the message "Missing some input keys: {'existing_answer'}" when running the code. There have been some suggestions in the comments on how to resolve this issue. One user recommends defining a custom template for the refine chain, while another suggests changing the default value of the context in the refine chain. Additionally, another user reports a similar issue with the FewShot learning template and asks for guidance.

Before we close this issue, we wanted to check with you if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on this issue. Otherwise, feel free to close the issue yourself or it will be automatically closed in 7 days.

Thank you for your contribution to the LangChain repository!

dosubot[bot] avatar Oct 25 '23 16:10 dosubot[bot]