Save load 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:
For YAML we do:
The saved files contain all the information needed to initialize new semantic routers. If you are using a remote index, you can use the sync features to keep the router in sync with the index.
Full Example
Here we will show how to save routers to YAML or JSON files, and how to load a router from file.
We start by installing the library:
Define Route
First let’s create a list of routes:
We define a semantic router using these routes and using the Cohere encoder.
Test Route
Output:
Output:
Save To JSON
To save our semantic router we call the to_json
method:
Loading from JSON
We can view the router file we just saved to see what information is stored.
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.
We can confirm that our router has been initialized with the expected attributes by viewing the SemanticRouter
object:
Test Route Again
Output:
Output: