{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -qU \"semantic-router[pinecone]==0.0.22\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/jamesbriggs/opt/anaconda3/envs/decision-layer/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "from semantic_router import Route\n",
    "\n",
    "# we could use this as a guide for our chatbot to avoid political conversations\n",
    "politics = Route(\n",
    "    name=\"politics\",\n",
    "    utterances=[\n",
    "        \"isn't politics the best thing ever\",\n",
    "        \"why don't you tell me about your political opinions\",\n",
    "        \"don't you just love the president\" \"don't you just hate the president\",\n",
    "        \"they're going to destroy this country!\",\n",
    "        \"they will save the country!\",\n",
    "    ],\n",
    ")\n",
    "\n",
    "# this could be used as an indicator to our chatbot to switch to a more\n",
    "# conversational prompt\n",
    "chitchat = Route(\n",
    "    name=\"chitchat\",\n",
    "    utterances=[\n",
    "        \"how's the weather today?\",\n",
    "        \"how are things going?\",\n",
    "        \"lovely weather today\",\n",
    "        \"the weather is horrendous\",\n",
    "        \"let's go to the chippy\",\n",
    "    ],\n",
    ")\n",
    "\n",
    "# we place both of our decisions together into single list\n",
    "routes = [politics, chitchat]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from getpass import getpass\n",
    "from semantic_router.encoders import OpenAIEncoder\n",
    "\n",
    "# get at platform.openai.com\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.environ.get(\"OPENAI_API_KEY\") or getpass(\n",
    "    \"Enter OpenAI API key: \"\n",
    ")\n",
    "encoder = OpenAIEncoder()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from semantic_router.index.pinecone import PineconeIndex\n",
    "\n",
    "# get at app.pinecone.io\n",
    "os.environ[\"PINECONE_API_KEY\"] = os.environ.get(\"PINECONE_API_KEY\") or getpass(\n",
    "    \"Enter Pinecone API key: \"\n",
    ")\n",
    "pc_index = PineconeIndex()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[32m2024-02-14 01:40:22 INFO semantic_router.utils.logger local\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "from semantic_router.layer import RouteLayer\n",
    "\n",
    "rl = RouteLayer(encoder=encoder, routes=routes, index=pc_index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can check our route layer and index information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['politics', 'chitchat']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rl.list_route_names()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(rl.index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also view all of the records for a given route:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'vectors': [{'id': 'politics#1c4e5a74a2cb693bd51dd4fe0d8deb41'}, {'id': 'politics#3f95e1e2c26aa109d7a6b352bd1da93e'}, {'id': 'politics#461468866b9123c5ef111b72040c20d8'}, {'id': 'politics#e9120cb91dcc3ff76e7d2ce0740e009c'}, {'id': 'politics#f798c13e0a6d8a9926fc35a0f10f880b'}], 'pagination': {'next': 'eyJza2lwX3Bhc3QiOiJwb2xpdGljcyNmNzk4YzEzZTBhNmQ4YTk5MjZmYzM1YTBmMTBmODgwYiIsInByZWZpeCI6InBvbGl0aWNzIn0='}, 'namespace': '', 'usage': {'readUnits': 1}}\n",
      "{'vectors': [], 'namespace': '', 'usage': {'readUnits': 1}}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['politics#1c4e5a74a2cb693bd51dd4fe0d8deb41',\n",
       " 'politics#3f95e1e2c26aa109d7a6b352bd1da93e',\n",
       " 'politics#461468866b9123c5ef111b72040c20d8',\n",
       " 'politics#e9120cb91dcc3ff76e7d2ce0740e009c',\n",
       " 'politics#f798c13e0a6d8a9926fc35a0f10f880b']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rl.index._get_route_ids(route_name=\"politics\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And query:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'politics'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rl(\"don't you love politics?\").name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'chitchat'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rl(\"how's the weather today?\").name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "rl(\"I'm interested in learning about llama 2\").name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can delete or update routes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(rl.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'vectors': [{'id': 'chitchat#1e01e2c7f411d4dfcc43fe7a3de2fb67'}, {'id': 'chitchat#34fd10a3b6d1436b49759d9c67068a7e'}, {'id': 'chitchat#74cfdf22939f052ea9be158d77bc6bda'}, {'id': 'chitchat#bc13a8bc005f437d2bdeacbb9e741b8b'}, {'id': 'chitchat#fca33e294c0c4bb08ed95e1e3fd944c0'}], 'namespace': '', 'usage': {'readUnits': 1}}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "rl.delete(route_name=\"chitchat\")\n",
    "time.sleep(1)\n",
    "len(rl.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "rl(\"how's the weather today?\").name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'vectors': [{'id': 'politics#1c4e5a74a2cb693bd51dd4fe0d8deb41'}, {'id': 'politics#3f95e1e2c26aa109d7a6b352bd1da93e'}, {'id': 'politics#461468866b9123c5ef111b72040c20d8'}, {'id': 'politics#e9120cb91dcc3ff76e7d2ce0740e009c'}, {'id': 'politics#f798c13e0a6d8a9926fc35a0f10f880b'}], 'namespace': '', 'usage': {'readUnits': 1}}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[('politics', \"they're going to destroy this country!\"),\n",
       " ('politics',\n",
       "  \"don't you just love the presidentdon't you just hate the president\"),\n",
       " ('politics', \"isn't politics the best thing ever\"),\n",
       " ('politics', \"why don't you tell me about your political opinions\"),\n",
       " ('politics', 'they will save the country!')]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rl.index.get_routes()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'type': 'pinecone', 'dimensions': 1536, 'vectors': 5}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rl.index.describe()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "semantic_router_1",
   "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}