Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine Chain Error #1724

Closed
reletreby opened this issue Mar 17, 2023 · 5 comments
Closed

Refine Chain Error #1724

reletreby opened this issue Mar 17, 2023 · 5 comments

Comments

@reletreby
Copy link

reletreby commented Mar 17, 2023

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?

@rchanggogogo
Copy link

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

@jsandlerus
Copy link

jsandlerus commented Jun 5, 2023

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'}

@lp380490944
Copy link

same issue

@adveatkarnik1
Copy link

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?

@dosubot
Copy link

dosubot bot commented Oct 25, 2023

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 dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Oct 25, 2023
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2023
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants