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.
We start by installing the library:
#!pip install -qU semantic-router==0.0.11
We start by defining a dictionary mapping s to example phrases that should trigger those s.
from semantic_router.route import Route
politics = Route(
name="politics",
utterances=[
"isn't politics the best thing ever",
"why don't you tell me about your political opinions",
"don't you just love the president",
"don't you just hate the president",
"they're going to destroy this country!",
"they will save the country!",
],
)
c:\Users\Siraj\Documents\Personal\Work\Aurelio\Virtual Environments\semantic_router_3\Lib\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
Let's define another for good measure:
chitchat = Route(
name="chitchat",
utterances=[
"how's the weather today?",
"how are things going?",
"lovely weather today",
"the weather is horrendous",
"let's go to the chippy",
],
)
chitchat = Route(
name="chitchat",
utterances=[
"how's the weather today?",
"how are things going?",
"lovely weather today",
"the weather is horrendous",
"let's go to the chippy",
],
)
routes = [politics, chitchat]
Now we initialize our embedding model:
import os
from semantic_router.encoders import CohereEncoder, BM25Encoder, TfidfEncoder
from getpass import getpass
os.environ["COHERE_API_KEY"] = os.environ["COHERE_API_KEY"] or getpass(
"Enter Cohere API Key: "
)
dense_encoder = CohereEncoder()
# sparse_encoder = BM25Encoder()
sparse_encoder = TfidfEncoder()
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
.
from semantic_router.hybrid_layer import HybridRouteLayer
dl = HybridRouteLayer(
encoder=dense_encoder, sparse_encoder=sparse_encoder, routes=routes
)
2024-05-07 21:15:31 INFO semantic_router.utils.logger Creating embeddings for all routes...
dl("don't you love politics?")
'politics'
dl("how's the weather today?")
'chitchat'