"# os.environ[\"COHERE_API_KEY\"] = getpass(\"Enter Cohere API Key: \")\n",
"os.environ[\"COHERE_API_KEY\"] = os.environ[\"COHERE_API_KEY\"] or \\\n",
"os.environ[\"COHERE_API_KEY\"]\n",
" getpass(\"Enter Cohere API Key: \")\n",
"\n",
"encoder = CohereEncoder()"
"encoder = CohereEncoder()"
]
]
},
},
...
...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
# Semantic Router Walkthrough
# Semantic Router Walkthrough
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
The Semantic Router library can be used as a super fast decision 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 decisions. Cutting decision making time down from seconds to milliseconds.
The Semantic Router library can be used as a super fast decision 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 decisions. Cutting decision 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.1
!pipinstall-qUsemantic-router==0.0.1
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We start by defining a dictionary mapping decisions to example phrases that should trigger those decisions.
We start by defining a dictionary mapping decisions to example phrases that should trigger those decisions.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
fromsemantic_router.schemaimportDecision
fromsemantic_router.schemaimportDecision
politics=Decision(
politics=Decision(
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!"
]
]
)
)
```
```
%% 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=Decision(
chitchat=Decision(
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"
]
]
)
)
decisions=[politics,chitchat]
decisions=[politics,chitchat]
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Now we initialize our embedding model:
Now we initialize our embedding model:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
fromsemantic_router.encodersimportCohereEncoder
fromsemantic_router.encodersimportCohereEncoder
fromgetpassimportgetpass
fromgetpassimportgetpass
importos
importos
# os.environ["COHERE_API_KEY"] = getpass("Enter Cohere API Key: ")
Now we define the `DecisionLayer`. When called, the decision layer will consume text (a query) and output the category (`Decision`) it belongs to — to initialize a `DecisionLayer` we need our `encoder` model and a list of `decisions`.
Now we define the `DecisionLayer`. When called, the decision layer will consume text (a query) and output the category (`Decision`) it belongs to — to initialize a `DecisionLayer` we need our `encoder` model and a list of `decisions`.