{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Setup UI" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loading settings from ../../env/ai.json\n" ] } ], "source": [ "import os\n", "import json\n", "\n", "# If the file does not exist it'll default to the manual setting see below\n", "filePathToSettingsFile = '../../env/ai.json'\n", "\n", "# Is there a settings file? \n", "if os.path.exists(filePathToSettingsFile):\n", " # Yes there is so load settings from there\n", " \n", " print(f'Loading settings from {filePathToSettingsFile}')\n", " f = open(filePathToSettingsFile)\n", " settingsJson = json.load(f)\n", " del f\n", "\n", " for key in settingsJson:\n", " os.environ[key] = settingsJson[key]\n", " \n", " del settingsJson\n", "else: \n", " # Set variables manually\n", " \n", " print('Setting variables manually as there is not ai.json settings file')\n", "\n", " # Update the variables below with your own settings\n", " os.environ['REQUESTS_CA_BUNDLE'] = '../../env/ZCert.pem' \n", " os.environ['HUGGING_FACE_API_KEY'] = 'Get here: https://huggingface.co/settings/tokens'\n", " os.environ['OPENAI_API_KEY'] = 'Get here: https://platform.openai.com/account/api-keys'\n", " os.environ[\"SERPAPI_API_KEY\"] = 'serpapi KEY, Get here: https://serpapi.com/manage-api-key' " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "import os\n", "import pinecone\n", "from langchain.chains import RetrievalQA\n", "from langchain.embeddings import OpenAIEmbeddings\n", "from langchain.llms import OpenAI\n", "from langchain.vectorstores import Pinecone\n", "import gradio as gr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Setup Vector Store\n", "There are two vector stores in Pinecone (hence the two API Keys). Each has a separate knowledge base\n", "1. Roman history\n", "2. A list of literature" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using vector store: Roman History\n", "Using index: rag-demo-1-history-rome\n" ] } ], "source": [ "embeddings = OpenAIEmbeddings()\n", "\n", "vector_store = None\n", "\n", "def create_vector_store(indexToUse: str):\n", " \n", " if indexToUse == \"Roman History\":\n", " print(f\"Using vector store: {indexToUse}\")\n", " apiKeyKey = \"PINECONE_API_KEY_2\"\n", " apiEnvKey = \"PINECONE_API_ENV_2\"\n", " testQuestion = \"When was Ceasar born?\"\n", " else:\n", " print(f\"Using vector store: {indexToUse}\")\n", " apiKeyKey = \"PINECONE_API_KEY\"\n", " apiEnvKey = \"PINECONE_API_ENV\"\n", " testQuestion = \"What is Moby Dick?\"\n", " \n", " pinecone.init(api_key=os.environ[apiKeyKey], environment=os.environ[apiEnvKey])\n", "\n", " index_name = pinecone.list_indexes()[0]\n", " print(f\"Using index: {index_name}\")\n", " index = pinecone.Index(index_name)\n", " vector_store = Pinecone(index, embeddings, \"text\") \n", "\n", " # query = testQuestion\n", " # print(f'Test questions: {testQuestion}')\n", " # result = vector_store.similarity_search(query, k=3)\n", " # print(result[0])\n", " \n", " return vector_store\n", "\n", "vector_store = create_vector_store(\"Roman History\")" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "llm = OpenAI(temperature=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conversational agent" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "from langchain.memory import ConversationBufferMemory\n", "from langchain.chains import ConversationalRetrievalChain\n", "\n", "memory = ConversationBufferMemory(memory_key=\"chat_history\", return_messages= True)\n", "chain = ConversationalRetrievalChain.from_llm(llm, retriever= vector_store.as_retriever(), memory= memory)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' \\nCrassus was a Roman general who was killed in battle by the Parthians. He was killed while trying to extend the Roman Empire into the Middle East. Ceasar was avenging Crassus by trying to defeat the Parthians and expand the Roman Empire. He was also trying to avenge the death of his friend and mentor.'" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "query = \"\"\" \n", " SYSTEM:\n", " You are a helpful teacher who is teaching a class of 10 year olds. \n", " Your answers must only come from the context provided to you in the question.\n", " If you don't know the answer then say so. \n", " The answers should be at least 40 words or longer\n", " \n", " QUESTION:\n", " Why was he avenging Crassus, what happened to him that Ceasar needed to avenge him? \n", " \n", "\"\"\"\n", "chain.run({'question': query})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# UI\n", "1. https://medium.com/@gabriel_renno/how-to-build-a-gpt3-5-powered-chatbot-for-your-landing-page-with-langchain-and-gradio-1236ddfb0cf1\n", "2. https://github.com/RajKKapadia/YouTube-Pinecone-Demo" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [], "source": [ "# RajKKapadi's \n", "from langchain.chains import ConversationalRetrievalChain\n", "from langchain.chat_models import ChatOpenAI\n", "from langchain.vectorstores import Pinecone\n", "from langchain.embeddings.openai import OpenAIEmbeddings\n", "from langchain.memory import ConversationBufferMemory\n", "import pinecone\n", "\n", "def create_conversation(query: str, chat_history: list, indexToUse: str) -> tuple: \n", " try:\n", " #vector_store = create_vector_store(\"Roman History\")\n", " print(indexToUse)\n", " vector_store = create_vector_store(\"Literature\")\n", " memory = ConversationBufferMemory(\n", " memory_key='chat_history',\n", " return_messages=False\n", " )\n", " cqa = ConversationalRetrievalChain.from_llm(\n", " llm=ChatOpenAI(temperature=0.0,\n", " openai_api_key=os.environ['OPENAI_API_KEY']),\n", " retriever=vector_store.as_retriever(search_kwargs={\"k\": 5}),\n", " memory=memory,\n", " get_chat_history=lambda h: h,\n", " )\n", " result = cqa({'question': query, 'chat_history': chat_history})\n", " chat_history.append((query, result['answer']))\n", " return '', chat_history, indexToUse\n", " except Exception as e:\n", " chat_history.append((query, e))\n", " return '', chat_history" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [], "source": [ "\n", "\n", "# vector_store_index_to_use = None \n", "# def set_vector_store_index(indexName:str):\n", "# vector_store_index_to_use = indexName\n", " \n", "# with gr.Blocks() as demo:\n", "# indexToUseDD = gr.Dropdown(choices=[\"Roman History\", \"Literature\"])" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7918\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "