Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Mar 14, 2024
1 parent b729aa1 commit 1cb44d2
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 52 deletions.
46 changes: 23 additions & 23 deletions R/class-message.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
new_gpttools_message <- function(role = character(),
content = character(),
datetime = character(),
model = character(),
service = character(),
temperature = double()) {
content = character(),
datetime = character(),
model = character(),
service = character(),
temperature = double()) {
if (!is_character(role)) cli_abort("`role` must be a character vector.")
if (!is_character(content)) cli_abort("`content` must be a character vector.")
if (!is_character(datetime, n = 1)) {
Expand All @@ -22,9 +22,9 @@ new_gpttools_message <- function(role = character(),
new_rcrd(
fields =
tibble::tibble(
role = role,
content = content,
),
role = role,
content = content,

Check warning on line 26 in R/class-message.R

View check run for this annotation

Codecov / codecov/patch

R/class-message.R#L22-L26

Added lines #L22 - L26 were not covered by tests
),
datetime = datetime,
model = model,
service = service,
Expand All @@ -34,16 +34,16 @@ new_gpttools_message <- function(role = character(),
}

gpttools_message <- function(role = character(),
content = character(),
datetime = character(),
model = character(),
service = character(),
temperature = double()) {
role <- vec_cast(role, character())
content <- vec_cast(content, character())
datetime <- vec_cast(datetime, character())
model <- vec_cast(model, character())
service <- vec_cast(service, character())
content = character(),
datetime = character(),
model = character(),
service = character(),
temperature = double()) {
role <- vec_cast(role, character())
content <- vec_cast(content, character())
datetime <- vec_cast(datetime, character())
model <- vec_cast(model, character())
service <- vec_cast(service, character())
temperature <- vec_cast(temperature, double())

Check warning on line 47 in R/class-message.R

View check run for this annotation

Codecov / codecov/patch

R/class-message.R#L42-L47

Added lines #L42 - L47 were not covered by tests

new_gpttools_message(role, content, datetime, model, service, temperature)

Check warning on line 49 in R/class-message.R

View check run for this annotation

Codecov / codecov/patch

R/class-message.R#L49

Added line #L49 was not covered by tests
Expand All @@ -53,11 +53,11 @@ gpttools_message <- function(role = character(),
print.gpttools_message <- function(x, ...) {
x_valid <- which(!is.na(x))

Check warning on line 54 in R/class-message.R

View check run for this annotation

Codecov / codecov/patch

R/class-message.R#L54

Added line #L54 was not covered by tests

role <- field(x, "role")[x_valid]
content <- field(x, "content")[x_valid]
datetime <- attr(x, "datetime")
model <- attr(x, "model")
service <- attr(x, "service")
role <- field(x, "role")[x_valid]
content <- field(x, "content")[x_valid]
datetime <- attr(x, "datetime")
model <- attr(x, "model")
service <- attr(x, "service")
temperature <- attr(x, "temperature")

Check warning on line 61 in R/class-message.R

View check run for this annotation

Codecov / codecov/patch

R/class-message.R#L56-L61

Added lines #L56 - L61 were not covered by tests

n <- length(field(x, "role"))
Expand Down
4 changes: 3 additions & 1 deletion R/harvest-docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ check_url <- function(url) {
TRUE
)

if (inherits(status, "try-error")) return(invisible(FALSE))
if (inherits(status, "try-error")) {
return(invisible(FALSE))

Check warning on line 32 in R/harvest-docs.R

View check run for this annotation

Codecov / codecov/patch

R/harvest-docs.R#L31-L32

Added lines #L31 - L32 were not covered by tests
}
status
}

Expand Down
1 change: 0 additions & 1 deletion R/stream-chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,3 @@ get_stream_pattern <- function(service) {
)
list(pattern = pattern, pluck = pluck)
}

8 changes: 5 additions & 3 deletions R/stream-cohere.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ stream_chat_cohere <- function(prompt,
req_body_json(data = request_body) |>
req_retry(max_tries = 3) |>
req_error(is_error = function(resp) FALSE) |>
req_perform_stream(callback = element_callback,
buffer_kb = 0.01,
round = "line")
req_perform_stream(
callback = element_callback,
buffer_kb = 0.01,
round = "line"

Check warning on line 25 in R/stream-cohere.R

View check run for this annotation

Codecov / codecov/patch

R/stream-cohere.R#L18-L25

Added lines #L18 - L25 were not covered by tests
)

if (resp_is_error(response)) {
status <- resp_status(response)
Expand Down
8 changes: 5 additions & 3 deletions R/stream-ollama.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ stream_chat_ollama <- function(prompt,
req_url_path_append("api") |>
req_url_path_append("generate") |>
req_body_json(data = body) |>
req_perform_stream(callback = element_callback,
buffer_kb = 0.01,
round = "line")
req_perform_stream(
callback = element_callback,
buffer_kb = 0.01,
round = "line"

Check warning on line 21 in R/stream-ollama.R

View check run for this annotation

Codecov / codecov/patch

R/stream-ollama.R#L13-L21

Added lines #L13 - L21 were not covered by tests
)

if (resp_is_error(response)) {
status <- resp_status(response)
Expand Down
43 changes: 25 additions & 18 deletions R/stream-openai.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ chat_openai <- function(prompt = "Tell me a joke about R.",
temperature = NULL,
stream = FALSE) {
response <-
req_chat(prompt = prompt,
model = model,
history = history,
temperature = temperature,
stream = is_true(stream)) |>
req_chat(
prompt = prompt,
model = model,
history = history,
temperature = temperature,
stream = is_true(stream)

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
) |>
resp_chat()

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

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
Expand All @@ -24,9 +26,10 @@ print.chat_tibble <- function(x, ...) {
print_role <- rule(stringr::str_to_title(x$role[i]))
print_role <-
switch(x$role[i],
"assistant" = col_green(print_role),
"system" = col_silver(print_role),
"user" = col_blue(print_role))
"assistant" = col_green(print_role),
"system" = col_silver(print_role),
"user" = col_blue(print_role)

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L25-L31

Added lines #L25 - L31 were not covered by tests
)
writeLines(print_role)
writeLines(x$content[i])

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L33-L34

Added lines #L33 - L34 were not covered by tests
}
Expand All @@ -37,8 +40,7 @@ print.chat_tibble <- function(x, ...) {
# Make API Request --------------------------------------------------------

req_base_openai <- function(
url = getOption("gpttools.url", "https://api.openai.com/")
) {
url = getOption("gpttools.url", "https://api.openai.com/")) {
request(url) |>
req_url_path_append("v1", "chat", "completions")

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L44-L45

Added lines #L44 - L45 were not covered by tests
}
Expand Down Expand Up @@ -79,21 +81,26 @@ add_history <- function(prompt, history) {
content = history$content

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L77-L81

Added lines #L77 - L81 were not covered by tests
)
})
list(history,
list(role = "user",
content = prompt)
list(
history,

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L84-L85

Added lines #L84 - L85 were not covered by tests
list(
role = "user",
content = prompt
)
)
}

req_chat <- function(prompt, model, history, temperature, stream = FALSE) {
req <-
req_base_openai() |>
req_auth_openai() |>
req_body_openai(prompt = prompt,
model = model,
history = history,
temperature = temperature,
stream = is_true(stream)) |>
req_body_openai(
prompt = prompt,
model = model,
history = history,
temperature = temperature,
stream = is_true(stream)

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

View check run for this annotation

Codecov / codecov/patch

R/stream-openai.R#L94-L102

Added lines #L94 - L102 were not covered by tests
) |>
req_retry(max_tries = 3) |>
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

Expand Down
8 changes: 5 additions & 3 deletions R/stream-perplexity.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ stream_chat_perplexity <- function(prompt,
) |>
req_body_json(data = request_body) |>
req_retry(max_tries = 3) |>
req_perform_stream(callback = element_callback,
buffer_kb = 0.01,
round = "line")
req_perform_stream(
callback = element_callback,
buffer_kb = 0.01,
round = "line"

Check warning on line 26 in R/stream-perplexity.R

View check run for this annotation

Codecov / codecov/patch

R/stream-perplexity.R#L21-L26

Added lines #L21 - L26 were not covered by tests
)

if (resp_is_error(response)) {
status <- resp_status(response)
Expand Down

0 comments on commit 1cb44d2

Please sign in to comment.