Skip to content
Snippets Groups Projects
Commit 13d25db4 authored by James Briggs's avatar James Briggs
Browse files

chore: lint

parent 8683f68f
Branches
Tags
No related merge requests found
%% Cell type:code id: tags:
``` python
#!pip install -qU "semantic-router[pinecone]==0.0.73"
```
%% Cell type:markdown id: tags:
# Syncing Routes with Pinecone Index
%% Cell type:markdown id: tags:
When using the `PineconeIndex`, our `RouteLayer` is stored in two places:
* We keep route layer metadata locally.
* 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.
%% Cell type:code id: tags:
``` python
from semantic_router import Route
# we could use this as a guide for our chatbot to avoid political conversations
politics = 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 president",
"don't you just hate the president",
"they're going to destroy this country!",
"they will save the country!",
],
)
# this could be used as an indicator to our chatbot to switch to a more
# conversational prompt
chitchat = Route(
name="chitchat",
utterances=[
"how's the weather today?",
"how are things going?",
"lovely weather today",
"the weather is horrendous",
"let's go to the chippy",
],
)
# we place both of our decisions together into single list
routes = [politics, chitchat]
```
%% Cell type:code id: tags:
``` python
import os
from getpass import getpass
from semantic_router.encoders import OpenAIEncoder
# get at platform.openai.com
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY") or getpass("Enter OpenAI API key: ")
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY") or getpass(
"Enter OpenAI API key: "
)
encoder = OpenAIEncoder(name="text-embedding-3-small")
```
%% Cell type:markdown id: tags:
For our `PineconeIndex` we do the exact same thing, ie we initialize as usual:
%% Cell type:code id: tags:
``` python
import os
from semantic_router.index.pinecone import PineconeIndex
# get at app.pinecone.io
os.environ["PINECONE_API_KEY"] = os.environ.get("PINECONE_API_KEY") or getpass("Enter Pinecone API key: ")
os.environ["PINECONE_API_KEY"] = os.environ.get("PINECONE_API_KEY") or getpass(
"Enter Pinecone API key: "
)
pc_index = PineconeIndex(
dimensions=1536,
init_async_index=True, # enables asynchronous methods, it's optional
sync=None, # defines whether we sync between local and remote route layers
# when sync is None, no sync is performed
)
```
%% Cell type:markdown id: tags:
## RouteLayer
%% Cell type:markdown id: tags:
The `RouteLayer` class supports both sync and async operations by default, so we initialize as usual:
%% Cell type:code id: tags:
``` python
from semantic_router.layer import RouteLayer
rl = RouteLayer(encoder=encoder, routes=routes, index=pc_index)
```
%% Cell type:markdown id: tags:
We can check our route layer and index information as usual:
%% Cell type:code id: tags:
``` python
rl.list_route_names()
```
%% Output
['politics', 'chitchat']
%% Cell type:code id: tags:
``` python
len(rl.index)
```
%% Output
0
%% Cell type:markdown id: tags:
Let's see if our local and remote instances are synchronized...
%% Cell type:code id: tags:
``` python
import time
# due to pinecone indexing latency we wait 3 seconds
time.sleep(3)
rl.index._read_hash()
```
%% Output
hash_id: sr_hash#
ConfigParameter(field='sr_hash', value='f8f04794014c855bd68e283e64c57d8cc7a92f2ecd143386105de98f57c55e04', namespace='', created_at='2024-11-10T21:41:35.991948')
%% Cell type:code id: tags:
``` python
rl.is_synced()
```
%% Output
hash_id: sr_hash#
True
%% Cell type:markdown id: tags:
It looks like everything is synced! Let's try deleting our local route layer, initializing it with just the politics route, and checking again.
%% Cell type:code id: tags:
``` python
del rl
rl = RouteLayer(encoder=encoder, routes=[politics], index=pc_index)
time.sleep(3)
```
%% Output
hash_id: sr_hash#
2024-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.
%% Cell type:markdown id: tags:
Let's try `rl.is_synced()` again:
%% Cell type:code id: tags:
``` python
rl.is_synced()
```
%% Output
hash_id: sr_hash#
False
%% Cell type:markdown id: tags:
We can use the `get_utterance_diff` method to see exactly _why_ our local and remote are not synced
%% Cell type:code id: tags:
``` python
rl.get_utterance_diff()
```
%% 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"]
["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"]
%% Cell type:markdown id: tags:
---
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment