{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Hybrid Router\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `HybridRouter` in the Semantic Router library can improve making performance particularly for niche use-cases that contain specific terminology, such as finance or medical.\n", "\n", "It helps us provide more importance to making based on the keywords contained in our utterances and user queries." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting Started\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start by installing the library:\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#!pip install -qU semantic-router==0.1.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start by defining a dictionary mapping s to example phrases that should trigger those s.\n" ] }, { "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.route 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:\n" ] }, { "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 use a dense encoder from [OpenAI](https://platform.openai.com/) and a sparse encoder from [Aurelio](https://platform.aurelio.ai/). The `AurelioSparseEncoder` we use here provides a remote sparse encoder that can significantly improve routing accuracy when combined with dense embeddings.\n", "\n", "Semantic Router supports other _local_ sparse encoders like `TfidfEncoder` or `BM25Encoder`. Compared to these, the `AurelioSparseEncoder`:\n", "\n", "1. Doesn't require local fitting (training) on your dataset\n", "2. Handles out-of-vocabulary words better\n", "3. Works better with asymmetric retrieval (different encoding for queries vs. documents)\n", "\n", "We initialize both like so:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import os\n", "from semantic_router.encoders import OpenAIEncoder, AurelioSparseEncoder\n", "from getpass import getpass\n", "\n", "# get OpenAI API key from https://platform.openai.com/\n", "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\") or getpass(\n", " \"Enter OpenAI API Key: \"\n", ")\n", "\n", "dense_encoder = OpenAIEncoder(name=\"text-embedding-3-small\", score_threshold=0.3)\n", "\n", "# get Aurelio API key from https://platform.aurelio.ai\n", "# use \"SRHYBRIDROUTER\" for free credits\n", "os.environ[\"AURELIO_API_KEY\"] = os.getenv(\"AURELIO_API_KEY\") or getpass(\n", " \"Enter Aurelio API Key: \"\n", ")\n", "\n", "# Using Aurelio's BM25 sparse encoder\n", "sparse_encoder = AurelioSparseEncoder(name=\"bm25\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we define the `RouteLayer`. When called, the route layer will consume text (a query) and output the category (`Route`) it belongs to — to initialize a `RouteLayer` we need our `encoder` model and a list of `routes`.\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m2024-11-24 00:37:14 INFO semantic_router.utils.logger Downloading and initializing default sBM25 model parameters.\u001b[0m\n", "\u001b[32m2024-11-24 00:37:22 INFO semantic_router.utils.logger Encoding route politics\u001b[0m\n", "\u001b[32m2024-11-24 00:37:23 INFO semantic_router.utils.logger Encoding route chitchat\u001b[0m\n" ] } ], "source": [ "from semantic_router.routers import HybridRouter\n", "\n", "router = HybridRouter(\n", " encoder=dense_encoder,\n", " sparse_encoder=sparse_encoder,\n", " routes=routes,\n", " alpha=0.5, # Balance between dense (0) and sparse (1) embeddings\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "RouteChoice(name='politics', function_call=None, similarity_score=1.1909239963848142)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "router(\"don't you love politics?\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "RouteChoice(name='chitchat', function_call=None, similarity_score=2.0)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "router(\"how's the weather today?\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n" ] } ], "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 }