""" This example shows how to use the map-reduce chain to summarize a document. """

import os

import langchain
from langchain_openai import ChatOpenAI
from langchain.chains.summarize import load_summarize_chain

from langchain_community.document_loaders import PyPDFLoader

from dotenv import load_dotenv
load_dotenv()

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

langchain.debug = True

llm = ChatOpenAI(
    openai_api_key=OPENAI_API_KEY, 
    model="gpt-3.5-turbo"
    )

pdf_file_path = "path/to/pdf/file"
pdf_loader = PyPDFLoader(pdf_file_path)

docs = pdf_loader.load_and_split()

chain = load_summarize_chain(llm, chain_type="map_reduce")
chain.invoke(docs)

langchain.debug = False

langchain_community.document_loaders.PyPDFLoader langchain_openai.ChatOpenAI langchain.chains.summarize.load_summarize_chain
[((318, 331), 'dotenv.load_dotenv', 'load_dotenv', ([], {}), '()\n', (329, 331), False, 'from dotenv import load_dotenv\n'), ((352, 379), 'os.getenv', 'os.getenv', (['"""OPENAI_API_KEY"""'], {}), "('OPENAI_API_KEY')\n", (361, 379), False, 'import os\n'), ((415, 479), 'langchain_openai.ChatOpenAI', 'ChatOpenAI', ([], {'openai_api_key': 'OPENAI_API_KEY', 'model': '"""gpt-3.5-turbo"""'}), "(openai_api_key=OPENAI_API_KEY, model='gpt-3.5-turbo')\n", (425, 479), False, 'from langchain_openai import ChatOpenAI\n'), ((550, 576), 'langchain_community.document_loaders.PyPDFLoader', 'PyPDFLoader', (['pdf_file_path'], {}), '(pdf_file_path)\n', (561, 576), False, 'from langchain_community.document_loaders import PyPDFLoader\n'), ((626, 676), 'langchain.chains.summarize.load_summarize_chain', 'load_summarize_chain', (['llm'], {'chain_type': '"""map_reduce"""'}), "(llm, chain_type='map_reduce')\n", (646, 676), False, 'from langchain.chains.summarize import load_summarize_chain\n')]