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:
Copy
Ask AI
# save to JSONrouter.to_json("router.json")# load from JSONnew_router = SemanticRouter.from_json("router.json")
For YAML we do:
Copy
Ask AI
# save to YAMLrouter.to_yaml("router.yaml")# load from YAMLnew_router = SemanticRouter.from_yaml("router.yaml")
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.
from semantic_router import Routepolitics = 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 semantic router using these routes and using the Cohere encoder.