" \"why don't you tell me about your political opinions\",\n",
" \"why don't you tell me about your political opinions\",\n",
" \"don't you just love the president\" \"don't you just hate the president\",\n",
" \"don't you just love the president\",\n",
" \"don't you just hate the president\",\n",
" \"they're going to destroy this country!\",\n",
" \"they're going to destroy this country!\",\n",
" \"they will save the country!\",\n",
" \"they will save the country!\",\n",
" ],\n",
" ],\n",
...
...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
[](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/00-introduction.ipynb) [](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/00-introduction.ipynb)
[](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/00-introduction.ipynb) [](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/00-introduction.ipynb)
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Semantic Router Intro
# Semantic Router Intro
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
The Semantic Router library can be used as a super fast route making layer on top of LLMs. That means rather than waiting on a slow agent to decide what to do, we can use the magic of semantic vector space to make routes. Cutting route making time down from seconds to milliseconds.
The Semantic Router library can be used as a super fast route making layer on top of LLMs. That means rather than waiting on a slow agent to decide what to do, we can use the magic of semantic vector space to make routes. Cutting route making time down from seconds to milliseconds.
%% 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:
We start by defining a dictionary mapping routes to example phrases that should trigger those routes.
We start by defining a dictionary mapping routes to example phrases that should trigger those 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",
Now we define the `RouteLayer`. When called, the route layer will consume text (a query) and output the category (`Route`) it belongs to — to initialize a `RouteLayer` we need our `encoder` model and a list of `routes`.
Now we define the `RouteLayer`. When called, the route layer will consume text (a query) and output the category (`Route`) it belongs to — to initialize a `RouteLayer` we need our `encoder` model and a list of `routes`.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
fromsemantic_router.layerimportRouteLayer
fromsemantic_router.layerimportRouteLayer
dl=RouteLayer(encoder=encoder,routes=routes)
dl=RouteLayer(encoder=encoder,routes=routes)
```
```
%% Output
%% Output
[32m2023-12-28 19:14:34 INFO semantic_router.utils.logger Initializing RouteLayer[0m
[32m2023-12-28 19:14:34 INFO semantic_router.utils.logger Initializing RouteLayer[0m
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Now we can test it:
Now we can test it:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
dl("don't you love politics?")
dl("don't you love politics?")
```
```
%% Output
%% Output
RouteChoice(name='politics', function_call=None)
RouteChoice(name='politics', function_call=None)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
dl("how's the weather today?")
dl("how's the weather today?")
```
```
%% Output
%% Output
RouteChoice(name='chitchat', function_call=None)
RouteChoice(name='chitchat', function_call=None)
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Both are classified accurately, what if we send a query that is unrelated to our existing `Route` objects?
Both are classified accurately, what if we send a query that is unrelated to our existing `Route` objects?
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
dl("I'm interested in learning about llama 2")
dl("I'm interested in learning about llama 2")
```
```
%% Output
%% Output
RouteChoice(name=None, function_call=None)
RouteChoice(name=None, function_call=None)
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
In this case, we return `None` because no matches were identified.
In this case, we return `None` because no matches were identified.