Files
langchain/docs/examples/chains/map_reduce.ipynb
T
Harrison Chase 347fc49d4d Harrison/combine documents chain (#212)
combine documents chain powering vector db qa with sources chain
2022-11-30 22:00:02 -08:00

2.4 KiB

Map Reduce

This notebok showcases an example of map-reduce chains: recursive summarization.

In [1]:
from langchain import OpenAI, PromptTemplate, LLMChain
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains.mapreduce import MapReduceChain

llm = OpenAI(temperature=0)

_prompt = """Write a concise summary of the following:


{text}


CONCISE SUMMARY:"""
prompt = PromptTemplate(template=_prompt, input_variables=["text"])

text_splitter = CharacterTextSplitter()

mp_chain = MapReduceChain.from_params(llm, prompt, text_splitter)
In [2]:
with open('../state_of_the_union.txt') as f:
    state_of_the_union = f.read()
mp_chain.run(state_of_the_union)
Out [2]:
"\n\nThe President discusses the recent aggression by Russia, and the response by the United States and its allies. He announces new sanctions against Russia, and says that the free world is united in holding Putin accountable. The President also discusses the American Rescue Plan, the Bipartisan Infrastructure Law, and the Bipartisan Innovation Act. Finally, the President addresses the need for women's rights and equality for LGBTQ+ Americans."
In [ ]: