{ "cells": [ { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "import datetime\n", "import gradio as gr\n", "from dotenv import load_dotenv\n", "from langchain.vectorstores import Chroma\n", "from langchain.embeddings.openai import OpenAIEmbeddings\n", "from langchain.chat_models import ChatOpenAI\n", "from langchain.prompts import PromptTemplate\n", "from langchain.chains import RetrievalQA\n", "from langchain.chains import ConversationalRetrievalChain\n", "from langchain.memory import ConversationBufferMemory\n", "\n", "\n", "import warnings\n", "warnings.filterwarnings('ignore')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# from langchain.memory import MemoryViewMemory\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gpt-3.5-turbo-0301\n" ] } ], "source": [ "import datetime\n", "current_date = datetime.datetime.now().date()\n", "if current_date < datetime.date(2023, 9, 2):\n", " llm_name = \"gpt-3.5-turbo-0301\"\n", "else:\n", " llm_name = \"gpt-3.5-turbo\"\n", "print(llm_name)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def chatWithNCAIR(question, history):\n", " load_dotenv()\n", "\n", " persist_directory = 'docs/chroma/'\n", " embedding = OpenAIEmbeddings()\n", " vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding)\n", " llm = ChatOpenAI(model_name=llm_name, temperature=0)\n", "\n", " template = \"\"\"Use the following pieces of context to answer the question at the end. \n", " If you don't know the answer, just say that you don't know, don't try to make up an answer. \n", " Use three sentences maximum. Keep the answer as concise as possible. \n", " Always say \"thank you for choosing NCAIR BOT!\" at the end of the answer. \n", " {context}\n", " Question: {question}\n", " Helpful Answer:\"\"\"\n", " QA_CHAIN_PROMPT = PromptTemplate(input_variables=[\"context\", \"question\"],template=template,)\n", "\n", " # Run chain\n", " from langchain.chains import RetrievalQA\n", " # question = \"Will interns go through the fabLab during the onboarding?\"\n", " qa_chain = RetrievalQA.from_chain_type(llm,\n", " retriever=vectordb.as_retriever(),\n", " return_source_documents=True,\n", " chain_type_kwargs={\"prompt\": QA_CHAIN_PROMPT})\n", "\n", " memory = ConversationBufferMemory(\n", " memory_key=\"chat_history\",\n", " return_messages=True\n", " )\n", " retriever=vectordb.as_retriever()\n", " qa = ConversationalRetrievalChain.from_llm(\n", " llm,\n", " retriever=retriever,\n", " memory=memory\n", " )\n", "\n", "\n", " result = qa({\"question\": question})\n", " return result[\"answer\"]\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Yes, as an intern in NCAIR, you will first undergo the onboarding session in Fablab, PCB, shopbot, 3D printing, solid work, and then go through compulsory NADIT programs.'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chatWithNCAIR(\"Will interns go through the fabLab during the onboarding?\",\"\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# chatWithNCAIR(\"Is it compulsory?\",\"\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# ! pip install --upgrade gradio" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7860\n", "Running on public URL: https://0e3e0326bf1f8474c9.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "