Route Layers from File#

Route layers can be saved to and loaded from files. This can be useful if we want to save a route layer to a file for later use, or if we want to load a route layer from a file.

We can save and load route layers to/from YAML or JSON files. For JSON we do:

# save to JSON
rl.to_json("layer.json")
# load from JSON
new_rl = RouteLayer.from_json("layer.json")

For YAML we do:

# save to YAML
rl.to_yaml("layer.yaml")
# load from YAML
new_rl = RouteLayer.from_yaml("layer.yaml")

The saved files contain all the information needed to initialize new route layers. If we are using a remote index, we can use the sync features to keep the route layer in sync with the index.

Full Example#

Open In Colab Open nbviewer

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

Define Route#

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]

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

Test Route#

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)

Save To JSON#

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

Loading from 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)]

Test Route Again#

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)