Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion R/chat-structured.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,27 @@ convert_from_type <- function(x, type) {
# don't convert to data frame, but put known properties first
lapply(x, \(y) y[union(names(type@items@properties), names(y))])
} else {
# Convert each declared property across all items
cols <- lapply(names(type@items@properties), function(name) {
vals <- lapply(x, function(y) y[[name]])
convert_from_type(vals, type_array(type@items@properties[[name]]))
})
names(cols) <- names(type@items@properties)
list2DF(cols)

# Flatten any nested data frames by prefixing with parent name
flat_cols <- list()
for (nm in names(cols)) {
col <- cols[[nm]]
if (is.data.frame(col)) {
for (sn in names(col)) {
flat_cols[[paste0(nm, ".", sn)]] <- col[[sn]]
}
} else {
flat_cols[[nm]] <- col
}
}

list2DF(flat_cols)
}
} else {
x
Expand Down
7 changes: 6 additions & 1 deletion R/parallel-chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ multi_convert <- function(
}

if (is.data.frame(out) && (include_tokens || include_cost)) {
tokens <- t(vapply(turns, \(turn) turn@tokens, integer(3)))
# Coerce to integer to handle providers that return doubles
tokens <- t(vapply(
turns,
function(turn) as.integer(turn@tokens),
integer(3)
))

if (include_tokens) {
out$input_tokens <- tokens[, 1]
Expand Down
Loading
Loading