mirror of
				https://github.com/hwchase17/langchain.git
				synced 2025-10-31 07:41:40 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			94 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			94 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| {
 | |
|  "cells": [
 | |
|   {
 | |
|    "cell_type": "markdown",
 | |
|    "id": "d9a0131f",
 | |
|    "metadata": {},
 | |
|    "source": [
 | |
|     "# Map Reduce\n",
 | |
|     "\n",
 | |
|     "This notebok showcases an example of map-reduce chains: recursive summarization."
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": 1,
 | |
|    "id": "e9db25f3",
 | |
|    "metadata": {},
 | |
|    "outputs": [],
 | |
|    "source": [
 | |
|     "from langchain import OpenAI, PromptTemplate, LLMChain\n",
 | |
|     "from langchain.text_splitter import CharacterTextSplitter\n",
 | |
|     "from langchain.chains.mapreduce import MapReduceChain\n",
 | |
|     "\n",
 | |
|     "llm = OpenAI(temperature=0)\n",
 | |
|     "\n",
 | |
|     "_prompt = \"\"\"Write a concise summary of the following:\n",
 | |
|     "\n",
 | |
|     "\n",
 | |
|     "{text}\n",
 | |
|     "\n",
 | |
|     "\n",
 | |
|     "CONCISE SUMMARY:\"\"\"\n",
 | |
|     "prompt = PromptTemplate(template=_prompt, input_variables=[\"text\"])\n",
 | |
|     "\n",
 | |
|     "text_splitter = CharacterTextSplitter()\n",
 | |
|     "\n",
 | |
|     "mp_chain = MapReduceChain.from_params(llm, prompt, text_splitter)"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": 3,
 | |
|    "id": "99bbe19b",
 | |
|    "metadata": {},
 | |
|    "outputs": [
 | |
|     {
 | |
|      "data": {
 | |
|       "text/plain": [
 | |
|        "\"\\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.\""
 | |
|       ]
 | |
|      },
 | |
|      "execution_count": 3,
 | |
|      "metadata": {},
 | |
|      "output_type": "execute_result"
 | |
|     }
 | |
|    ],
 | |
|    "source": [
 | |
|     "with open('../state_of_the_union.txt') as f:\n",
 | |
|     "    state_of_the_union = f.read()\n",
 | |
|     "mp_chain.run(state_of_the_union)"
 | |
|    ]
 | |
|   },
 | |
|   {
 | |
|    "cell_type": "code",
 | |
|    "execution_count": null,
 | |
|    "id": "b581501e",
 | |
|    "metadata": {},
 | |
|    "outputs": [],
 | |
|    "source": []
 | |
|   }
 | |
|  ],
 | |
|  "metadata": {
 | |
|   "kernelspec": {
 | |
|    "display_name": "Python 3 (ipykernel)",
 | |
|    "language": "python",
 | |
|    "name": "python3"
 | |
|   },
 | |
|   "language_info": {
 | |
|    "codemirror_mode": {
 | |
|     "name": "ipython",
 | |
|     "version": 3
 | |
|    },
 | |
|    "file_extension": ".py",
 | |
|    "mimetype": "text/x-python",
 | |
|    "name": "python",
 | |
|    "nbconvert_exporter": "python",
 | |
|    "pygments_lexer": "ipython3",
 | |
|    "version": "3.8.7"
 | |
|   }
 | |
|  },
 | |
|  "nbformat": 4,
 | |
|  "nbformat_minor": 5
 | |
| }
 |