"[](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/encoders/fastembed.ipynb) [](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/encoders/fastembed.ipynb)"
"[](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/encoders/openai-embed-3.ipynb) [](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/encoders/openai-embed-3.ipynb)"
]
]
},
},
{
{
...
...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
[](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/encoders/fastembed.ipynb) [](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/encoders/fastembed.ipynb)
[](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/encoders/openai-embed-3.ipynb) [](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/encoders/openai-embed-3.ipynb)
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Using OpenAI Embed 3 Models
# Using OpenAI Embed 3 Models
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
The 3rd generation embedding models from OpenAI (`text-embedding-3-small` and `text-embedding-3-large`) can both be used with our `OpenAIEncoder` and usage is primarily the same as with the 2nd generation `text-embedding-ada-002`. However, there is a new `dimensions` parameter — which we will discuss below.
The 3rd generation embedding models from OpenAI (`text-embedding-3-small` and `text-embedding-3-large`) can both be used with our `OpenAIEncoder` and usage is primarily the same as with the 2nd generation `text-embedding-ada-002`. However, there is a new `dimensions` parameter — which we will discuss below.
%% 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 semantic-router. Support for the new `dimensions` parameter was added in `semantic-router==0.0.19` and `openai==1.10.0`.
We start by installing semantic-router. Support for the new `dimensions` parameter was added in `semantic-router==0.0.19` and `openai==1.10.0`.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
!pipinstall-qUsemantic-router==0.0.20
!pipinstall-qUsemantic-router==0.0.20
```
```
%% 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 love the president",
"don't you just hate the president",
"don't you just hate the president",
"they're going to destroy this country!",
"they're going to destroy this country!",
"they will save the country!",
"they will save the country!",
],
],
)
)
```
```
%% Output
%% 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
/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
from .autonotebook import tqdm as notebook_tqdm
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Let's define another for good measure:
Let's define another for good measure:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
chitchat=Route(
chitchat=Route(
name="chitchat",
name="chitchat",
utterances=[
utterances=[
"how's the weather today?",
"how's the weather today?",
"how are things going?",
"how are things going?",
"lovely weather today",
"lovely weather today",
"the weather is horrendous",
"the weather is horrendous",
"let's go to the chippy",
"let's go to the chippy",
],
],
)
)
routes=[politics,chitchat]
routes=[politics,chitchat]
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Now we initialize our embedding model, we will use the `-3-large` model alongside a `dimensions` value of `256`. This will produce _tiny_ 256-dimensional vectors that — according to OpenAI — outperform the 1536-dimensional vectors produced by `text-embedding-ada-002`.
Now we initialize our embedding model, we will use the `-3-large` model alongside a `dimensions` value of `256`. This will produce _tiny_ 256-dimensional vectors that — according to OpenAI — outperform the 1536-dimensional vectors produced by `text-embedding-ada-002`.
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
rl=RouteLayer(encoder=encoder,routes=routes)
rl=RouteLayer(encoder=encoder,routes=routes)
```
```
%% Output
%% Output
[32m2024-01-28 12:40:08 INFO semantic_router.utils.logger Initializing RouteLayer[0m
[32m2024-01-28 12:40:08 INFO semantic_router.utils.logger Initializing RouteLayer[0m
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We can check the dimensionality of our vectors by looking at the `index` attribute of the `RouteLayer`.
We can check the dimensionality of our vectors by looking at the `index` attribute of the `RouteLayer`.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
rl.index.shape
rl.index.shape
```
```
%% Output
%% Output
(11, 256)
(11, 256)
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We do have 256-dimensional vectors. Now let's test them:
We do have 256-dimensional vectors. Now let's test them:
In this case, we return `None` because no matches were identified. We always recommend optimizing your `RouteLayer` for optimal performance, you can see how in [this notebook](https://github.com/aurelio-labs/semantic-router/blob/main/docs/06-threshold-optimization.ipynb).
In this case, we return `None` because no matches were identified. We always recommend optimizing your `RouteLayer` for optimal performance, you can see how in [this notebook](https://github.com/aurelio-labs/semantic-router/blob/main/docs/06-threshold-optimization.ipynb).