langchain[minor]: add document_variable_name to create_stuff_documents_chain (#24083)

- **Description:** `StuffDocumentsChain` uses `LLMChain` which is
deprecated by langchain runnables. `create_stuff_documents_chain` is the
replacement, but needs support for `document_variable_name` to allow
multiple uses of the chain within a longer chain.
- **Issue:** none
- **Dependencies:** none
This commit is contained in:
Xander Dumaine 2024-07-11 22:31:46 -04:00 committed by GitHub
parent 8858846607
commit 35784d1c33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -27,13 +27,14 @@ def create_stuff_documents_chain(
output_parser: Optional[BaseOutputParser] = None, output_parser: Optional[BaseOutputParser] = None,
document_prompt: Optional[BasePromptTemplate] = None, document_prompt: Optional[BasePromptTemplate] = None,
document_separator: str = DEFAULT_DOCUMENT_SEPARATOR, document_separator: str = DEFAULT_DOCUMENT_SEPARATOR,
document_variable_name: str = DOCUMENTS_KEY,
) -> Runnable[Dict[str, Any], Any]: ) -> Runnable[Dict[str, Any], Any]:
"""Create a chain for passing a list of Documents to a model. """Create a chain for passing a list of Documents to a model.
Args: Args:
llm: Language model. llm: Language model.
prompt: Prompt template. Must contain input variable "context", which will be prompt: Prompt template. Must contain input variable "context" (override by
used for passing in the formatted documents. setting document_variable), which will be used for passing in the formatted documents.
output_parser: Output parser. Defaults to StrOutputParser. output_parser: Output parser. Defaults to StrOutputParser.
document_prompt: Prompt used for formatting each document into a string. Input document_prompt: Prompt used for formatting each document into a string. Input
variables can be "page_content" or any metadata keys that are in all variables can be "page_content" or any metadata keys that are in all
@ -42,6 +43,8 @@ def create_stuff_documents_chain(
automatically retrieved from the `Document.metadata` dictionary. Default to automatically retrieved from the `Document.metadata` dictionary. Default to
a prompt that only contains `Document.page_content`. a prompt that only contains `Document.page_content`.
document_separator: String separator to use between formatted document strings. document_separator: String separator to use between formatted document strings.
document_variable_name: Variable name to use for the formatted documents in the prompt.
Defaults to "context".
Returns: Returns:
An LCEL Runnable. The input is a dictionary that must have a "context" key that An LCEL Runnable. The input is a dictionary that must have a "context" key that
@ -78,11 +81,12 @@ def create_stuff_documents_chain(
def format_docs(inputs: dict) -> str: def format_docs(inputs: dict) -> str:
return document_separator.join( return document_separator.join(
format_document(doc, _document_prompt) for doc in inputs[DOCUMENTS_KEY] format_document(doc, _document_prompt)
for doc in inputs[document_variable_name]
) )
return ( return (
RunnablePassthrough.assign(**{DOCUMENTS_KEY: format_docs}).with_config( RunnablePassthrough.assign(**{document_variable_name: format_docs}).with_config(
run_name="format_inputs" run_name="format_inputs"
) )
| prompt | prompt