Skip to content
Snippets Groups Projects
Commit 703ff79e authored by Lawson Taylor's avatar Lawson Taylor
Browse files

[DOCS] add docker image for postgres

parent 889a7360
Branches
Tags
No related merge requests found
services:
postgres:
container_name: pgvector
image: ankane/pgvector
restart: always
shm_size: 128mb
ports:
- "5432:5432"
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: semantic_router
volumes:
- postgres_data:/var/lib/pgvector/data
volumes:
postgres_data:
driver: local
\ No newline at end of file
%% 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: %% Cell type:code id: tags:
``` python ``` python
!echo "Running docker compose to start postgres instance with pgvector extension"
!docker compose -f ./postgres.compose.yaml up --detach
``` ```
%% Output
/Users/frankjames/Projects/semantic-router/docs/indexes/postgres
[?25l[+] Running 0/0
⠋ Container pgvector Starting 0.1s 
[?25h[?25l[+] Running 1/1
✔ Container pgvector Started 0.1s 
[?25h
%% 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 hate the president", "don't you just love 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( os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY") or getpass(
"Enter OpenAI API key: " "Enter OpenAI API key: "
) )
encoder = OpenAIEncoder() encoder = OpenAIEncoder()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from semantic_router.index.postgres import PostgresIndex from semantic_router.index.postgres import PostgresIndex
import os import os
os.environ["POSTGRES_CONNECTION_STRING"] = "postgresql://user:password@localhost:5432/semantic_router" os.environ["POSTGRES_CONNECTION_STRING"] = (
"postgresql://user:password@localhost:5432/semantic_router"
)
postgres_index = PostgresIndex() postgres_index = PostgresIndex()
``` ```
%% 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=postgres_index) rl = RouteLayer(encoder=encoder, routes=routes, index=postgres_index)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We can check our route layer and index information. We can check our route layer and index information.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.list_route_names() rl.list_route_names()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
len(rl.index) len(rl.index)
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We can also view all of the records for a given route: We can also view all of the records for a given route:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.index._get_route_ids(route_name="politics") rl.index._get_route_ids(route_name="politics")
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
And query: And query:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl("I like voting") rl("I like voting")
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl("how's the weather today?").name rl("how's the weather today?").name
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl("where are you?").name rl("where are you?").name
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
We can delete or update routes. We can delete or update routes.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
len(rl.index) len(rl.index)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import time import time
rl.delete(route_name="chitchat") rl.delete(route_name="chitchat")
time.sleep(1) time.sleep(1)
len(rl.index) len(rl.index)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl("how's the weather today?").name rl("how's the weather today?").name
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.index.get_routes() rl.index.get_routes()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rl.index.describe() rl.index.describe()
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment