{ "cells": [ { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "from langchain.llms import OpenAI\n", "\n", "llm = OpenAI(model_name=\"text-davinci-002\")\n", "no_cache_llm = OpenAI(model_name=\"text-davinci-002\", cache=False)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "from langchain.text_splitter import CharacterTextSplitter\n", "from langchain.chains.mapreduce import MapReduceChain\n", "\n", "text_splitter = CharacterTextSplitter()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "with open('./txt/poem.txt') as f:\n", " state_of_the_union = f.read()\n", "texts = text_splitter.split_text(state_of_the_union)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "from langchain.docstore.document import Document\n", "docs = [Document(page_content=t) for t in texts[:3]]\n", "from langchain.chains.summarize import load_summarize_chain" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "chain = load_summarize_chain(llm, chain_type=\"map_reduce\", reduce_llm=no_cache_llm)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Retrying langchain.llms.openai.completion_with_retry.._completion_with_retry in 4.0 seconds as it raised APIConnectionError: Error communicating with OpenAI.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 367 ms, sys: 39.4 ms, total: 406 ms\n", "Wall time: 34.3 s\n" ] }, { "data": { "text/plain": [ "'\\n\\nA young woman in Suzhou is pining for her lover who has left her. She spends her days drinking and looking at the moon, hoping he will return to her.'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "chain.run(docs)" ] } ], "metadata": { "kernelspec": { "display_name": "base", "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.10.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }