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

chore: lint

parent bb86c6e6
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Postgres pgvector Index Example
**Note**: You'll require Docker to be installed locally, or a remote instance of Postgres with the pgvector extension installed.
%% Cell type:code id: tags:
``` python
# Start the Postgres instance with the pgvector extension using Docker Compose
!echo "Running Docker Compose to start Postgres instance with pgvector extension"
!docker compose -f ./docs/indexes/postgres/postgres.compose.yaml up -d
```
%% Cell type:code id: tags:
``` python
# Import necessary modules
from semantic_router import Route
# Define routes to guide the chatbot's responses
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!",
],
)
# Define a chitchat route for general conversations
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",
],
)
# Combine both routes into a single list
routes = [politics, chitchat]
```
%% Output
/home/vittorio/.cache/pypoetry/virtualenvs/semantic-router-EZimjtOW-py3.10/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from .autonotebook import tqdm as notebook_tqdm
%% Cell type:code id: tags:
``` python
# Import necessary modules
import os
from getpass import getpass
from semantic_router.encoders import OpenAIEncoder
# Set OpenAI API key for the encoder
# You can get your API key from platform.openai.com
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY") or getpass(
"Enter OpenAI API key: "
)
encoder = OpenAIEncoder()
```
%% Cell type:code id: tags:
``` python
# Import the Postgres index module
from semantic_router.index.postgres import PostgresIndex
import os
# Set Postgres connection string
os.environ["POSTGRES_CONNECTION_STRING"] = (
"postgresql://admin:root@localhost:5432/semantic_router"
)
os.environ[
"POSTGRES_CONNECTION_STRING"
] = "postgresql://admin:root@localhost:5432/semantic_router"
# Initialize the Postgres index
postgres_index = PostgresIndex()
```
%% Cell type:code id: tags:
``` python
# Import the RouteLayer class
from semantic_router.layer import RouteLayer
# Initialize the RouteLayer with the encoder, routes, and index
rl = RouteLayer(encoder=encoder, routes=routes, index=postgres_index)
```
%% Cell type:markdown id: tags:
## Check Route Layer and Index Information
We can check our route layer and index information.
%% Cell type:code id: tags:
``` python
# List the names of the defined routes
rl.list_route_names()
```
%% Output
['politics', 'chitchat']
%% Cell type:code id: tags:
``` python
# Check the total number of entries in the index
len(rl.index)
```
%% Output
10
%% Cell type:markdown id: tags:
## View All Records for a Given Route
We can also view all of the records for a given route:
%% Cell type:code id: tags:
``` python
# Get all records for the 'politics' route
rl.index._get_route_ids(route_name="politics")
```
%% Output
['politics#af3cd8c8-defb-5940-b66a-d59e8dbbada7',
'politics#ce06418e-133f-5484-83fb-d8d3f136fc5d',
'politics#c24f2d13-c2d5-5ddf-9169-03646cc3dad0',
'politics#5f21f564-3d1e-58d9-a0b5-9e0c79e33d72',
'politics#f248c2fa-4ab0-5bf9-8678-b25f7945705a']
%% Cell type:markdown id: tags:
## Query the Routes
We can query the routes to get the appropriate responses.
%% Cell type:code id: tags:
``` python
# Query the route layer with a statement related to politics
rl("I like voting. What do you think about the president?")
```
%% Output
RouteChoice(name='politics', function_call=None, similarity_score=None)
%% Cell type:code id: tags:
``` python
# Query the route layer with a chitchat statement
rl("how's the weather today?").name
```
%% Output
'chitchat'
%% Cell type:code id: tags:
``` python
# Query the route layer with another chitchat statement
rl("where are you?").name
```
%% Output
'chitchat'
%% Cell type:markdown id: tags:
## Delete or Update Routes
We can delete or update routes as needed.
%% Cell type:code id: tags:
``` python
# Check the total number of entries in the index before deletion
len(rl.index)
```
%% Output
10
%% Cell type:code id: tags:
``` python
# Delete the 'chitchat' route and check the index length after deletion
import time
rl.delete(route_name="chitchat")
time.sleep(1)
len(rl.index)
```
%% Output
5
%% Cell type:code id: tags:
``` python
# Attempt to query the deleted 'chitchat' route
print(rl("how's the weather today?").name)
```
%% Output
None
%% Cell type:code id: tags:
``` python
# Get all the current routes and their utterances
rl.index.get_routes()
```
%% Output
[('politics', "isn't politics the best thing ever"),
('politics', "why don't you tell me about your political opinions"),
('politics', "don't you just love the president"),
('politics', "they're going to destroy this country!"),
('politics', 'they will save the country!')]
%% Cell type:code id: tags:
``` python
# Describe the index to get details like type, dimensions, and total vector count
rl.index.describe()
```
%% Output
{'type': 'postgres', 'dimensions': 1536, 'total_vector_count': 5}
......
......@@ -198,7 +198,6 @@ class BedrockEncoder(DenseEncoder):
embeddings = []
if self.name and "amazon" in self.name:
for doc in docs:
embedding_body = {}
if isinstance(doc, dict):
......
......@@ -674,7 +674,6 @@ class PineconeIndex(BaseIndex):
if self.base_url and "api.pinecone.io" in self.base_url:
self.index.delete(ids=route_vec_ids, namespace=self.namespace)
else:
response = requests.post(
f"{self.index_host}/vectors/delete",
json=DeleteRequest(
......
......@@ -101,9 +101,12 @@ class HybridRouter(BaseRouter):
if isinstance(routes, Route):
routes = [routes]
# create embeddings for all routes
route_names, all_utterances, all_function_schemas, all_metadata = (
self._extract_routes_details(routes, include_metadata=True)
)
(
route_names,
all_utterances,
all_function_schemas,
all_metadata,
) = self._extract_routes_details(routes, include_metadata=True)
# TODO: to merge, self._encode should probably output a special
# TODO Embedding type that can be either dense or hybrid
dense_emb, sparse_emb = self._encode(all_utterances)
......
......@@ -73,9 +73,12 @@ class SemanticRouter(BaseRouter):
if isinstance(routes, Route):
routes = [routes]
# create embeddings for all routes
route_names, all_utterances, all_function_schemas, all_metadata = (
self._extract_routes_details(routes, include_metadata=True)
)
(
route_names,
all_utterances,
all_function_schemas,
all_metadata,
) = self._extract_routes_details(routes, include_metadata=True)
dense_emb = self._encode(all_utterances)
self.index.add(
embeddings=dense_emb.tolist(),
......
......@@ -81,7 +81,6 @@ def init_index(
issues during testing.
"""
if index_cls is PineconeIndex:
if index_name:
if not dimensions and "OpenAIEncoder" in index_name:
dimensions = 1536
......
......@@ -115,7 +115,6 @@ def init_index(
issues during testing.
"""
if index_cls is PineconeIndex:
if index_name:
if not dimensions and "OpenAIEncoder" in index_name:
dimensions = 1536
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment