forked from pgvector/pgvector-r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.R
31 lines (23 loc) · 966 Bytes
/
example.R
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
library(dbx)
db <- dbxConnect(adapter="postgres", dbname="pgvector_r_test")
invisible(dbxExecute(db, "CREATE EXTENSION IF NOT EXISTS vector"))
invisible(dbxExecute(db, "DROP TABLE IF EXISTS items"))
invisible(dbxExecute(db, "CREATE TABLE items (embedding vector(3))"))
pgvector.serialize <- function(v) {
stopifnot(is.numeric(v))
paste0("[", paste(v, collapse=","), "]")
}
pgvector.unserialize <- function(v) {
lapply(strsplit(substring(v, 2, nchar(v) - 1), ","), as.numeric)
}
embeddings <- matrix(c(
1, 1, 1,
2, 2, 2,
1, 1, 2
), nrow=3, byrow=TRUE)
items <- data.frame(embedding=apply(embeddings, 1, pgvector.serialize))
invisible(dbxInsert(db, "items", items))
params <- pgvector.serialize(c(1, 1, 1))
result <- dbxSelect(db, "SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5", params=params)
print(pgvector.unserialize(result$embedding))
invisible(dbxExecute(db, "CREATE INDEX my_index ON items USING ivfflat (embedding vector_l2_ops)"))