|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "# Semantic Router: Hybrid Layer\n" |
| 7 | + "# Hybrid Router\n" |
8 | 8 | ]
|
9 | 9 | },
|
10 | 10 | {
|
|
107 | 107 | "cell_type": "markdown",
|
108 | 108 | "metadata": {},
|
109 | 109 | "source": [
|
110 |
| - "Now we initialize our embedding model:\n" |
| 110 | + "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", |
| 111 | + "\n", |
| 112 | + "Semantic Router supports other _local_ sparse encoders like `TfidfEncoder` or `BM25Encoder`. Compared to these, the `AurelioSparseEncoder`:\n", |
| 113 | + "\n", |
| 114 | + "1. Doesn't require local fitting (training) on your dataset\n", |
| 115 | + "2. Handles out-of-vocabulary words better\n", |
| 116 | + "3. Works better with asymmetric retrieval (different encoding for queries vs. documents)\n", |
| 117 | + "\n", |
| 118 | + "We initialize both like so:" |
111 | 119 | ]
|
112 | 120 | },
|
113 | 121 | {
|
|
117 | 125 | "outputs": [],
|
118 | 126 | "source": [
|
119 | 127 | "import os\n",
|
120 |
| - "from semantic_router.encoders import OpenAIEncoder, TfidfEncoder\n", |
| 128 | + "from semantic_router.encoders import OpenAIEncoder, AurelioSparseEncoder\n", |
121 | 129 | "from getpass import getpass\n",
|
122 | 130 | "\n",
|
| 131 | + "# get OpenAI API key from https://platform.openai.com/\n", |
123 | 132 | "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\") or getpass(\n",
|
124 | 133 | " \"Enter OpenAI API Key: \"\n",
|
125 | 134 | ")\n",
|
126 | 135 | "\n",
|
127 |
| - "dense_encoder = OpenAIEncoder()\n", |
128 |
| - "# sparse_encoder = BM25Encoder()\n", |
129 |
| - "sparse_encoder = TfidfEncoder()" |
| 136 | + "dense_encoder = OpenAIEncoder(name=\"text-embedding-3-small\", score_threshold=0.3)\n", |
| 137 | + "\n", |
| 138 | + "# get Aurelio API key from https://platform.aurelio.ai\n", |
| 139 | + "# use \"SRHYBRIDROUTER\" for free credits\n", |
| 140 | + "os.environ[\"AURELIO_API_KEY\"] = os.getenv(\"AURELIO_API_KEY\") or getpass(\n", |
| 141 | + " \"Enter Aurelio API Key: \"\n", |
| 142 | + ")\n", |
| 143 | + "\n", |
| 144 | + "# Using Aurelio's BM25 sparse encoder\n", |
| 145 | + "sparse_encoder = AurelioSparseEncoder(name=\"bm25\")" |
130 | 146 | ]
|
131 | 147 | },
|
132 | 148 | {
|
|
155 | 171 | "from semantic_router.routers import HybridRouter\n",
|
156 | 172 | "\n",
|
157 | 173 | "router = HybridRouter(\n",
|
158 |
| - " encoder=dense_encoder, sparse_encoder=sparse_encoder, routes=routes\n", |
| 174 | + " encoder=dense_encoder, \n", |
| 175 | + " sparse_encoder=sparse_encoder, \n", |
| 176 | + " routes=routes,\n", |
| 177 | + " alpha=0.5 # Balance between dense (0) and sparse (1) embeddings\n", |
159 | 178 | ")"
|
160 | 179 | ]
|
161 | 180 | },
|
|
0 commit comments