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
No related branches found
No related tags found
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:
``` 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:
``` 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: "
)
encoder = OpenAIEncoder()
```
%% Cell type:code id: tags:
``` python
from semantic_router.index.postgres import PostgresIndex
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()
```
%% Cell type:code id: tags:
``` python
from semantic_router.layer import RouteLayer
rl = RouteLayer(encoder=encoder, routes=routes, index=postgres_index)
```
%% Cell type:markdown id: tags:
We can check our route layer and index information.
%% Cell type:code id: tags:
``` python
rl.list_route_names()
```
%% Cell type:code id: tags:
``` python
len(rl.index)
```
%% Cell type:markdown id: tags:
We can also view all of the records for a given route:
%% Cell type:code id: tags:
``` python
rl.index._get_route_ids(route_name="politics")
```
%% Cell type:markdown id: tags:
And query:
%% Cell type:code id: tags:
``` python
rl("I like voting")
```
%% Cell type:code id: tags:
``` python
rl("how's the weather today?").name
```
%% Cell type:code id: tags:
``` python
rl("where are you?").name
```
%% Cell type:markdown id: tags:
We can delete or update routes.
%% Cell type:code id: tags:
``` python
len(rl.index)
```
%% Cell type:code id: tags:
``` python
import time
rl.delete(route_name="chitchat")
time.sleep(1)
len(rl.index)
```
%% Cell type:code id: tags:
``` python
rl("how's the weather today?").name
```
%% Cell type:code id: tags:
``` python
rl.index.get_routes()
```
%% Cell type:code id: tags:
``` python
rl.index.describe()
```
......
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