Skip to content

Commit

Permalink
stream returns chat_tibble class
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHWade committed Mar 14, 2024
1 parent 61f9ada commit 4084243
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions R/stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ stream <- function(text, model = "mistral-tiny", dry_run = FALSE, ..., error_cal
return(req)
}

env <- caller_env()

env$response <- character()

resp <- req_perform_stream(req,
callback = stream_callback,
round = "line",
buffer_kb = 0.01
callback = \(x) stream_callback(x, env),
round = "line",
buffer_kb = 0.01
)

invisible(resp)
tib <- tibble(role = "assistant",
content = env$response)
class(tib) <- c("chat_tibble", class(tib))

invisible(tib)
}

#' @importFrom jsonlite fromJSON
stream_callback <- function(x) {
stream_callback <- function(x, env) {
txt <- rawToChar(x)

lines <- str_split(txt, "\n")[[1]]
Expand All @@ -34,6 +42,8 @@ stream_callback <- function(x) {
chunk$choices$delta$content
})

env$response <- paste0(env$response, tokens)

cat(tokens)

TRUE
Expand Down

0 comments on commit 4084243

Please sign in to comment.