"/Users/danielgriffiths/Coding_files/Aurelio_local/semantic-router/.venv/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.route import Route\n",
"\n",
"\n",
"politics = Route(\n",
"politics = Route(\n",
" name=\"politics\",\n",
" name=\"politics\",\n",
...
@@ -81,7 +90,7 @@
...
@@ -81,7 +90,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
...
@@ -119,7 +128,7 @@
...
@@ -119,7 +128,7 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
"source": [
"source": [
...
@@ -145,9 +154,17 @@
...
@@ -145,9 +154,17 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m2024-01-03 11:00:59 INFO semantic_router.utils.logger Creating embeddings for all routes...\u001b[0m\n"
The Hybrid Layer in the Semantic Router library can improve making performance particularly for niche use-cases that contain specific terminology, such as finance or medical. It helps us provide more importance to making based on the keywords contained in our utterances and user queries.
The Hybrid Layer in the Semantic Router library can improve making performance particularly for niche use-cases that contain specific terminology, such as finance or medical. It helps us provide more importance to making based on the keywords contained in our utterances and user queries.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Getting Started
## Getting Started
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We start by installing the library:
We start by installing the library:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
!pipinstall-qUsemantic-router==0.0.11
#!pip install -qU semantic-router==0.0.11
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We start by defining a dictionary mapping s to example phrases that should trigger those s.
We start by defining a dictionary mapping s to example phrases that should trigger those s.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
fromsemantic_router.schemaimportRoute
fromsemantic_router.routeimportRoute
politics=Route(
politics=Route(
name="politics",
name="politics",
utterances=[
utterances=[
"isn't politics the best thing ever",
"isn't politics the best thing ever",
"why don't you tell me about your political opinions",
"why don't you tell me about your political opinions",
"don't you just love the president",
"don't you just love the president",
"don't you just hate the president",
"don't you just hate the president",
"they're going to destroy this country!",
"they're going to destroy this country!",
"they will save the country!",
"they will save the country!",
],
],
)
)
```
```
%% Output
/Users/danielgriffiths/Coding_files/Aurelio_local/semantic-router/.venv/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
from .autonotebook import tqdm as notebook_tqdm
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Let's define another for good measure:
Let's define another for good measure:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
```
```
%%Celltype:codeid:tags:
%%Celltype:codeid:tags:
``` python
``` python
chitchat = Route(
chitchat = Route(
name="chitchat",
name="chitchat",
utterances=[
utterances=[
"how's the weather today?",
"how's the weather today?",
"how are things going?",
"how are things going?",
"lovely weather today",
"lovely weather today",
"the weather is horrendous",
"the weather is horrendous",
"let's go to the chippy",
"let's go to the chippy",
],
],
)
)
chitchat = Route(
chitchat = Route(
name="chitchat",
name="chitchat",
utterances=[
utterances=[
"how's the weather today?",
"how's the weather today?",
"how are things going?",
"how are things going?",
"lovely weather today",
"lovely weather today",
"the weather is horrendous",
"the weather is horrendous",
"let's go to the chippy",
"let's go to the chippy",
],
],
)
)
routes = [politics, chitchat]
routes = [politics, chitchat]
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Now we initialize our embedding model:
Now we initialize our embedding model:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
import os
import os
from semantic_router.encoders import CohereEncoder, BM25Encoder, TfidfEncoder
from semantic_router.encoders import CohereEncoder, BM25Encoder, TfidfEncoder
from getpass import getpass
from getpass import getpass
os.environ["COHERE_API_KEY"] = os.environ["COHERE_API_KEY"] or getpass(
os.environ["COHERE_API_KEY"] = os.environ["COHERE_API_KEY"] or getpass(
"Enter Cohere API Key: "
"Enter Cohere API Key: "
)
)
dense_encoder = CohereEncoder()
dense_encoder = CohereEncoder()
# sparse_encoder = BM25Encoder()
# sparse_encoder = BM25Encoder()
sparse_encoder = TfidfEncoder()
sparse_encoder = TfidfEncoder()
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
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`.
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`.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
from semantic_router.hybrid_layer import HybridRouteLayer
from semantic_router.hybrid_layer import HybridRouteLayer