Here we will show how to save routers to YAML or JSON files, and how to load a route layer from file.
We start by installing the library:
!pip install -qU semantic-router
First let's create a list of 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!",
],
)
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]
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
We define a route layer using these routes and using the Cohere encoder.
import os
from getpass import getpass
from semantic_router import RouteLayer
from semantic_router.encoders import CohereEncoder
# dashboard.cohere.ai
os.environ["COHERE_API_KEY"] = os.getenv("COHERE_API_KEY") or getpass(
"Enter Cohere API Key: "
)
encoder = CohereEncoder()
rl = RouteLayer(encoder=encoder, routes=routes)
2024-05-07 15:03:35 INFO semantic_router.utils.logger local
rl("isn't politics the best thing ever")
RouteChoice(name='politics', function_call=None, similarity_score=None)
rl("how's the weather today?")
RouteChoice(name='chitchat', function_call=None, similarity_score=None)
To save our route layer we call the to_json
method:
rl.to_json("layer.json")
2024-05-07 15:03:37 INFO semantic_router.utils.logger Saving route config to layer.json
We can view the router file we just saved to see what information is stored.
import json
with open("layer.json", "r") as f:
layer_json = json.load(f)
print(layer_json)
{'encoder_type': 'cohere', 'encoder_name': 'embed-english-v3.0', 'routes': [{'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 presidentdon't you just hate the president", "they're going to destroy this country!", 'they will save the country!'], 'description': None, 'function_schemas': None, 'llm': None, 'score_threshold': 0.3}, {'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"], 'description': None, 'function_schemas': None, 'llm': None, 'score_threshold': 0.3}]}
It tells us our encoder type, encoder name, and routes. This is everything we need to initialize a new router. To do so, we use the from_json
method.
rl = RouteLayer.from_json("layer.json")
2024-05-07 15:03:37 INFO semantic_router.utils.logger Loading route config from layer.json 2024-05-07 15:03:37 INFO semantic_router.utils.logger local
We can confirm that our layer has been initialized with the expected attributes by viewing the RouteLayer
object:
print(
f"""{rl.encoder.type=}
{rl.encoder.name=}
{rl.routes=}"""
)
rl.encoder.type='cohere' rl.encoder.name='embed-english-v3.0' rl.routes=[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 presidentdon't you just hate the president", "they're going to destroy this country!", 'they will save the country!'], description=None, function_schemas=None, llm=None, score_threshold=0.3), 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"], description=None, function_schemas=None, llm=None, score_threshold=0.3)]
rl("isn't politics the best thing ever")
RouteChoice(name='politics', function_call=None, similarity_score=None)
rl("how's the weather today?")
RouteChoice(name='chitchat', function_call=None, similarity_score=None)