yasetevo commited on
Commit
c16a7ab
1 Parent(s): 88d8f8e

Upload LangChain_QA_Panel_App.ipynb

Browse files
Files changed (1) hide show
  1. LangChain_QA_Panel_App.ipynb +270 -0
LangChain_QA_Panel_App.ipynb ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "04815d1b-44ee-4bd3-878e-fa0c3bf9fa7f",
6
+ "metadata": {
7
+ "tags": [],
8
+ "id": "04815d1b-44ee-4bd3-878e-fa0c3bf9fa7f"
9
+ },
10
+ "source": [
11
+ "# LangChain QA Panel App\n",
12
+ "\n",
13
+ "This notebook shows how to make this app:"
14
+ ]
15
+ },
16
+ {
17
+ "cell_type": "code",
18
+ "execution_count": null,
19
+ "id": "a181568b-9cde-4a55-a853-4d2a41dbfdad",
20
+ "metadata": {
21
+ "tags": [],
22
+ "id": "a181568b-9cde-4a55-a853-4d2a41dbfdad"
23
+ },
24
+ "outputs": [],
25
+ "source": [
26
+ "#!pip install langchain openai chromadb tiktoken pypdf panel\n"
27
+ ]
28
+ },
29
+ {
30
+ "cell_type": "code",
31
+ "execution_count": null,
32
+ "id": "9a464409-d064-4766-a9cb-5119f6c4b8f5",
33
+ "metadata": {
34
+ "tags": [],
35
+ "id": "9a464409-d064-4766-a9cb-5119f6c4b8f5"
36
+ },
37
+ "outputs": [],
38
+ "source": [
39
+ "import os \n",
40
+ "from langchain.chains import RetrievalQA\n",
41
+ "from langchain.llms import OpenAI\n",
42
+ "from langchain.document_loaders import TextLoader\n",
43
+ "from langchain.document_loaders import PyPDFLoader\n",
44
+ "from langchain.indexes import VectorstoreIndexCreator\n",
45
+ "from langchain.text_splitter import CharacterTextSplitter\n",
46
+ "from langchain.embeddings import OpenAIEmbeddings\n",
47
+ "from langchain.vectorstores import Chroma\n",
48
+ "import panel as pn\n",
49
+ "import tempfile\n"
50
+ ]
51
+ },
52
+ {
53
+ "cell_type": "code",
54
+ "execution_count": null,
55
+ "id": "b2d07ea5-9ff2-4c96-a8dc-92895d870b73",
56
+ "metadata": {
57
+ "tags": [],
58
+ "id": "b2d07ea5-9ff2-4c96-a8dc-92895d870b73"
59
+ },
60
+ "outputs": [],
61
+ "source": [
62
+ "pn.extension('texteditor', template=\"bootstrap\", sizing_mode='stretch_width')\n",
63
+ "pn.state.template.param.update(\n",
64
+ " main_max_width=\"690px\",\n",
65
+ " header_background=\"#F08080\",\n",
66
+ ")"
67
+ ]
68
+ },
69
+ {
70
+ "cell_type": "code",
71
+ "execution_count": null,
72
+ "id": "763db4d0-3436-41d3-8b0f-e66ce16468cd",
73
+ "metadata": {
74
+ "tags": [],
75
+ "id": "763db4d0-3436-41d3-8b0f-e66ce16468cd"
76
+ },
77
+ "outputs": [],
78
+ "source": [
79
+ "file_input = pn.widgets.FileInput(width=300)\n",
80
+ "\n",
81
+ "openaikey = pn.widgets.PasswordInput(\n",
82
+ " value=\"\", placeholder=\"Enter your OpenAI API Key here...\", width=300\n",
83
+ ")\n",
84
+ "prompt = pn.widgets.TextEditor(\n",
85
+ " value=\"\", placeholder=\"Enter your questions here...\", height=160, toolbar=False\n",
86
+ ")\n",
87
+ "run_button = pn.widgets.Button(name=\"Run!\")\n",
88
+ "\n",
89
+ "select_k = pn.widgets.IntSlider(\n",
90
+ " name=\"Number of relevant chunks\", start=1, end=5, step=1, value=2\n",
91
+ ")\n",
92
+ "select_chain_type = pn.widgets.RadioButtonGroup(\n",
93
+ " name='Chain type', \n",
94
+ " options=['stuff', 'map_reduce', \"refine\", \"map_rerank\"]\n",
95
+ ")\n",
96
+ "\n",
97
+ "widgets = pn.Row(\n",
98
+ " pn.Column(prompt, run_button, margin=5),\n",
99
+ " pn.Card(\n",
100
+ " \"Chain type:\",\n",
101
+ " pn.Column(select_chain_type, select_k),\n",
102
+ " title=\"Advanced settings\", margin=10\n",
103
+ " ), width=600\n",
104
+ ")"
105
+ ]
106
+ },
107
+ {
108
+ "cell_type": "code",
109
+ "execution_count": null,
110
+ "id": "9b83cc06-3401-498f-8f84-8a98370f3121",
111
+ "metadata": {
112
+ "tags": [],
113
+ "id": "9b83cc06-3401-498f-8f84-8a98370f3121"
114
+ },
115
+ "outputs": [],
116
+ "source": [
117
+ "def qa(file, query, chain_type, k):\n",
118
+ " # load document\n",
119
+ " loader = PyPDFLoader(file)\n",
120
+ " documents = loader.load()\n",
121
+ " # split the documents into chunks\n",
122
+ " text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
123
+ " texts = text_splitter.split_documents(documents)\n",
124
+ " # select which embeddings we want to use\n",
125
+ " embeddings = OpenAIEmbeddings()\n",
126
+ " # create the vectorestore to use as the index\n",
127
+ " db = Chroma.from_documents(texts, embeddings)\n",
128
+ " # expose this index in a retriever interface\n",
129
+ " retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"k\": k})\n",
130
+ " # create a chain to answer questions \n",
131
+ " qa = RetrievalQA.from_chain_type(\n",
132
+ " llm=OpenAI(), chain_type=chain_type, retriever=retriever, return_source_documents=True)\n",
133
+ " result = qa({\"query\": query})\n",
134
+ " print(result['result'])\n",
135
+ " return result"
136
+ ]
137
+ },
138
+ {
139
+ "cell_type": "code",
140
+ "execution_count": null,
141
+ "id": "2722f43b-daf6-4d17-a842-41203ae9b140",
142
+ "metadata": {
143
+ "tags": [],
144
+ "id": "2722f43b-daf6-4d17-a842-41203ae9b140"
145
+ },
146
+ "outputs": [],
147
+ "source": [
148
+ "# result = qa(\"example.pdf\", \"what is the total number of AI publications?\")"
149
+ ]
150
+ },
151
+ {
152
+ "cell_type": "code",
153
+ "execution_count": null,
154
+ "id": "60e1b3d3-c0d2-4260-ae0c-26b03f1b8824",
155
+ "metadata": {
156
+ "id": "60e1b3d3-c0d2-4260-ae0c-26b03f1b8824"
157
+ },
158
+ "outputs": [],
159
+ "source": [
160
+ "convos = [] # store all panel objects in a list\n",
161
+ "\n",
162
+ "def qa_result(_):\n",
163
+ " os.environ[\"OPENAI_API_KEY\"] = openaikey.value\n",
164
+ " \n",
165
+ " # save pdf file to a temp file \n",
166
+ " if file_input.value is not None:\n",
167
+ " file_input.save(\"/.cache/temp.pdf\")\n",
168
+ " \n",
169
+ " prompt_text = prompt.value\n",
170
+ " if prompt_text:\n",
171
+ " result = qa(file=\"/.cache/temp.pdf\", query=prompt_text, chain_type=select_chain_type.value, k=select_k.value)\n",
172
+ " convos.extend([\n",
173
+ " pn.Row(\n",
174
+ " pn.panel(\"\\U0001F60A\", width=10),\n",
175
+ " prompt_text,\n",
176
+ " width=600\n",
177
+ " ),\n",
178
+ " pn.Row(\n",
179
+ " pn.panel(\"\\U0001F916\", width=10),\n",
180
+ " pn.Column(\n",
181
+ " result[\"result\"],\n",
182
+ " \"Relevant source text:\",\n",
183
+ " pn.pane.Markdown('\\n--------------------------------------------------------------------\\n'.join(doc.page_content for doc in result[\"source_documents\"]))\n",
184
+ " )\n",
185
+ " )\n",
186
+ " ])\n",
187
+ " #return convos\n",
188
+ " return pn.Column(*convos, margin=15, width=575, min_height=400)\n"
189
+ ]
190
+ },
191
+ {
192
+ "cell_type": "code",
193
+ "execution_count": null,
194
+ "id": "c3a70857-0b98-4f62-a9c0-b62ca42b474c",
195
+ "metadata": {
196
+ "tags": [],
197
+ "id": "c3a70857-0b98-4f62-a9c0-b62ca42b474c"
198
+ },
199
+ "outputs": [],
200
+ "source": [
201
+ "qa_interactive = pn.panel(\n",
202
+ " pn.bind(qa_result, run_button),\n",
203
+ " loading_indicator=True,\n",
204
+ ")"
205
+ ]
206
+ },
207
+ {
208
+ "cell_type": "code",
209
+ "execution_count": null,
210
+ "id": "228e2b42-b1ed-43af-b923-031a70241ab0",
211
+ "metadata": {
212
+ "tags": [],
213
+ "id": "228e2b42-b1ed-43af-b923-031a70241ab0"
214
+ },
215
+ "outputs": [],
216
+ "source": [
217
+ "output = pn.WidgetBox('*Output will show up here:*', qa_interactive, width=630, scroll=True)"
218
+ ]
219
+ },
220
+ {
221
+ "cell_type": "code",
222
+ "execution_count": null,
223
+ "id": "1b0ec253-2bcd-4f91-96d8-d8456e900a58",
224
+ "metadata": {
225
+ "tags": [],
226
+ "id": "1b0ec253-2bcd-4f91-96d8-d8456e900a58"
227
+ },
228
+ "outputs": [],
229
+ "source": [
230
+ "# layout\n",
231
+ "pn.Column(\n",
232
+ " pn.pane.Markdown(\"\"\"\n",
233
+ " ## \\U0001F60A! Question Answering with your PDF file\n",
234
+ " \n",
235
+ " 1) Upload a PDF. 2) Enter OpenAI API key. This costs $. Set up billing at [OpenAI](https://platform.openai.com/account). 3) Type a question and click \"Run\".\n",
236
+ " \n",
237
+ " \"\"\"),\n",
238
+ " pn.Row(file_input,openaikey),\n",
239
+ " output,\n",
240
+ " widgets\n",
241
+ "\n",
242
+ ").servable()"
243
+ ]
244
+ }
245
+ ],
246
+ "metadata": {
247
+ "kernelspec": {
248
+ "display_name": "Python 3 (ipykernel)",
249
+ "language": "python",
250
+ "name": "python3"
251
+ },
252
+ "language_info": {
253
+ "codemirror_mode": {
254
+ "name": "ipython",
255
+ "version": 3
256
+ },
257
+ "file_extension": ".py",
258
+ "mimetype": "text/x-python",
259
+ "name": "python",
260
+ "nbconvert_exporter": "python",
261
+ "pygments_lexer": "ipython3",
262
+ "version": "3.10.10"
263
+ },
264
+ "colab": {
265
+ "provenance": []
266
+ }
267
+ },
268
+ "nbformat": 4,
269
+ "nbformat_minor": 5
270
+ }