-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.R
More file actions
40 lines (33 loc) · 1.26 KB
/
example.R
File metadata and controls
40 lines (33 loc) · 1.26 KB
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
library(DBI)
library(httr2)
db <- dbConnect(RPostgres::Postgres(), dbname="pgvector_example")
invisible(dbExecute(db, "CREATE EXTENSION IF NOT EXISTS vector"))
invisible(dbExecute(db, "DROP TABLE IF EXISTS documents"))
invisible(dbExecute(db, "CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(1536))"))
embed <- function(input) {
url <- "https://api.openai.com/v1/embeddings"
token <- Sys.getenv("OPENAI_API_KEY")
data <- list(
input=input,
model="text-embedding-3-small"
)
resp <- request(url) |> req_auth_bearer_token(token) |> req_body_json(data) |> req_perform()
lapply((resp |> resp_body_json())$data, function(x) { unlist(x$embedding) })
}
encodeVector <- function(vec) {
stopifnot(is.numeric(vec))
paste0("[", paste(vec, collapse=","), "]")
}
input <- c(
"The dog is barking",
"The cat is purring",
"The bear is growling"
)
embeddings <- embed(input)
items <- data.frame(content=input, embedding=sapply(embeddings, encodeVector))
invisible(dbAppendTable(db, "documents", items))
query <- "forest"
queryEmbedding <- embed(c(query))[[1]]
params <- list(encodeVector(queryEmbedding))
result <- dbGetQuery(db, "SELECT content FROM documents ORDER BY embedding <=> $1 LIMIT 5", params=params)
print(result$content)