[](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:
# 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.
%% Cell type:markdown id: tags:
## Getting Started
%% Cell type:markdown id: tags:
We start by installing the library:
%% Cell type:code id: tags:
``` python
!pipinstall-qUsemantic-router==0.0.14
```
%% Cell type:markdown id: tags:
_**⚠️ If using Google Colab, install the prerequisites and then restart the notebook before continuing**_
%% Cell type:markdown id: tags:
## Saving to JSON
%% Cell type:markdown id: tags:
First let's create a list of routes:
%% Cell type:code id: tags:
``` python
fromsemantic_routerimportRoute
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]
```
%% 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
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.
%% Cell type:markdown id: tags:
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
%% Cell type:markdown id: tags:
To save our route layer we call the `to_json` method:
%% Cell type:code id: tags:
``` python
layer.to_json("layer.json")
```
%% Output
[32m2023-12-28 19:17:03 INFO semantic_router.utils.logger Saving route config to layer.json[0m
%% Cell type:markdown id: tags:
## Loading from JSON
%% Cell type:markdown id: tags:
We can view the router file we just saved to see what information is stored.
%% Cell type:code id: tags:
``` python
importjson
withopen("layer.json","r")asf:
router_json=json.load(f)
print(router_json)
```
%% 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}]}
%% 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.
%% Cell type:code id: tags:
``` python
layer=RouteLayer.from_json("layer.json")
```
%% 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 Initializing RouteLayer[0m
%% Cell type:markdown id: tags:
We can confirm that our layer has been initialized with the expected attributes by viewing the `RouteLayer` object:
%% Cell type:code id: tags:
``` python
print(f"""{layer.encoder.type=}
print(
f"""{layer.encoder.type=}
{layer.encoder.name=}
{layer.routes=}""")
{layer.routes=}"""
)
```
%% Output
layer.encoder.type='cohere'
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)]