langchain/docs/modules/chains/combine_docs_examples/analyze_document.ipynb

163 lines
4.4 KiB
Plaintext
Raw Normal View History

2023-01-25 05:58:14 +00:00
{
"cells": [
{
"cell_type": "markdown",
2023-01-25 06:10:12 +00:00
"id": "ad719b65",
2023-01-25 05:58:14 +00:00
"metadata": {},
"source": [
"# Analyze Document\n",
"\n",
"The AnalyzeDocumentChain is more of an end to chain. This chain takes in a single documents, splits it up, and then runs it through a CombineDocumentsChain. This can be used as more of an end-to-end chain.\n",
"\n",
"Let's take a look at it in action below, using it summarize a long document."
]
},
{
"cell_type": "code",
"execution_count": 7,
2023-01-25 06:10:12 +00:00
"id": "765d6326",
2023-01-25 05:58:14 +00:00
"metadata": {},
"outputs": [],
"source": [
"from langchain import OpenAI\n",
"from langchain.chains.summarize import load_summarize_chain\n",
"\n",
"with open('../../state_of_the_union.txt') as f:\n",
" state_of_the_union = f.read()\n",
"\n",
"llm = OpenAI(temperature=0)\n",
"summary_chain = load_summarize_chain(llm, chain_type=\"map_reduce\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
2023-01-25 06:10:12 +00:00
"id": "3a3d3ebc",
2023-01-25 05:58:14 +00:00
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains import AnalyzeDocumentChain"
]
},
{
"cell_type": "code",
"execution_count": 6,
2023-01-25 06:10:12 +00:00
"id": "97178aad",
2023-01-25 05:58:14 +00:00
"metadata": {},
"outputs": [],
"source": [
"summarize_document_chain = AnalyzeDocumentChain(combine_docs_chain=summary_chain)"
]
},
{
"cell_type": "code",
"execution_count": 8,
2023-01-25 06:10:12 +00:00
"id": "2e5a7bf7",
2023-01-25 05:58:14 +00:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\" In this speech, President Biden addresses the American people and the world, discussing the recent aggression of Russia's Vladimir Putin in Ukraine and the US response. He outlines economic sanctions and other measures taken to hold Putin accountable, and announces the US Department of Justice's task force to go after the crimes of Russian oligarchs. He also announces the American Rescue Plan and the Bipartisan Infrastructure Law to help struggling families and create jobs, and the Bipartisan Innovation Act to make record investments in emerging technologies and American manufacturing. He calls for immigration reform, protecting the rights of women, and advancing the rights of LGBTQ+ Americans, and outlines a Unity Agenda for the Nation. He pays tribute to military families and announces the expansion of eligibility for veterans suffering from nine respiratory cancers. He concludes by expressing his optimism for the future of America.\""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"summarize_document_chain.run(state_of_the_union)"
]
},
2023-01-25 06:10:12 +00:00
{
"cell_type": "markdown",
"id": "35739404",
"metadata": {},
"source": [
"Let's take a look at this using a question answering chain."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "8b9b7705",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains.question_answering import load_qa_chain"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "60c309a8",
"metadata": {},
"outputs": [],
"source": [
"qa_chain = load_qa_chain(llm, chain_type=\"map_reduce\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "ba1fc940",
"metadata": {},
"outputs": [],
"source": [
"qa_document_chain = AnalyzeDocumentChain(combine_docs_chain=qa_chain)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "9aa1fbde",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"' The president thanked Justice Breyer for his service.'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"qa_document_chain.run(input_document=state_of_the_union, question=\"what did the president say about justice breyer?\")"
]
},
2023-01-25 05:58:14 +00:00
{
"cell_type": "code",
"execution_count": null,
2023-01-25 06:10:12 +00:00
"id": "7eb02f1e",
2023-01-25 05:58:14 +00:00
"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.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}