Dynamic Routes
In semantic-router there are two types of routes that can be chosen.
Both routes belong to the Route
object, the only difference between
them is that static routes return a Route.name
when chosen,
whereas dynamic routes use an LLM call to produce parameter input
values.
For example, a static route will tell us if a query is talking about
mathematics by returning the route name (which could be "math"
for
example). A dynamic route does the same thing, but it also extracts
key information from the input utterance to be used in a function
associated with that route.
For example we could provide a dynamic route with associated utterances:
and we could also provide the route with a schema outlining key features of the function:
Then, if the users input utterance is “What is 2 to the power of 3?”,
the route will be triggered, as the input utterance is semantically
similar to the route utterances. Furthermore, the route utilizes an LLM
to identify that base=2
and expoenent=3
. These values are
returned in such a way that they can be used in the above power
function. That is, the dynamic router automates the process of calling
relevant functions from natural language inputs.
As with static routes, we must create a dynamic route before adding it
to our route layer. To make a route dynamic, we need to provide the
function_schemas
as a list. Each function schema provides
instructions on what a function is, so that an LLM can decide how to use
it correctly.
To get the function schema we can use the get_schemas_openai
function.
We use this to define our dynamic route:
Then add the new route to a route layer.
Full Example
Installing the Library
Initializing Routes and RouteLayer
Dynamic routes are treated in the same way as static routes, let’s begin
by initializing a RouteLayer
consisting of static routes.
⚠️ Note: We have a fully local version of dynamic routes available at docs/05-local-execution.ipynb. The local 05 version tends to outperform the OpenAI version we demo in this notebook, so we’d recommend trying 05!
We initialize our RouteLayer
with our encoder
and routes
. We
can use popular encoder APIs like CohereEncoder
and
OpenAIEncoder
, or local alternatives like FastEmbedEncoder
.
We run the solely static routes layer:
Creating a Dynamic Route
As with static routes, we must create a dynamic route before adding it
to our route layer. To make a route dynamic, we need to provide the
function_schemas
as a list. Each function schema provides
instructions on what a function is, so that an LLM can decide how to use
it correctly.
To get the function schema we can use the get_schema
function from
the function_call
module.
We use this to define our dynamic route:
Add the new route to our layer
:
Now we can ask our layer a time related question to trigger our new dynamic route.
Our dynamic route provides both the route itself and the input parameters required to use the route.
Dynamic Routes with Multiple Functions
Routes can be assigned multiple functions. Then, when that particular Route is selected by the Route Layer, a number of those functions might be invoked due to the users utterance containing relevant information that fits their arguments.
Let’s define a Route that has multiple functions.