Files
langchain/cookbook/analyze_document.ipynb
T
Bagatur 480626dc99 docs, community[patch], experimental[patch], langchain[patch], cli[pa… (#15412)
…tch]: import models from community

ran
```bash
git grep -l 'from langchain\.chat_models' | xargs -L 1 sed -i '' "s/from\ langchain\.chat_models/from\ langchain_community.chat_models/g"
git grep -l 'from langchain\.llms' | xargs -L 1 sed -i '' "s/from\ langchain\.llms/from\ langchain_community.llms/g"
git grep -l 'from langchain\.embeddings' | xargs -L 1 sed -i '' "s/from\ langchain\.embeddings/from\ langchain_community.embeddings/g"
git checkout master libs/langchain/tests/unit_tests/llms
git checkout master libs/langchain/tests/unit_tests/chat_models
git checkout master libs/langchain/tests/unit_tests/embeddings/test_imports.py
make format
cd libs/langchain; make format
cd ../experimental; make format
cd ../core; make format
```
2024-01-02 15:32:16 -05: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_community.chat_models 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."'