-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgvectorHelloWorld.py
49 lines (39 loc) · 1.32 KB
/
pgvectorHelloWorld.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import vecs
# connection requires client to communicate with Postgres, I used Neon https://neon.tech/
DB_CONNECTION = "postgresql://<user>:<password>@<host>:<port>/<db_name>"
# create vector store client
vx = vecs.create_client(DB_CONNECTION)
# create collection
docs = vx.create_collection(name="docs", dimension=3)
# get an existing collection
docs = vx.get_collection(name="docs")
# add records to the collection
docs.upsert(
records=[
(
"vec0", # the vector's identifier
[0.1, 0.2, 0.3], # the vector. list or np.array
{"year": 1973} # associated metadata
),
(
"vec1",
[0.7, 0.8, 0.9],
{"year": 2012}
)
]
)
# index the collection to be queried by cosine distance
docs.create_index(measure=vecs.IndexMeasure.cosine_distance)
# basic query
docs.query(
data=[0.4,0.5,0.6], # required
limit=5, # number of records to return
filters={}, # metadata filters
measure="cosine_distance", # distance measure to use
include_value=False, # should distance measure values be returned?
include_metadata=False, # should record metadata be returned?
# metatdata filtering
docs.query(
data=[0.4,0.5,0.6],
filters={"year": {"$eq": 2012}}, # metadata filters
)