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