[](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/01-save-load-from-file.ipynb) [](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/01-save-load-from-file.ipynb)
[](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/01-save-load-from-file.ipynb) [](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/01-save-load-from-file.ipynb)
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Route Layers from File
# Route Layers from File
Here we will show how to save routers to YAML or JSON files, and how to load a route layer from file.
Here we will show how to save routers to YAML or JSON files, and how to load a route layer from file.
%% 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
!pipinstall-qUsemantic-router==0.0.14
!pipinstall-qUsemantic-router==0.0.14
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
_**⚠️ If using Google Colab, install the prerequisites and then restart the notebook before continuing**_
_**⚠️ If using Google Colab, install the prerequisites and then restart the notebook before continuing**_
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Saving to JSON
## Saving to JSON
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
First let's create a list of routes:
First let's create a list of routes:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
fromsemantic_routerimportRoute
fromsemantic_routerimportRoute
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 hate the president",
"don't you just love 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!",
],
],
)
)
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]
```
```
%% Output
%% Output
/Users/jamesbriggs/opt/anaconda3/envs/decision-layer/lib/python3.11/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/opt/anaconda3/envs/decision-layer/lib/python3.11/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
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We define a route layer using these routes and using the default Cohere encoder.
We define a route layer using these routes and using the default Cohere encoder.
[32m2023-12-28 19:16:54 INFO semantic_router.utils.logger Initializing RouteLayer[0m
[32m2023-12-28 19:16:54 INFO semantic_router.utils.logger Initializing RouteLayer[0m
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
To save our route layer we call the `to_json` method:
To save our route layer we call the `to_json` method:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
layer.to_json("layer.json")
layer.to_json("layer.json")
```
```
%% Output
%% Output
[32m2023-12-28 19:17:03 INFO semantic_router.utils.logger Saving route config to layer.json[0m
[32m2023-12-28 19:17:03 INFO semantic_router.utils.logger Saving route config to layer.json[0m
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
## Loading from JSON
## Loading from JSON
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We can view the router file we just saved to see what information is stored.
We can view the router file we just saved to see what information is stored.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
importjson
importjson
withopen("layer.json","r")asf:
withopen("layer.json","r")asf:
router_json=json.load(f)
router_json=json.load(f)
print(router_json)
print(router_json)
```
```
%% Output
%% Output
{'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_schema': None}, {'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_schema': None}]}
{'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_schema': None}, {'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_schema': None}]}
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
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.
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.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
layer=RouteLayer.from_json("layer.json")
layer=RouteLayer.from_json("layer.json")
```
```
%% Output
%% Output
[32m2023-12-28 19:17:08 INFO semantic_router.utils.logger Loading route config from layer.json[0m
[32m2023-12-28 19:17:08 INFO semantic_router.utils.logger Loading route config from layer.json[0m
[32m2023-12-28 19:17:08 INFO semantic_router.utils.logger Initializing RouteLayer[0m
[32m2023-12-28 19:17:08 INFO semantic_router.utils.logger Initializing RouteLayer[0m
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We can confirm that our layer has been initialized with the expected attributes by viewing the `RouteLayer` object:
We can confirm that our layer has been initialized with the expected attributes by viewing the `RouteLayer` object:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
print(f"""{layer.encoder.type=}
print(
f"""{layer.encoder.type=}
{layer.encoder.name=}
{layer.encoder.name=}
{layer.routes=}""")
{layer.routes=}"""
)
```
```
%% Output
%% Output
layer.encoder.type='cohere'
layer.encoder.type='cohere'
layer.encoder.name='embed-english-v3.0'
layer.encoder.name='embed-english-v3.0'
layer.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_schema=None), 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_schema=None)]
layer.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_schema=None), 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_schema=None)]