Files
langchain/cookbook/analyze_document.ipynb
T
Erick FriisandHarrison Chase b1fa726377 docs: langchain-openai (#15513)
Updates docs and cookbooks to import ChatOpenAI, OpenAI, and OpenAI
Embeddings from `langchain_openai`

There are likely more

---------

Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
2024-01-06 15:54:48 -08:00

2.6 KiB

Analyze a single long document

The AnalyzeDocumentChain takes in a single document, splits it up, and then runs it through a CombineDocumentsChain.

In [3]:
with open("../docs/docs/modules/state_of_the_union.txt") as f:
    state_of_the_union = f.read()
In [7]:
from langchain.chains import AnalyzeDocumentChain
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
In [8]:
from langchain.chains.question_answering import load_qa_chain

qa_chain = load_qa_chain(llm, chain_type="map_reduce")
In [9]:
qa_document_chain = AnalyzeDocumentChain(combine_docs_chain=qa_chain)
In [10]:
qa_document_chain.run(
    input_document=state_of_the_union,
    question="what did the president say about justice breyer?",
)
Out [10]:
'The President said, "Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service."'