Skip to content

Commit 6ccef69

Browse files
committed
accept pre-computed embedding in store_update()
1 parent bf7a1a5 commit 6ccef69

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

R/store-v2.R

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ ragnar_store_build_index_v2 <- function(store, type = c("vss", "fts")) {
251251
r"--(
252252
PRAGMA create_fts_index(
253253
'chunks', -- input_table
254-
'chunk_id', -- input_id
255-
'context', 'text', -- *input_values
254+
'chunk_id', -- input_id
255+
'context', 'text', -- *input_values
256256
overwrite = 1
257257
);
258258
)--"
@@ -277,9 +277,15 @@ ragnar_store_update_v2 <- function(store, chunks) {
277277
S7_inherits(chunks, MarkdownDocumentChunks)
278278
)
279279

280-
# Chunks are not refering to any document, or the document doesn't have an origin.
281-
if (is.null(chunks@document) || is.na(chunks@document@origin) || is.null(chunks@document@origin)) {
282-
stop("Can't update chunks when the document does not specify a valid origin, use ragnar_store_insert instead.")
280+
# Chunks are not referring to any document, or the document doesn't have an origin.
281+
if (
282+
is.null(chunks@document) ||
283+
is.na(chunks@document@origin) ||
284+
is.null(chunks@document@origin)
285+
) {
286+
stop(
287+
"Can't update chunks when the document does not specify a valid origin, use `ragnar_store_insert()` instead."
288+
)
283289
}
284290

285291
if ("text" %in% names(chunks)) {
@@ -317,11 +323,12 @@ ragnar_store_update_v2 <- function(store, chunks) {
317323
text = as.character(chunks@document)
318324
)
319325

326+
if (!is.null(store@embed) && !"embedding" %in% names(chunks)) {
327+
chunks$embedding <-
328+
store@embed(with(chunks, stri_c(context %|% "", "\n", text)))
329+
}
330+
320331
embeddings <- chunks |>
321-
mutate(
322-
embedding = store@embed(stri_c(context, "\n", text)),
323-
text = NULL
324-
) |>
325332
select(any_of(dbListFields(con, "embeddings"))) |>
326333
vctrs::vec_cast(store@schema)
327334

@@ -383,9 +390,9 @@ ragnar_store_insert_v2 <- function(store, chunks, replace_existing = FALSE) {
383390
} else {
384391
chunks$text <- stri_sub(chunks@document, chunks$start, chunks$end)
385392
}
386-
387393
if (!is.null(store@embed) && !"embedding" %in% names(chunks)) {
388-
chunks$embedding <- store@embed(with(chunks, stri_c(context, "\n", text)))
394+
chunks$embedding <-
395+
store@embed(with(chunks, stri_c(context %|% "", "\n", text)))
389396
}
390397

391398
con <- store@con

0 commit comments

Comments
 (0)