Skip to content

Commit

Permalink
Feature: API metadata as attr
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrodriguez1989 committed Aug 2, 2024
1 parent d22a39a commit 3e457b4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ URL: https://github.com/jcrodriguez1989/chatgpt
BugReports: https://github.com/jcrodriguez1989/chatgpt/issues
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Imports:
clipr,
httr,
Expand Down
3 changes: 2 additions & 1 deletion R/gpt_get_completions.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ gpt_get_completions <- function(prompt, openai_api_key = Sys.getenv("OPENAI_API_
temperature = as.numeric(Sys.getenv("OPENAI_TEMPERATURE", 1)),
top_p = as.numeric(Sys.getenv("OPENAI_TOP_P", 1)),
frequency_penalty = as.numeric(Sys.getenv("OPENAI_FREQUENCY_PENALTY", 0)),
presence_penalty = as.numeric(Sys.getenv("OPENAI_PRESENCE_PENALTY", 0))
presence_penalty = as.numeric(Sys.getenv("OPENAI_PRESENCE_PENALTY", 0)),
logprobs = as.logical(Sys.getenv("OPENAI_LOGPROBS", FALSE))
)
if (get_verbosity()) {
message(paste0("\n*** ChatGPT input:\n\n", prompt, "\n"))
Expand Down
13 changes: 9 additions & 4 deletions R/parse_response.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
#' @return Returns a character vector containing the text content of the response.
#'
parse_response <- function(raw_responses, verbosity = get_verbosity()) {
# Parse the message content of the list of raw_responses. Trim those messages, and paste them.
parsed_response <- paste(trimws(sapply(raw_responses, function(response) {
sapply(response$choices, function(x) x$message$content)
})), collapse = "")
# If we provide a numeric value to `OPENAI_VERBOSE`, and it is `> 1` print return verbosity.
if (verbosity > 1) {
lapply(raw_responses, function(response) message(toJSON(response, pretty = TRUE)))
if (verbosity > 2) {
# If we are in 3-verbose mode, add the raw_responses as an attribute to the return object.
attr(parsed_response, "raw_responses") <- raw_responses
}
}
# Parse the message content of the list of raw_responses. Trim those messages, and paste them.
paste(trimws(sapply(raw_responses, function(response) {
sapply(response$choices, function(x) x$message$content)
})), collapse = "")
parsed_response
}
1 change: 1 addition & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,4 @@ The following environment variables can be set to tweak the behavior, as documen
* `OPENAI_TOP_P`; defaults to `1`
* `OPENAI_FREQUENCY_PENALTY`; defaults to `0`
* `OPENAI_PRESENCE_PENALTY`; defaults to `0`
* `OPENAI_LOGPROBS`; defaults to `FALSE`
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,4 @@ documented in
- `OPENAI_TOP_P`; defaults to `1`
- `OPENAI_FREQUENCY_PENALTY`; defaults to `0`
- `OPENAI_PRESENCE_PENALTY`; defaults to `0`
- `OPENAI_LOGPROBS`; defaults to `FALSE`

0 comments on commit 3e457b4

Please sign in to comment.