langchain icon indicating copy to clipboard operation
langchain copied to clipboard

How to use StuffDocumentsChain / Error when using third input variable

Open syberkitten opened this issue 2 years ago • 2 comments

Hi

I'm trying to use the class StuffDocumentsChain but have not seen any usage example.

I simply wish to reload existing code fragment and re-shape it (iterate).

When doing so from scratch it works fine, since the memory is provided to the next calls and the context is maintained, so work can be while in the same "session"

The problem is when I load an existing file and try to start with it, after the response arrived propery, I get this error:

raise ValueError(f"One input key expected got {prompt_input_keys}") ValueError: One input key expected got ['reshape', 'human_input']

this is my template, and I'm using {reshape} as the starting input of the file contents, but for some reason it fails, that's why i started looking into the StuffDocumentsChain.

''' {reshape}
/* {history} / / {human_input} */ '''

I also tried to change the memory before sending so to "populate it" with pre-loaded file contents, but was not able to do so.

syberkitten avatar Jan 01 '23 09:01 syberkitten

ps. Right now I'm using my own custom api to openAI, but once it becomes clear how to connect the different parts would be happy to start using LangChain again.

syberkitten avatar Jan 02 '23 07:01 syberkitten

hi @syberkitten ! sorry for the delayed response. From my understanding of what you are trying to do, something like this would probably work:

from langchain.chains.combine_documents.stuff import StuffDocumentsChain
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain.chains import LLMChain

template = '''
{input_documents}
/* {history} /
/ {human_input} */
'''
prompt = PromptTemplate(template=template, input_variables=['input_documents', 'history', 'human_input'])

llm_chain = LLMChain(llm=OpenAI(), prompt=prompt, memory=ConversationBufferMemory(input_key="human_input"))
chain = StuffDocumentsChain(llm_chain=llm_chain, document_variable_name='input_documents')

from langchain.docstore.document import Document

chain({"input_documents": [Document(page_content="foo")], "human_input": "change to bar"})

side note: the stuff documents chain is mainly meant for combining multiple documents. if you only have a single one, something like this may be simpler:

from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain.chains import LLMChain

template = '''
{reshape}
/* {history} /
/ {human_input} */
'''
prompt = PromptTemplate(template=template, input_variables=['reshape', 'history', 'human_input'])

llm_chain = LLMChain(llm=OpenAI(), prompt=prompt, memory=ConversationBufferMemory(input_key="human_input"))
llm_chain({"reshape": "foo", "human_input": "change to bar"})

does this help? if not just lmk - happy to provide more help

PS - this actually highlighted a bug in the stuff documents chain that i will work on fixing! so thank you for that :)

hwchase17 avatar Jan 02 '23 15:01 hwchase17

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

Based on my understanding, you were experiencing a ValueError when using the class StuffDocumentsChain. You mentioned that you tried changing the memory before sending, but it didn't work. hwchase17 provided a possible solution by suggesting to use a different chain if only a single document is involved. They also mentioned that they will work on fixing the bug in the stuff documents chain.

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

Thank you for your understanding and contribution to the LangChain project!

dosubot[bot] avatar Aug 19 '23 16:08 dosubot[bot]