CoolFace
Apppublic

Nehaa/Sematic_Scribe

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
LangChain_QA_Panel_App.ipynb267 linesDownload Raw Back to root
1{2 "cells": [3  {4   "attachments": {},5   "cell_type": "markdown",6   "id": "04815d1b-44ee-4bd3-878e-fa0c3bf9fa7f",7   "metadata": {8    "tags": []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   },23   "outputs": [],24   "source": [25    "#!pip install langchain openai chromadb tiktoken pypdf panel\n"26   ]27  },28  {29   "cell_type": "code",30   "execution_count": null,31   "id": "9a464409-d064-4766-a9cb-5119f6c4b8f5",32   "metadata": {33    "tags": []34   },35   "outputs": [],36   "source": [37    "import os \n",38    "from langchain.chains import RetrievalQA\n",39    "from langchain_community.llms import OpenAI\n",40    "from langchain_community.document_loaders import TextLoader\n",41    "from langchain_community.document_loaders import PyPDFLoader\n",42    "from langchain.indexes import VectorstoreIndexCreator\n",43    "from langchain.text_splitter import CharacterTextSplitter\n",44    "from langchain_community.embeddings import OpenAIEmbeddings\n",45    "from langchain_community.vectorstores import Chroma\n",46    "import panel as pn\n",47    "import tempfile\n"48   ]49  },50  {51   "cell_type": "code",52   "execution_count": null,53   "id": "b2d07ea5-9ff2-4c96-a8dc-92895d870b73",54   "metadata": {55    "tags": []56   },57   "outputs": [],58   "source": [59    "pn.extension('texteditor', template=\"bootstrap\", sizing_mode='stretch_width')\n",60    "pn.state.template.param.update(\n",61    "    main_max_width=\"690px\",\n",62    "    header_background=\"#F08080\",\n",63    ")"64   ]65  },66  {67   "cell_type": "code",68   "execution_count": null,69   "id": "763db4d0-3436-41d3-8b0f-e66ce16468cd",70   "metadata": {71    "tags": []72   },73   "outputs": [],74   "source": [75    "file_input = pn.widgets.FileInput(width=300)\n",76    "\n",77    "openaikey = pn.widgets.PasswordInput(\n",78    "    value=\"\", placeholder=\"Enter your OpenAI API Key here...\", width=300\n",79    ")\n",80    "prompt = pn.widgets.TextEditor(\n",81    "    value=\"\", placeholder=\"Enter your questions here...\", height=160, toolbar=False\n",82    ")\n",83    "run_button = pn.widgets.Button(name=\"Run!\")\n",84    "\n",85    "select_k = pn.widgets.IntSlider(\n",86    "    name=\"Number of relevant chunks\", start=1, end=5, step=1, value=2\n",87    ")\n",88    "select_chain_type = pn.widgets.RadioButtonGroup(\n",89    "    name='Chain type', \n",90    "    options=['stuff', 'map_reduce', \"refine\", \"map_rerank\"]\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\", margin=10\n",99    "    ), width=600\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    "    # create the vectorestore to use as the index\n",122    "    db = Chroma.from_documents(texts, embeddings)\n",123    "    # expose this index in a retriever interface\n",124    "    retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"k\": k})\n",125    "    # create a chain to answer questions \n",126    "    qa = RetrievalQA.from_chain_type(\n",127    "        llm=OpenAI(), chain_type=chain_type, retriever=retriever, return_source_documents=True)\n",128    "    result = qa({\"query\": query})\n",129    "    print(result['result'])\n",130    "    return result"131   ]132  },133  {134   "cell_type": "code",135   "execution_count": null,136   "id": "58ac9945",137   "metadata": {},138   "outputs": [],139   "source": [140    "#os.environ[\"OPENAI_API_KEY\"]=\"\""141   ]142  },143  {144   "cell_type": "code",145   "execution_count": null,146   "id": "2722f43b-daf6-4d17-a842-41203ae9b140",147   "metadata": {148    "tags": []149   },150   "outputs": [],151   "source": [152    "# result = qa(\"example.pdf\", \"what is the total number of AI publications?\")"153   ]154  },155  {156   "cell_type": "code",157   "execution_count": null,158   "id": "60e1b3d3-c0d2-4260-ae0c-26b03f1b8824",159   "metadata": {},160   "outputs": [],161   "source": [162    "convos = []  # store all panel objects in a list\n",163    "\n",164    "def qa_result(_):\n",165    "    os.environ[\"OPENAI_API_KEY\"] = openaikey.value\n",166    "    \n",167    "    # save pdf file to a temp file \n",168    "    if file_input.value is not None:\n",169    "        file_input.save(\"/.cache/temp.pdf\")\n",170    "    \n",171    "        prompt_text = prompt.value\n",172    "        if prompt_text:\n",173    "            result = qa(file=\"/.cache/temp.pdf\", query=prompt_text, chain_type=select_chain_type.value, k=select_k.value)\n",174    "            convos.extend([\n",175    "                pn.Row(\n",176    "                    pn.panel(\"\\U0001F60A\", width=10),\n",177    "                    prompt_text,\n",178    "                    width=600\n",179    "                ),\n",180    "                pn.Row(\n",181    "                    pn.panel(\"\\U0001F916\", width=10),\n",182    "                    pn.Column(\n",183    "                        result[\"result\"],\n",184    "                        \"Relevant source text:\",\n",185    "                        pn.pane.Markdown('\\n--------------------------------------------------------------------\\n'.join(doc.page_content for doc in result[\"source_documents\"]))\n",186    "                    )\n",187    "                )\n",188    "            ])\n",189    "            #return convos\n",190    "    return pn.Column(*convos, margin=15, width=575, min_height=400)\n"191   ]192  },193  {194   "cell_type": "code",195   "execution_count": null,196   "id": "c3a70857-0b98-4f62-a9c0-b62ca42b474c",197   "metadata": {198    "tags": []199   },200   "outputs": [],201   "source": [202    "qa_interactive = pn.panel(\n",203    "    pn.bind(qa_result, run_button),\n",204    "    loading_indicator=True,\n",205    ")"206   ]207  },208  {209   "cell_type": "code",210   "execution_count": null,211   "id": "228e2b42-b1ed-43af-b923-031a70241ab0",212   "metadata": {213    "tags": []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   },227   "outputs": [],228   "source": [229    "# layout\n",230    "pn.Column(\n",231    "    pn.pane.Markdown(\"\"\"\n",232    "    ## \\U0001F60A! Question Answering with your PDF file\n",233    "    \n",234    "    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",235    "    \n",236    "    \"\"\"),\n",237    "    pn.Row(file_input,openaikey),\n",238    "    output,\n",239    "    widgets\n",240    "\n",241    ").servable()"242   ]243  }244 ],245 "metadata": {246  "kernelspec": {247   "display_name": "Python 3 (ipykernel)",248   "language": "python",249   "name": "python3"250  },251  "language_info": {252   "codemirror_mode": {253    "name": "ipython",254    "version": 3255   },256   "file_extension": ".py",257   "mimetype": "text/x-python",258   "name": "python",259   "nbconvert_exporter": "python",260   "pygments_lexer": "ipython3",261   "version": "3.10.10"262  }263 },264 "nbformat": 4,265 "nbformat_minor": 5266}267