{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#!pip install langchain openai chromadb tiktoken pypdf panel" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os \n", "from langchain.chains import RetrievalQA\n", "from langchain.llms import OpenAI\n", "from langchain.document_loaders import TextLoader\n", "from langchain.document_loaders import PyPDFLoader\n", "from langchain.indexes import VectorstoreIndexCreator\n", "from langchain.text_splitter import CharacterTextSplitter\n", "from langchain.embeddings import OpenAIEmbeddings\n", "from langchain.vectorstores import Chroma\n", "import panel as pn\n", "import tempfile" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pn.extension('texteditor', template=\"bootstrap\", sizing_mode='stretch_width')\n", "pn.state.template.param.update(\n", " main_max_width=\"690px\",\n", " header_background=\"#F08080\",\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "file_input = pn.widgets.FileInput(width=300)\n", "\n", "openaikey = pn.widgets.PasswordInput(\n", " value=\"\", placeholder=\"Enter your OpenAI API Key here...\", width=300\n", ")\n", "prompt = pn.widgets.TextEditor(\n", " value=\"\", placeholder=\"Enter your questions here...\", height=160, toolbar=False\n", ")\n", "run_button = pn.widgets.Button(name=\"Run!\")\n", "\n", "select_k = pn.widgets.IntSlider(\n", " name=\"Number of relevant chunks\", start=1, end=5, step=1, value=2\n", ")\n", "select_chain_type = pn.widgets.RadioButtonGroup(\n", " name='Chain type', \n", " options=['stuff', 'map_reduce', \"refine\", \"map_rerank\"]\n", ")\n", "\n", "widgets = pn.Row(\n", " pn.Column(prompt, run_button, margin=5),\n", " pn.Card(\n", " \"Chain type:\",\n", " pn.Column(select_chain_type, select_k),\n", " title=\"Advanced settings\", margin=10\n", " ), width=600\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def qa(file, query, chain_type, k):\n", " # load document\n", " loader = PyPDFLoader(file)\n", " documents = loader.load()\n", " # split the documents into chunks\n", " text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n", " texts = text_splitter.split_documents(documents)\n", " # select which embeddings we want to use\n", " embeddings = OpenAIEmbeddings()\n", " # create the vectorestore to use as the index\n", " db = Chroma.from_documents(texts, embeddings)\n", " # expose this index in a retriever interface\n", " retriever = db.as_retriever(search_type=\"similarity\", search_kwargs={\"k\": k})\n", " # create a chain to answer questions \n", " qa = RetrievalQA.from_chain_type(\n", " llm=OpenAI(), chain_type=chain_type, retriever=retriever, return_source_documents=True)\n", " result = qa({\"query\": query})\n", " print(result['result'])\n", " return result" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# result = qa(\"example.pdf\", \"what is the total number of AI publications?\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "convos = [] # store all panel objects in a list\n", "\n", "def qa_result(_):\n", " os.environ[\"OPENAI_API_KEY\"] = openaikey.value\n", " \n", " # save pdf file to a temp file \n", " if file_input.value is not None:\n", " file_input.save(\"/.cache/temp.pdf\")\n", " \n", " prompt_text = prompt.value\n", " if prompt_text:\n", " result = qa(file=\"/.cache/temp.pdf\", query=prompt_text, chain_type=select_chain_type.value, k=select_k.value)\n", " convos.extend([\n", " pn.Row(\n", " pn.panel(\"\\U0001F60A\", width=10),\n", " prompt_text,\n", " width=600\n", " ),\n", " pn.Row(\n", " pn.panel(\"\\U0001F916\", width=10),\n", " pn.Column(\n", " result[\"result\"],\n", " \"Relevant source text:\",\n", " pn.pane.Markdown('\\n--------------------------------------------------------------------\\n'.join(doc.page_content for doc in result[\"source_documents\"]))\n", " )\n", " )\n", " ])\n", " #return convos\n", " return pn.Column(*convos, margin=15, width=575, min_height=400)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "qa_interactive = pn.panel(\n", " pn.bind(qa_result, run_button),\n", " loading_indicator=True,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = pn.WidgetBox('*Output will show up here:*', qa_interactive, width=630, scroll=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# layout\n", "pn.Column(\n", " pn.pane.Markdown(\"\"\"\n", " ## \\U0001F60A! Question Answering with your PDF file\n", " \n", " 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", " \n", " \"\"\"),\n", " pn.Row(file_input,openaikey),\n", " output,\n", " widgets\n", "\n", ").servable()" ] } ], "metadata": { "language_info": { "name": "python" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }