{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/encoders/aurelio-bm25.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/encoders/aurelio-bm25.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Using PineconeIndex for Hybrid Routes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hybrid indexes combine both sparse and dense encodings to produce more accurate results. The dense encoder allows us to search based on semantic meaning, while the sparse encoder allows us to search based on text matches. Merging both dense and sparse into a single hybrid retrieval step allows us to step up our performance beyond what dense-only or sparse-only could achieve." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting Started" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start by installing semantic-router. Support for the new `AurelioSparseEncoder` parameter was added in `semantic-router==0.1.0`." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "!pip install -qU \"semantic-router[pinecone]==0.1.0\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start by defining a dictionary mapping routes to example phrases that should trigger those routes." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/jamesbriggs/Library/Caches/pypoetry/virtualenvs/semantic-router-C1zr4a78-py3.12/lib/python3.12/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", "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\",\n", " \"don't you just hate the president\",\n", " \"they're going to destroy this country!\",\n", " \"they will save the country!\",\n", " ],\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's define another for good measure:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "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", "routes = [politics, chitchat]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we initialize our embedding models. We are going to use a hybrid index which requires both a dense and sparse encoder. For the sparse encoder we will use the pretrained `bm25` model from the Aurelio Platform and OpenAI's `text-embedding-3-small` for the dense encoder.\n", "\n", "To get an API key for the Aurelio Platform, we head to the [Aurelio Platform](https://platform.aurelio.ai/settings/api-keys)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import os\n", "from getpass import getpass\n", "from semantic_router.encoders.aurelio import AurelioSparseEncoder\n", "\n", "os.environ[\"AURELIO_API_KEY\"] = os.getenv(\"AURELIO_API_KEY\") or getpass(\n", " \"Enter Aurelio API Key: \"\n", ")\n", "\n", "sparse_encoder = AurelioSparseEncoder(name=\"bm25\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sparse encoders return dictionaries containing the the indices and values of the non-zero elements in the sparse matrix." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from semantic_router.encoders import OpenAIEncoder\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\") or getpass(\n", " \"Enter OpenAI API Key: \"\n", ")\n", "\n", "encoder = OpenAIEncoder(name=\"text-embedding-3-small\", score_threshold=0.3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We now have both our sparse and dense encoders. When using both sparse and dense encoders we need to initialize an index that supports hybrid, such as the `HybridLocalIndex` or `PineconeIndex`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-11-27 15:41:32 - pinecone_plugin_interface.logging - INFO - discover_namespace_packages.py:12 - discover_subpackages() - Discovering subpackages in _NamespacePath(['/Users/jamesbriggs/Library/Caches/pypoetry/virtualenvs/semantic-router-C1zr4a78-py3.12/lib/python3.12/site-packages/pinecone_plugins'])\n", "2024-11-27 15:41:32 - pinecone_plugin_interface.logging - INFO - discover_plugins.py:9 - discover_plugins() - Looking for plugins in pinecone_plugins.inference\n", "2024-11-27 15:41:32 - pinecone_plugin_interface.logging - INFO - installation.py:10 - install_plugins() - Installing plugin inference into Pinecone\n" ] } ], "source": [ "from semantic_router.index import PineconeIndex\n", "\n", "os.environ[\"PINECONE_API_KEY\"] = os.getenv(\"PINECONE_API_KEY\") or getpass(\n", " \"Enter Pinecone API Key: \"\n", ")\n", "\n", "index = PineconeIndex(\n", " index_name=\"hybrid-test\",\n", " dimensions=1536,\n", " metric=\"dotproduct\",\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we define the `HybridRouter`. When called, the router will consume text (a query) and output the category (`Route`) it belongs to — to initialize a `HybridRouter` we need an `encoder`, `sparse_encoder` our `routes`, and the hybrid `index` we just define." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from semantic_router.routers import HybridRouter\n", "\n", "router = HybridRouter(\n", " encoder=encoder,\n", " sparse_encoder=sparse_encoder,\n", " routes=routes,\n", " index=index,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see if our local and remote instances are synchronized..." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "router.is_synced()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It seems like our `router` is not synchronized, meaning there are differences between the utterances in our local `HybridRouter` and the remote `PineconeIndex`. We can view the differences by calling `get_utterance_diff()`:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[' chitchat: how are things going?',\n", " \" chitchat: how's the weather today?\",\n", " \" chitchat: let's go to the chippy\",\n", " ' chitchat: lovely weather today',\n", " ' chitchat: the weather is horrendous',\n", " \" politics: don't you just hate the president\",\n", " \" politics: don't you just love the president\",\n", " \" politics: isn't politics the best thing ever\",\n", " ' politics: they will save the country!',\n", " \" politics: they're going to destroy this country!\",\n", " \" politics: why don't you tell me about your political opinions\"]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "router.get_utterance_diff()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From this, we can see that every utterance is preceeded by a `-` meaning it is unique to the local `HybridRouter`. So it seems our `PineconeIndex` is missing all utterances. We can confirm this further by calling `router.index.get_utterances()` to see all utterances in the remote `PineconeIndex`:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[Utterance(route='chitchat', utterance='how are things going?', function_schemas=None, metadata={}, diff_tag=' '),\n", " Utterance(route='chitchat', utterance=\"how's the weather today?\", function_schemas=None, metadata={}, diff_tag=' '),\n", " Utterance(route='chitchat', utterance='the weather is horrendous', function_schemas=None, metadata={}, diff_tag=' '),\n", " Utterance(route='chitchat', utterance='lovely weather today', function_schemas=None, metadata={}, diff_tag=' '),\n", " Utterance(route='chitchat', utterance=\"let's go to the chippy\", function_schemas=None, metadata={}, diff_tag=' '),\n", " Utterance(route='politics', utterance=\"don't you just hate the president\", function_schemas=None, metadata={}, diff_tag=' '),\n", " Utterance(route='politics', utterance=\"don't you just love the president\", function_schemas=None, metadata={}, diff_tag=' '),\n", " Utterance(route='politics', utterance=\"they're going to destroy this country!\", function_schemas=None, metadata={}, diff_tag=' '),\n", " Utterance(route='politics', utterance='they will save the country!', function_schemas=None, metadata={}, diff_tag=' '),\n", " Utterance(route='politics', utterance=\"isn't politics the best thing ever\", function_schemas=None, metadata={}, diff_tag=' '),\n", " Utterance(route='politics', utterance=\"why don't you tell me about your political opinions\", function_schemas=None, metadata={}, diff_tag=' ')]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "router.index.get_utterances()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As expected, we have no utterances in the remote `PineconeIndex`. The reason for this is that when initializing our `HybridRouter` we did not specify an `auto_sync` parameter, so `auto_sync` defaulted to `None`. When `auto_sync=None` no synchronization is performed during initialization. Let's try again with `auto_sync=\"local\"`, meaning take what we have locally and overwrite the remote `PineconeIndex` with these local values." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "router = HybridRouter(\n", " encoder=encoder,\n", " sparse_encoder=sparse_encoder,\n", " routes=routes,\n", " index=index,\n", " auto_sync=\"local\",\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's check our sync state:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "router.is_synced()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[' chitchat: how are things going?',\n", " \" chitchat: how's the weather today?\",\n", " \" chitchat: let's go to the chippy\",\n", " ' chitchat: lovely weather today',\n", " ' chitchat: the weather is horrendous',\n", " \" politics: don't you just hate the president\",\n", " \" politics: don't you just love the president\",\n", " \" politics: isn't politics the best thing ever\",\n", " ' politics: they will save the country!',\n", " \" politics: they're going to destroy this country!\",\n", " \" politics: why don't you tell me about your political opinions\"]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "router.get_utterance_diff()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "... NEED TO FINISH HERE" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-11-27 15:42:03 - httpx - INFO - _client.py:1013 - _send_single_request() - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n" ] }, { "data": { "text/plain": [ "RouteChoice(name=None, function_call=None, similarity_score=None)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "router(\"it's raining cats and dogs today\")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-11-27 15:42:06 - httpx - INFO - _client.py:1013 - _send_single_request() - HTTP Request: POST https://api.openai.com/v1/embeddings \"HTTP/1.1 200 OK\"\n" ] }, { "data": { "text/plain": [ "RouteChoice(name=None, function_call=None, similarity_score=None)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "router(\"I'm interested in learning about llama 2\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case, we return `None` because no matches were identified. We always recommend optimizing your `RouteLayer` for optimal performance, you can see how in [this notebook](https://github.com/aurelio-labs/semantic-router/blob/main/docs/06-threshold-optimization.ipynb)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] } ], "metadata": { "kernelspec": { "display_name": "decision-layer", "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.12.7" } }, "nbformat": 4, "nbformat_minor": 2 }