Skip to content
Snippets Groups Projects
Commit e8f00a38 authored by jamescalam's avatar jamescalam
Browse files

fix: update old hybrid example

parent e365910a
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Semantic Router: Hybrid Layer # Semantic Router: Hybrid Layer
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
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 `HybridRouter` 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
#!pip install -qU semantic-router==0.0.11 #!pip install -qU semantic-router==0.1.0.dev2
``` ```
%% 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
from semantic_router.route import Route from semantic_router.route import Route
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 %% Output
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 /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
from .autonotebook import tqdm as notebook_tqdm 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
```
%% Cell type:code id: tags:
``` python
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( 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 OpenAIEncoder, TfidfEncoder
from getpass import getpass from getpass import getpass
os.environ["COHERE_API_KEY"] = os.environ["COHERE_API_KEY"] or getpass( os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") or getpass(
"Enter Cohere API Key: " "Enter OpenAI API Key: "
) )
dense_encoder = CohereEncoder() dense_encoder = OpenAIEncoder()
# 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.routers import HybridRouter
dl = HybridRouteLayer( router = HybridRouter(
encoder=dense_encoder, sparse_encoder=sparse_encoder, routes=routes encoder=dense_encoder,
sparse_encoder=sparse_encoder,
routes=routes
) )
``` ```
%% Output %% Output
2024-05-07 21:15:31 INFO semantic_router.utils.logger Creating embeddings for all routes... 2024-11-24 00:37:14 INFO semantic_router.utils.logger Downloading and initializing default sBM25 model parameters.
2024-11-24 00:37:22 INFO semantic_router.utils.logger Encoding route politics
2024-11-24 00:37:23 INFO semantic_router.utils.logger Encoding route chitchat
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
dl("don't you love politics?") router("don't you love politics?")
``` ```
%% Output %% Output
'politics' RouteChoice(name='politics', function_call=None, similarity_score=1.1909239963848142)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
dl("how's the weather today?") router("how's the weather today?")
``` ```
%% Output %% Output
'chitchat' RouteChoice(name='chitchat', function_call=None, similarity_score=2.0)
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
--- ---
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment