{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "683953b3",
   "metadata": {},
   "source": [
    "# Momento Vector Index (MVI)\n",
    "\n",
    ">[MVI](https://gomomento.com): the most productive, easiest to use, serverless vector index for your data. To get started with MVI, simply sign up for an account. There's no need to handle infrastructure, manage servers, or be concerned about scaling. MVI is a service that scales automatically to meet your needs.\n",
    "\n",
    "To sign up and access MVI, visit the [Momento Console](https://console.gomomento.com)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82581e78",
   "metadata": {},
   "source": [
    "# Setup"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3120d063",
   "metadata": {},
   "source": [
    "## Install prerequisites"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d7e5fd5",
   "metadata": {},
   "source": [
    "You will need:\n",
    "- the [`momento`](https://pypi.org/project/momento/) package for interacting with MVI, and\n",
    "- the openai package for interacting with the OpenAI API.\n",
    "- the tiktoken package for tokenizing text."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a62cff8a-bcf7-4e33-bbbc-76999c2e3e20",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "!pip install momento openai tiktoken\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8317b9df",
   "metadata": {},
   "source": [
    "## Enter API keys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4b96eed5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import getpass\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ce4b6f7",
   "metadata": {},
   "source": [
    "### Momento: for indexing data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78b8b2ee",
   "metadata": {},
   "source": [
    "Visit the [Momento Console](https://console.gomomento.com) to get your API key."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "211407a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.environ[\"MOMENTO_API_KEY\"] = getpass.getpass(\"Momento API Key:\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08148c5f",
   "metadata": {},
   "source": [
    "### OpenAI: for text embeddings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8b6ed9cd-81b9-46e5-9c20-5aafca2844d0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "347932a6",
   "metadata": {},
   "source": [
    "# Load your data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cfa2538",
   "metadata": {},
   "source": [
    "Here we use the example dataset from Langchain, the state of the union address.\n",
    "\n",
    "First we load relevant modules:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "aac9563e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.embeddings.openai import OpenAIEmbeddings\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain.vectorstores import MomentoVectorIndex\n",
    "from langchain.document_loaders import TextLoader\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f75e1221",
   "metadata": {},
   "source": [
    "Then we load the data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "a3c3999a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loader = TextLoader(\"../../modules/state_of_the_union.txt\")\n",
    "documents = loader.load()\n",
    "len(documents)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31a90e56",
   "metadata": {},
   "source": [
    "Note the data is one large file, hence there is only one document:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1926aaae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "38539"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(documents[0].page_content)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ff35d84",
   "metadata": {},
   "source": [
    "Because this is one large text file, we split it into chunks for question answering. That way, user questions will be answered from the most relevant chunk."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "1de69459",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
    "docs = text_splitter.split_documents(documents)\n",
    "len(docs)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb7854c1",
   "metadata": {},
   "source": [
    "# Index your data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42059ec1",
   "metadata": {},
   "source": [
    "Indexing your data is as simple as instantiating the `MomentoVectorIndex` object. Here we use the `from_documents` helper to both instantiate and index the data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "dcf88bdf",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "vector_db = MomentoVectorIndex.from_documents(\n",
    "    docs,\n",
    "    OpenAIEmbeddings(),\n",
    "    index_name=\"sotu\"\n",
    ")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "225cd0e2",
   "metadata": {},
   "source": [
    "This connects to the Momento Vector Index service using your API key and indexes the data. If the index did not exist before, this process creates it for you. The data is now searchable."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffb2c44e",
   "metadata": {},
   "source": [
    "# Query your data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e705a976",
   "metadata": {},
   "source": [
    "## Ask a question directly against the index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "173185cd",
   "metadata": {},
   "source": [
    "The most direct way to query the data is to search against the index. We can do that as follows using the `VectorStore` API:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "a8c513ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "query = \"What did the president say about Ketanji Brown Jackson\"\n",
    "docs = vector_db.similarity_search(query)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fc516993",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \\n\\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \\n\\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \\n\\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "docs[0].page_content\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f03b7cf6",
   "metadata": {},
   "source": [
    "While this does contain relevant information about Ketanji Brown Jackson, we don't have a concise, human-readable answer. We'll tackle that in the next section."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8979022",
   "metadata": {},
   "source": [
    "## Use an LLM to generate fluent answers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5d51797",
   "metadata": {},
   "source": [
    "With the data indexed in MVI, we can integrate with any chain that leverages vector similarity search. Here we use the `RetrievalQA` chain to demonstrate how to answer questions from the indexed data."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f5a4386",
   "metadata": {},
   "source": [
    "First we load the relevant modules:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "7fa71a86",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.chains import RetrievalQA\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "026cffe3",
   "metadata": {},
   "source": [
    "Then we instantiate the retrieval QA chain:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "3c0a7b2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0)\n",
    "qa_chain = RetrievalQA.from_chain_type(llm, retriever=vector_db.as_retriever())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "19c1a3c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'query': 'What did the president say about Ketanji Brown Jackson?',\n",
       " 'result': \"The President said that he nominated Circuit Court of Appeals Judge Ketanji Brown Jackson to serve on the United States Supreme Court. He described her as one of the nation's top legal minds and mentioned that she has received broad support from various groups, including the Fraternal Order of Police and former judges appointed by Democrats and Republicans.\"}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qa_chain({\"query\": \"What did the president say about Ketanji Brown Jackson?\"})\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d120efcf",
   "metadata": {},
   "source": [
    "# Next Steps"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96a0cc66",
   "metadata": {},
   "source": [
    "That's it! You've now indexed your data and can query it using the Momento Vector Index. You can use the same index to query your data from any chain that supports vector similarity search.\n",
    "\n",
    "With Momento you can not only index your vector data, but also cache your API calls and store your chat message history. Check out the other Momento langchain integrations to learn more.\n",
    "\n",
    "To learn more about the Momento Vector Index, visit the [Momento Documentation](https://docs.gomomento.com).\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f18e7b7",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
