The Semantic Router library can be used as a super fast route making layer on top of LLMs. That means rather than waiting on a slow agent to decide what to do, we can use the magic of semantic vector space to make routes. Cutting route making time down from seconds to milliseconds.
We start by installing the library:
!pip install -qU "semantic-router>=0.1.3"
We start by defining a dictionary mapping routes to example phrases that should trigger those routes.
from semantic_router 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!",
],
)
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",
],
)
routes = [politics, chitchat]
Now we initialize our embedding model:
import os
from getpass import getpass
from semantic_router.encoders import CohereEncoder, OpenAIEncoder
os.environ["COHERE_API_KEY"] = os.getenv("COHERE_API_KEY") or getpass(
"Enter Cohere API Key: "
)
# os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY") or getpass(
# "Enter OpenAI API Key: "
# )
encoder = CohereEncoder()
# encoder = OpenAIEncoder()
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.routers import SemanticRouter
rl = SemanticRouter(encoder=encoder, routes=routes, auto_sync="local")
2025-01-06 12:42:33 - semantic_router.utils.logger - WARNING - base.py:356 - _get_index() - No index provided. Using default LocalIndex. 2025-01-06 12:42:33 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed "HTTP/1.1 200 OK" 2025-01-06 12:42:34 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed "HTTP/1.1 200 OK" 2025-01-06 12:42:34 - semantic_router.utils.logger - WARNING - local.py:148 - _write_config() - No config is written for LocalIndex.
Now we can test it:
rl("don't you love politics?")
2025-01-06 12:42:37 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed "HTTP/1.1 200 OK" 2025-01-06 12:42:39 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed "HTTP/1.1 200 OK" 2025-01-06 12:42:42 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed "HTTP/1.1 200 OK" 2025-01-06 12:42:46 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed "HTTP/1.1 200 OK" 2025-01-06 12:42:48 - httpx - INFO - _client.py:1025 - _send_single_request() - HTTP Request: POST https://api.cohere.com/v1/embed "HTTP/1.1 200 OK"
RouteChoice(name='politics', function_call=None, similarity_score=None)
rl("how's the weather today?")
RouteChoice(name='chitchat', function_call=None, similarity_score=None)
Both are classified accurately, what if we send a query that is unrelated to our existing Route
objects?
rl("I'm interested in learning about llama 2")
RouteChoice(name=None, function_call=None, similarity_score=None)
In this case, we return None
because no matches were identified.
Now, let's demonstrate the filter feature. We can specify a subset of routes to consider when making a classification. This can be useful if we want to restrict the scope of possible routes based on some context.
For example, let's say we only want to consider the "chitchat" route for a particular query:
rl("don't you love politics?", route_filter=["chitchat"])
RouteChoice(name='chitchat', function_call=None, similarity_score=None)
Even though the query might be more related to the "politics" route, it will be classified as "chitchat" because we've restricted the routes to consider.
Similarly, we can restrict it to the "politics" route:
rl("how's the weather today?", route_filter=["politics"])
RouteChoice(name=None, function_call=None, similarity_score=None)
In this case, it will return None because the query doesn't match the "politics" route well enough to pass the threshold.