-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.R
31 lines (23 loc) · 984 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(DBI)
db <- dbConnect(RPostgres::Postgres(), dbname="pgvector_r_test")
invisible(dbExecute(db, "CREATE EXTENSION IF NOT EXISTS vector"))
invisible(dbExecute(db, "DROP TABLE IF EXISTS items"))
invisible(dbExecute(db, "CREATE TABLE items (id bigserial PRIMARY KEY, 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(dbAppendTable(db, "items", items))
params <- pgvector.serialize(c(1, 1, 1))
result <- dbGetQuery(db, "SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", params=params)
print(pgvector.unserialize(result$embedding))
invisible(dbExecute(db, "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)"))