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>=0.1.5"
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 presidentdon'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\Joshu\OneDrive\Documents\Aurelio\agents-course\07-pratical-ai\.venv\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.encoders import CohereEncoder
from semantic_router.routers import SemanticRouter
# dashboard.cohere.ai
os.environ["COHERE_API_KEY"] = os.getenv("COHERE_API_KEY") or getpass(
"Enter Cohere API Key: "
)
encoder = CohereEncoder()
rl = SemanticRouter(encoder=encoder, routes=routes, auto_sync="local")
2025-01-06 11:59:56 - semantic_router.utils.logger - WARNING - base.py:356 - _get_index() - No index provided. Using default LocalIndex. 2025-01-06 11:59:56 - 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 11:59:57 - 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 11:59:57 - semantic_router.utils.logger - WARNING - local.py:148 - _write_config() - No config is written for LocalIndex.
rl("isn't politics the best thing ever")
2025-01-06 12:00:03 - 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:00:08 - 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:00: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:03:49 - 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:03:49 - 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:04:00 - 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:04:09 - 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:04:38 - 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:04:38 - 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)
To save our route layer we call the to_json
method:
rl.to_json("layer.json")
2025-01-06 12:00:14 - semantic_router.utils.logger - INFO - base.py:211 - to_file() - 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, 'metadata': {}}, {'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, 'metadata': {}}]}
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 = SemanticRouter.from_json("layer.json")
2025-01-06 12:00:30 - semantic_router.utils.logger - INFO - base.py:96 - from_file() - Loading route config from layer.json 2025-01-06 12:00:31 - semantic_router.utils.logger - WARNING - base.py:356 - _get_index() - No index provided. Using default LocalIndex.
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, metadata={}), 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, metadata={})]
new_rl = SemanticRouter(encoder=rl.encoder, routes=rl.routes, auto_sync="local")
2025-01-06 12:04:38 - semantic_router.utils.logger - WARNING - base.py:356 - _get_index() - No index provided. Using default LocalIndex. 2025-01-06 12:04:38 - semantic_router.utils.logger - WARNING - local.py:148 - _write_config() - No config is written for LocalIndex.
new_rl("isn't politics the best thing ever")
RouteChoice(name='politics', function_call=None, similarity_score=None)
new_rl("how's the weather today?")
RouteChoice(name='chitchat', function_call=None, similarity_score=None)