mirror of
https://github.com/hwchase17/langchain
synced 2024-11-10 01:10:59 +00:00
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:
parent
8858846607
commit
35784d1c33
@ -27,13 +27,14 @@ def create_stuff_documents_chain(
|
||||
output_parser: Optional[BaseOutputParser] = None,
|
||||
document_prompt: Optional[BasePromptTemplate] = None,
|
||||
document_separator: str = DEFAULT_DOCUMENT_SEPARATOR,
|
||||
document_variable_name: str = DOCUMENTS_KEY,
|
||||
) -> Runnable[Dict[str, Any], Any]:
|
||||
"""Create a chain for passing a list of Documents to a model.
|
||||
|
||||
Args:
|
||||
llm: Language model.
|
||||
prompt: Prompt template. Must contain input variable "context", which will be
|
||||
used for passing in the formatted documents.
|
||||
prompt: Prompt template. Must contain input variable "context" (override by
|
||||
setting document_variable), which will be used for passing in the formatted documents.
|
||||
output_parser: Output parser. Defaults to StrOutputParser.
|
||||
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
|
||||
@ -42,6 +43,8 @@ def create_stuff_documents_chain(
|
||||
automatically retrieved from the `Document.metadata` dictionary. Default to
|
||||
a prompt that only contains `Document.page_content`.
|
||||
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:
|
||||
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:
|
||||
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 (
|
||||
RunnablePassthrough.assign(**{DOCUMENTS_KEY: format_docs}).with_config(
|
||||
RunnablePassthrough.assign(**{document_variable_name: format_docs}).with_config(
|
||||
run_name="format_inputs"
|
||||
)
|
||||
| prompt
|
||||
|
Loading…
Reference in New Issue
Block a user