Skip to content

Commit

Permalink
wip api call refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHWade committed Mar 14, 2024
1 parent d8e93a1 commit b729aa1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion R/stream-chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ create_handler <- function(service = "openai",
where = "console") {
env <- rlang::env()
env$resp <- NULL
env$full_resp <- NULL
env$full_resp <- ""

Check warning on line 53 in R/stream-chat.R

View check run for this annotation

Codecov / codecov/patch

R/stream-chat.R#L53

Added line #L53 was not covered by tests

stream_details <- get_stream_pattern(service)
new_pattern <- stream_details$pattern
Expand Down
41 changes: 23 additions & 18 deletions R/stream-openai.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@ chat_openai <- function(prompt = "Tell me a joke about R.",
model = "gpt-3.5-turbo",
history = NULL,
temperature = NULL,
stream = FALSE,
api_key = Sys.getenv("OPENAI_API_KEY"),
url = getOption("gpttools.url", "https://api.openai.com/")) {
stream = FALSE) {
response <-
req_base_openai(url) |>
req_auth_openai() |>
req_body_openai(prompt = prompt,
model = model,
history = history,
temperature = temperature,
stream = is_true(stream)) |>
req_chat(stream = is_true(stream))

response <- resp_chat(response)
req_chat(prompt = prompt,
model = model,
history = history,
temperature = temperature,
stream = is_true(stream)) |>
resp_chat()

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L6-L12

Added lines #L6 - L12 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L14

Added line #L14 was not covered by tests

Expand All @@ -39,6 +33,9 @@ print.chat_tibble <- function(x, ...) {
invisible(x)

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L33

Added line #L33 was not covered by tests
}


# Make API Request --------------------------------------------------------

req_base_openai <- function(
url = getOption("gpttools.url", "https://api.openai.com/")
) {
Expand All @@ -59,7 +56,7 @@ req_body_openai <- function(request,
if (!is_null(history)) {
prompt <- add_history(prompt, history)

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L56-L57

Added lines #L56 - L57 were not covered by tests
} else {
prompt <- list(list(role = "user", content = "prompt"))
prompt <- list(list(role = "user", content = prompt))

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L59

Added line #L59 was not covered by tests
}

body <-

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L62

Added line #L62 was not covered by tests
Expand Down Expand Up @@ -88,17 +85,22 @@ add_history <- function(prompt, history) {
)
}

req_chat <- function(request, stream = FALSE, callback = NULL) {
req_chat <- function(prompt, model, history, temperature, stream = FALSE) {
req <-
request |>
req_base_openai() |>
req_auth_openai() |>
req_body_openai(prompt = prompt,
model = model,
history = history,
temperature = temperature,
stream = is_true(stream)) |>
req_retry(max_tries = 3) |>
req_error(is_error = function(resp) FALSE)

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L89-L98

Added lines #L89 - L98 were not covered by tests

if (is_true(stream)) {
req |>
req_perform_stream(
callback = create_handler("openai"),

buffer_kb = 0.01

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L100-L104

Added lines #L100 - L104 were not covered by tests
)
} else {
Expand All @@ -107,6 +109,9 @@ req_chat <- function(request, stream = FALSE, callback = NULL) {
}
}


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

resp_chat <- function(response) {
response |>
resp_chat_error() |>
Expand All @@ -121,7 +126,7 @@ resp_chat_error <- function(response) {
description <- resp_status_desc(response)

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

cli_abort(message = c(
"x" = glue::glue("API request failed. Error {status} - {description}"),
"x" = glue::glue("OpenAI API request failed. Error {status} - {description}"),
"i" = "Visit the OpenAI API documentation for more details"

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L128-L130

Added lines #L128 - L130 were not covered by tests
))
} else {
Expand Down

0 comments on commit b729aa1

Please sign in to comment.