"Let's load our index first. As mentioned, `\"index\"` is the default index name, so we don't need to specify this parameter — but we do so below for demonstrative purposes."
"Let's load our index first. As mentioned, `\"index\"` is the default index name and `\"namespace\"` is the default namespace name, so we don't need to specify this parameter — but we do so below for demonstrative purposes."
]
},
{
...
...
%% Cell type:markdown id: tags:
[](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/examples/pinecone-and-scaling.ipynb) [](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/examples/pinecone-and-scaling.ipynb)
%% Cell type:markdown id: tags:
# Scaling to Many Routes and Using Pinecone
%% Cell type:markdown id: tags:
Semantic router can be used with many hundreds, thousands, or even more routes. At very large scales it can be useful to use a vector database to store and search though your route vector space. Although we do not demonstrate _very large_ scale in this notebook, we will demonstrate more routes than usual and we will also see how to use the `PineconeIndex` for potential scalability and route persistence beyond our local machines.
'utterances': ["isn't politics the best thing ever",
"why don't you tell me about your political opinions",
"don't you just love the presidentdon't you just hate the president",
"they're going to destroy this country!",
'they will save the country!'],
'description': None,
'function_schema': None,
'llm': None,
'score_threshold': 0.82}
%% Cell type:markdown id: tags:
We transform these into `Route` objects like so:
%% Cell type:code id: tags:
``` python
fromsemantic_routerimportRoute
routes=[Route(**data[i])foriinrange(len(data))]
routes[0]
```
%% Output
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 presidentdon't you just hate the president", "they're going to destroy this country!", 'they will save the country!'], description=None, function_schema=None, llm=None, score_threshold=0.82)
%% Cell type:markdown id: tags:
Next we initialize an `encoder`. We will use a simple `HuggingFaceEncoder`, we can also use popular encoder APIs like `CohereEncoder` and `OpenAIEncoder`.
[32m2024-04-15 01:56:59 INFO semantic_router.utils.logger local[0m
%% Cell type:markdown id: tags:
We run the solely static routes layer:
%% Cell type:code id: tags:
``` python
rl("how's the weather today?").name
```
%% Output
'chitchat'
%% Cell type:markdown id: tags:
_If you see a warning about no classification being found, wait a moment and run the above cell again._
%% Cell type:markdown id: tags:
## Loading Index From Previous Initialization
%% Cell type:markdown id: tags:
Because we're using Pinecone our route index can now persist / be access from different locations by simply connecting to the pre-existing index, by default this index uses the identifier `"semantic-router--index"` — this is the index we'll be loading here, but we can change the name via the `index_name` parameter if preferred.
First, let's delete our old route layer, `index`, and `routes`.
%% Cell type:code id: tags:
``` python
delrl,index,routes
```
%% Cell type:markdown id: tags:
Let's load our index first. As mentioned, `"index"` is the default index name, so we don't need to specify this parameter — but we do so below for demonstrative purposes.
Let's load our index first. As mentioned, `"index"` is the default index name and `"namespace"` is the default namespace name, so we don't need to specify this parameter — but we do so below for demonstrative purposes.
'tech_trends': ['tell me about the latest gadgets',
"what's new in technology?",
'what are the emerging tech trends?'],
'open_source_contributions': ['best practices for open-source contributors',
'finding projects that accept contributions',
'how to start contributing to open source'],
'mobile_app_development': ['Kotlin vs Swift for mobile development',
'optimizing performance in mobile apps',
'best tools for cross-platform mobile development'],
'politics': ["isn't politics the best thing ever",
"don't you just love the presidentdon't you just hate the president",
'they will save the country!',
"why don't you tell me about your political opinions",
"they're going to destroy this country!"],
'motivation': ['give me a motivational quote',
'I need some motivation',
'inspire me'],
'pet_care_advice': ['what should I know about keeping a pet rabbit?',
'suggest some tips for cat care',
'how can I train my dog?'],
'version_control_systems': ['best practices for branching in Git',
'introduction to SVN for beginners',
'how to revert a commit in Git'],
'software_architecture': ['differences between MVC and MVVM',
'introduction to domain-driven design',
'explain microservices architecture'],
'project_management_in_tech': ['agile vs waterfall project management',
'how to lead a development team',
'tools for managing tech projects']}
%% Cell type:markdown id: tags:
Now we transform these into a list of `Route` objects.
%% Cell type:code id: tags:
``` python
routes=[
Route(name=route,utterances=utterances)
forroute,utterancesinroutes_dict.items()
]
routes[0]
```
%% Output
Route(name='food_and_recipes', utterances=["what's your favorite food?", 'suggest a recipe for dinner', 'tell me about a dish from your country'], description=None, function_schema=None, llm=None, score_threshold=None)
[32m2024-04-15 01:57:19 INFO semantic_router.utils.logger local[0m
%% Cell type:markdown id: tags:
And test it again:
%% Cell type:code id: tags:
``` python
rl("say something to make me laugh").name
```
%% Output
'jokes'
%% Cell type:code id: tags:
``` python
rl("tell me something amusing").name
```
%% Output
'jokes'
%% Cell type:code id: tags:
``` python
rl("it's raining cats and dogs today").name
```
%% Output
'chitchat'
%% Cell type:code id: tags:
``` python
# delete index
index.delete_index()
```
%% Cell type:markdown id: tags:
Perfect, our routes loaded from our `PineconeIndex` are working as expected! As mentioned, we can use the `PineconeIndex` for persistance and high scale use-cases, for example where we might have hundreds of thousands of utterances, or even millions.