-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.R
More file actions
41 lines (34 loc) · 1.33 KB
/
example.R
File metadata and controls
41 lines (34 loc) · 1.33 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
41
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 bit(1536))"))
toBits <- function(ubinary) {
paste0(sapply(ubinary, function(v) { rev(as.integer(intToBits(v)[1:8])) }), collapse="")
}
embed <- function(texts, inputType) {
url <- "https://api.cohere.com/v2/embed"
token <- Sys.getenv("CO_API_KEY")
data <- list(
texts=texts,
model="embed-v4.0",
input_type=inputType,
embedding_types=list("ubinary")
)
resp <- request(url) |> req_auth_bearer_token(token) |> req_body_json(data) |> req_perform()
sapply((resp |> resp_body_json())$embeddings$ubinary, function(v) { toBits(v) })
}
input <- c(
"The dog is barking",
"The cat is purring",
"The bear is growling"
)
embeddings <- embed(input, "search_document")
items <- data.frame(content=input, embedding=embeddings)
invisible(dbAppendTable(db, "documents", items))
query <- "forest"
queryEmbedding <- embed(list(query), "search_query")[[1]]
params <- list(queryEmbedding)
result <- dbGetQuery(db, "SELECT content FROM documents ORDER BY embedding <~> $1 LIMIT 5", params=params)
print(result$content)