Spaces:
Running
Running
File size: 4,538 Bytes
cfd3735 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
{
"cells": [
{
"cell_type": "markdown",
"id": "ad719b65",
"metadata": {},
"source": [
"# Analyze Document\n",
"\n",
"The AnalyzeDocumentChain is more of an end to chain. This chain takes in a single document, splits it up, and then runs it through a CombineDocumentsChain. This can be used as more of an end-to-end chain."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "15e1a8a2",
"metadata": {},
"outputs": [],
"source": [
"with open(\"../../state_of_the_union.txt\") as f:\n",
" state_of_the_union = f.read()"
]
},
{
"cell_type": "markdown",
"id": "14da4012",
"metadata": {},
"source": [
"## Summarize\n",
"Let's take a look at it in action below, using it summarize a long document."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "765d6326",
"metadata": {},
"outputs": [],
"source": [
"from langchain import OpenAI\n",
"from langchain.chains.summarize import load_summarize_chain\n",
"\n",
"llm = OpenAI(temperature=0)\n",
"summary_chain = load_summarize_chain(llm, chain_type=\"map_reduce\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "3a3d3ebc",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains import AnalyzeDocumentChain"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "97178aad",
"metadata": {},
"outputs": [],
"source": [
"summarize_document_chain = AnalyzeDocumentChain(combine_docs_chain=summary_chain)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2e5a7bf7",
"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 plans to fight inflation and lower costs for families, invest in American manufacturing, and provide military, economic, and humanitarian assistance to Ukraine. He calls for immigration reform, protecting the rights of women, and advancing the rights of LGBTQ+ Americans, and pays tribute to military families. He concludes with optimism for the future of America.\""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"summarize_document_chain.run(state_of_the_union)"
]
},
{
"cell_type": "markdown",
"id": "35739404",
"metadata": {},
"source": [
"## Question Answering\n",
"Let's take a look at this using a question answering chain."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "8b9b7705",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains.question_answering import load_qa_chain"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "60c309a8",
"metadata": {},
"outputs": [],
"source": [
"qa_chain = load_qa_chain(llm, chain_type=\"map_reduce\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "ba1fc940",
"metadata": {},
"outputs": [],
"source": [
"qa_document_chain = AnalyzeDocumentChain(combine_docs_chain=qa_chain)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "9aa1fbde",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"' The president thanked Justice Breyer for his service.'"
]
},
"execution_count": 9,
"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?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7eb02f1e",
"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.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|