Skip to content

Commit

Permalink
streaming tweaks continue
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHWade committed Mar 16, 2024
1 parent 1cb44d2 commit ce98ed0
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions R/stream-openai.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ chat_openai <- function(prompt = "Tell me a joke about R.",

class(response) <- c("chat_tibble", class(response))

Check warning on line 16 in R/stream-openai.R

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L16

Added line #L16 was not covered by tests

invisible(response)
response

Check warning on line 18 in R/stream-openai.R

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L18

Added line #L18 was not covered by tests
}

#' @export
Expand Down Expand Up @@ -105,6 +105,7 @@ req_chat <- function(prompt, model, history, temperature, stream = FALSE) {
req_error(is_error = function(resp) FALSE)

Check warning on line 105 in R/stream-openai.R

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L104-L105

Added lines #L104 - L105 were not covered by tests

if (is_true(stream)) {
env <- caller_env()
req |>
req_perform_stream(
callback = create_handler("openai"),
Expand All @@ -119,12 +120,21 @@ req_chat <- function(prompt, model, history, temperature, stream = FALSE) {

# Process API Response ----------------------------------------------------

resp_chat <- function(response) {
response |>
resp_chat <- function(response, stream) {
resp <- response |>
resp_chat_error() |>
resp_body_json(simplifyVector = TRUE) |>
pluck("choices", "message") |>
tibble::as_tibble()
resp_body_json(simplifyVector = TRUE)

Check warning on line 126 in R/stream-openai.R

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L124-L126

Added lines #L124 - L126 were not covered by tests

if (is_true(stream)) {
resp |>
pluck("delta", "message") |>
tibble::as_tibble()

Check warning on line 131 in R/stream-openai.R

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L128-L131

Added lines #L128 - L131 were not covered by tests
} else {
resp |>
pluck("choices", "message") |>
tibble::as_tibble()
resp_chat_normal(response)

Check warning on line 136 in R/stream-openai.R

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L133-L136

Added lines #L133 - L136 were not covered by tests
}
}

resp_chat_error <- function(response) {
Expand Down

0 comments on commit ce98ed0

Please sign in to comment.