This commit is contained in:
Bagatur
2023-12-22 16:23:05 -05:00
parent a732063a77
commit d418259865
2 changed files with 14 additions and 1 deletions

View File

@@ -14,6 +14,19 @@ from langchain.chains.combine_documents.reduce import ReduceDocumentsChain
from langchain.chains.llm import LLMChain
def create_map_reduce_documents_chain(mapdocument_input_key: str) -> Runnable:
# The chain we'll apply to each individual document.
# Returns a summary of the document.
map_chain = map_prompt | llm | StrOutputParser()
# A wrapper chain to keep the original Document metadata
map_as_doc_chain = (
RunnableParallel({"doc": RunnablePassthrough(), "content": map_chain})
| (lambda x: Document(page_content=x["content"],
metadata=x["doc"].metadata))
).with_config(run_name="Summarize (return doc)")
class MapReduceDocumentsChain(BaseCombineDocumentsChain):
"""Combining documents by mapping a chain over them, then combining results.

View File

@@ -23,8 +23,8 @@ def create_stuff_documents_chain(
llm: LanguageModelLike,
prompt: BasePromptTemplate,
output_parser: BaseOutputParser,
document_input_key: str,
*,
document_input_key: str,
document_prompt: Optional[BasePromptTemplate] = None,
document_separator: str = "\n\n",
output_key: Optional[str] = None,