When using the `PineconeIndex`, our `RouteLayer` is stored in two places:
When using the `PineconeIndex`, our `RouteLayer` is stored in two places:
* We keep route layer metadata locally.
* We keep route layer metadata locally.
* Vectors alongside a backup of our metadata is stored remotely in Pinecone.
* Vectors alongside a backup of our metadata is stored remotely in Pinecone.
By storing some data locally and some remotely we achieve improved persistence and the ability to recover our local state if lost. However, it does come with challenges around keep our local and remote instances synchronized. Fortunately, we have [several synchronization options](https://docs.aurelio.ai/semantic-router/route_layer/sync.html). In this example, we'll see how to use these options to keep our local and remote Pinecone instances synchronized.
By storing some data locally and some remotely we achieve improved persistence and the ability to recover our local state if lost. However, it does come with challenges around keep our local and remote instances synchronized. Fortunately, we have [several synchronization options](https://docs.aurelio.ai/semantic-router/route_layer/sync.html). In this example, we'll see how to use these options to keep our local and remote Pinecone instances synchronized.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
fromsemantic_routerimportRoute
fromsemantic_routerimportRoute
# we could use this as a guide for our chatbot to avoid political conversations
# we could use this as a guide for our chatbot to avoid political conversations
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!",
],
],
)
)
# this could be used as an indicator to our chatbot to switch to a more
# this could be used as an indicator to our chatbot to switch to a more
# conversational prompt
# conversational prompt
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",
],
],
)
)
# we place both of our decisions together into single list
# we place both of our decisions together into single list
routes=[politics,chitchat]
routes=[politics,chitchat]
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
importos
importos
fromgetpassimportgetpass
fromgetpassimportgetpass
fromsemantic_router.encodersimportOpenAIEncoder
fromsemantic_router.encodersimportOpenAIEncoder
# get at platform.openai.com
# get at platform.openai.com
os.environ["OPENAI_API_KEY"]=os.environ.get("OPENAI_API_KEY")orgetpass("Enter OpenAI API key: ")
[33m2024-11-10 22:41:41 WARNING semantic_router.utils.logger Local and remote route layers were not aligned. Remote hash not updated. Use `RouteLayer.get_utterance_diff()` to see details.[0m
[33m2024-11-10 22:41:41 WARNING semantic_router.utils.logger Local and remote route layers were not aligned. Remote hash not updated. Use `RouteLayer.get_utterance_diff()` to see details.[0m
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Let's try `rl.is_synced()` again:
Let's try `rl.is_synced()` again:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
rl.is_synced()
rl.is_synced()
```
```
%% Output
%% Output
hash_id: sr_hash#
hash_id: sr_hash#
False
False
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We can use the `get_utterance_diff` method to see exactly _why_ our local and remote are not synced
We can use the `get_utterance_diff` method to see exactly _why_ our local and remote are not synced
%% Cell type:code id: tags:
%% Cell type:code id: tags:
``` python
``` python
rl.get_utterance_diff()
rl.get_utterance_diff()
```
```
%% Output
%% Output
['chitchat: how are things going?', "chitchat: how's the weather today?", "chitchat: let's go to the chippy", 'chitchat: lovely weather today', 'chitchat: the weather is horrendous', "politics: don't you just hate the president", "politics: don't you just love the president", "politics: isn't politics the best thing ever", 'politics: they will save the country!', "politics: they're going to destroy this country!", "politics: why don't you tell me about your political opinions"]
['chitchat: how are things going?', "chitchat: how's the weather today?", "chitchat: let's go to the chippy", 'chitchat: lovely weather today', 'chitchat: the weather is horrendous', "politics: don't you just hate the president", "politics: don't you just love the president", "politics: isn't politics the best thing ever", 'politics: they will save the country!', "politics: they're going to destroy this country!", "politics: why don't you tell me about your political opinions"]
["politics: don't you just hate the president", "politics: don't you just love the president", "politics: isn't politics the best thing ever", 'politics: they will save the country!', "politics: they're going to destroy this country!", "politics: why don't you tell me about your political opinions"]
["politics: don't you just hate the president", "politics: don't you just love the president", "politics: isn't politics the best thing ever", 'politics: they will save the country!', "politics: they're going to destroy this country!", "politics: why don't you tell me about your political opinions"]
['+ chitchat: how are things going?',
['+ chitchat: how are things going?',
"+ chitchat: how's the weather today?",
"+ chitchat: how's the weather today?",
"+ chitchat: let's go to the chippy",
"+ chitchat: let's go to the chippy",
'+ chitchat: lovely weather today',
'+ chitchat: lovely weather today',
'+ chitchat: the weather is horrendous',
'+ chitchat: the weather is horrendous',
" politics: don't you just hate the president",
" politics: don't you just hate the president",
" politics: don't you just love the president",
" politics: don't you just love the president",
" politics: isn't politics the best thing ever",
" politics: isn't politics the best thing ever",
' politics: they will save the country!',
' politics: they will save the country!',
" politics: they're going to destroy this country!",
" politics: they're going to destroy this country!",
" politics: why don't you tell me about your political opinions"]
" politics: why don't you tell me about your political opinions"]