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

chore: lint

parent 8683f68f
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#!pip install -qU "semantic-router[pinecone]==0.0.73" #!pip install -qU "semantic-router[pinecone]==0.0.73"
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Syncing Routes with Pinecone Index # Syncing Routes with Pinecone Index
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
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
from semantic_router import Route from semantic_router import Route
# 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
import os import os
from getpass import getpass from getpass import getpass
from semantic_router.encoders import OpenAIEncoder from semantic_router.encoders import OpenAIEncoder
# get at platform.openai.com # 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") encoder = OpenAIEncoder(name="text-embedding-3-small")
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
For our `PineconeIndex` we do the exact same thing, ie we initialize as usual: For our `PineconeIndex` we do the exact same thing, ie we initialize as usual:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import os import os
from semantic_router.index.pinecone import PineconeIndex from semantic_router.index.pinecone import PineconeIndex
# get at app.pinecone.io # 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( pc_index = PineconeIndex(
dimensions=1536, dimensions=1536,
init_async_index=True, # enables asynchronous methods, it's optional init_async_index=True, # enables asynchronous methods, it's optional
sync=None, # defines whether we sync between local and remote route layers sync=None, # defines whether we sync between local and remote route layers
# when sync is None, no sync is performed # when sync is None, no sync is performed
) )
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## RouteLayer ## RouteLayer
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
The `RouteLayer` class supports both sync and async operations by default, so we initialize as usual: The `RouteLayer` class supports both sync and async operations by default, so we initialize as usual:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from semantic_router.layer import RouteLayer from semantic_router.layer import RouteLayer
rl = RouteLayer(encoder=encoder, routes=routes, index=pc_index) rl = RouteLayer(encoder=encoder, routes=routes, index=pc_index)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We can check our route layer and index information as usual: We can check our route layer and index information as usual:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.list_route_names() rl.list_route_names()
``` ```
%% Output %% Output
['politics', 'chitchat'] ['politics', 'chitchat']
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
len(rl.index) len(rl.index)
``` ```
%% Output %% Output
0 0
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Let's see if our local and remote instances are synchronized... Let's see if our local and remote instances are synchronized...
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import time import time
# due to pinecone indexing latency we wait 3 seconds # due to pinecone indexing latency we wait 3 seconds
time.sleep(3) time.sleep(3)
rl.index._read_hash() rl.index._read_hash()
``` ```
%% Output %% Output
hash_id: sr_hash# hash_id: sr_hash#
ConfigParameter(field='sr_hash', value='f8f04794014c855bd68e283e64c57d8cc7a92f2ecd143386105de98f57c55e04', namespace='', created_at='2024-11-10T21:41:35.991948') ConfigParameter(field='sr_hash', value='f8f04794014c855bd68e283e64c57d8cc7a92f2ecd143386105de98f57c55e04', namespace='', created_at='2024-11-10T21:41:35.991948')
%% 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#
True True
%% Cell type:markdown id: tags: %% 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. 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: %% Cell type:code id: tags:
``` python ``` python
del rl del rl
rl = RouteLayer(encoder=encoder, routes=[politics], index=pc_index) rl = RouteLayer(encoder=encoder, routes=[politics], index=pc_index)
time.sleep(3) time.sleep(3)
``` ```
%% Output %% Output
hash_id: sr_hash# 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. 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: %% 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"]
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
--- ---
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment