Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v0.2.4 #50

Merged
merged 10 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/code-review-gpt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ jobs:
GITHUB_BASE_REF <- paste0("origin/", Sys.getenv("GITHUB_BASE_REF"))
GITHUB_HEAD_REF <- paste0("origin/", Sys.getenv("GITHUB_HEAD_REF"))
system("git fetch origin")
diff <- system(paste("git diff", GITHUB_BASE_REF, GITHUB_HEAD_REF), intern = TRUE)
git_diff_cmnd <- paste("git diff", GITHUB_HEAD_REF, GITHUB_BASE_REF)
diff <- system(git_diff_cmnd, intern = TRUE)
prompt <- paste(
"Between sextuple quotes is a GitHub PR.",
paste0(
"Between sextuple quotes is a GitHub PR created with `", git_diff_cmnd, "` command."
),
"Please perform a code review on it.",
"Only notify if you find any issues.",
"If possible, specify on which files and lines you find the issues.",
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: chatgpt
Type: Package
Title: Interface to 'ChatGPT' from R
Version: 0.2.3
Version: 0.2.4
Authors@R: c(
person(
given = "Juan Cruz", family = "Rodriguez", role = c("aut", "cre"),
Expand Down
4 changes: 2 additions & 2 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ run_addin <- function(addin_name) {
doc_range <- as.document_range(c(c(0, 0), c(Inf, Inf)))
}
modifyRange(doc_range, out, doc_context$id)
} else if (as.logical(Sys.getenv("OPENAI_VERBOSE", TRUE))) {
} else if (get_verbosity()) {
message(paste0("\n*** ChatGPT output:\n\n", out, "\n"))
} else {
warning("Please set one of `OPENAI_ADDIN_REPLACE=TRUE` or `OPENAI_VERBOSE=TRUE`")
Expand Down Expand Up @@ -74,7 +74,7 @@ run_addin_ask_chatgpt <- function() {
server <- function(input, output, session) {
observeEvent(input$ask_button, {
chatgpt_reply <- ask_chatgpt(input$question)
if (as.logical(Sys.getenv("OPENAI_VERBOSE", TRUE))) {
if (get_verbosity()) {
message(paste0("\n*** ChatGPT output:\n\n", chatgpt_reply, "\n"))
}
updateTextAreaInput(session, "answer", value = chatgpt_reply)
Expand Down
2 changes: 2 additions & 0 deletions R/chatgpt-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ assign(
list(list(role = "system", content = "You are a helpful assistant.")),
envir = .state
)

api_url <- Sys.getenv("OPENAI_API_URL", "https://api.openai.com/v1")
10 changes: 10 additions & 0 deletions R/get_verbosity.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#' Get Verbosity Level
#'
get_verbosity <- function() {
# `OPENAI_VERBOSE` should be one of `numeric` or `FALSE`/`TRUE`. But we'll return it as numeric.
suppressWarnings(max(
as.logical(Sys.getenv("OPENAI_VERBOSE", TRUE)),
as.numeric(Sys.getenv("OPENAI_VERBOSE", TRUE)),
na.rm = TRUE
))
}
7 changes: 4 additions & 3 deletions R/gpt_get_completions.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ gpt_get_completions <- function(prompt, openai_api_key = Sys.getenv("OPENAI_API_
frequency_penalty = as.numeric(Sys.getenv("OPENAI_FREQUENCY_PENALTY", 0)),
presence_penalty = as.numeric(Sys.getenv("OPENAI_PRESENCE_PENALTY", 0))
)
if (as.logical(Sys.getenv("OPENAI_VERBOSE", TRUE))) {
if (get_verbosity()) {
message(paste0("\n*** ChatGPT input:\n\n", prompt, "\n"))
}
return_language <- Sys.getenv("OPENAI_RETURN_LANGUAGE")
Expand Down Expand Up @@ -62,7 +62,7 @@ gpt_get_completions <- function(prompt, openai_api_key = Sys.getenv("OPENAI_API_
keep_querying <- TRUE
while (keep_querying) {
post_res <- POST(
"https://api.openai.com/v1/chat/completions",
paste0(api_url, "/chat/completions"),
add_headers("Authorization" = paste("Bearer", openai_api_key)),
content_type_json(),
body = toJSON(c(params, list(messages = messages)), auto_unbox = TRUE),
Expand All @@ -78,7 +78,8 @@ gpt_get_completions <- function(prompt, openai_api_key = Sys.getenv("OPENAI_API_
# And update the messages sent to ChatGPT, in order to continue the current session.
messages <- append(
append(
messages, list(list(role = "assistant", content = parse_response(list(post_res))))
messages,
list(list(role = "assistant", content = parse_response(list(post_res), verbosity = 0)))
),
list(list(role = "user", content = "continue"))
)
Expand Down
11 changes: 9 additions & 2 deletions R/parse_response.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
#' Takes the raw response from the OpenAI API and extracts the text content from it.
#'
#' @param raw_responses The raw response object returned by the OpenAI API.
#' @param verbosity The verbosity level for this function.
#'
#' @importFrom jsonlite toJSON
#'
#' @return Returns a character vector containing the text content of the response.
#'
parse_response <- function(raw_responses) {
# Parse the message content of the list of raw_responses. Trim those message, and paste them.
parse_response <- function(raw_responses, verbosity = get_verbosity()) {
# 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)))
}
# 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 = "")
Expand Down
5 changes: 5 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ cat(chatgpt::explain_code("for (i in 1:10) {\n print(i ** 2)\n}"))
In order to run ChatGPT queries behind a proxy, set the `OPENAI_PROXY` environment variable with a valid `IP:PORT` proxy.
E.g., `Sys.setenv("OPENAI_PROXY" = "81.94.255.13:8080")`.

### Switch OPENAI's API URL

To replace the default OPENAI's API URL (`"https://api.openai.com/v1"`), you can set the `OPENAI_API_URL` environment variable with the URL you need to use.
E.g., `Sys.setenv("OPENAI_API_URL" = "https://api.chatanywhere.com.cn")`.

### ChatGPT Model Tweaks

ChatGPT model parameters can be tweaked by using environment variables.
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@ In order to run ChatGPT queries behind a proxy, set the `OPENAI_PROXY`
environment variable with a valid `IP:PORT` proxy. E.g.,
`Sys.setenv("OPENAI_PROXY" = "81.94.255.13:8080")`.

### Switch OPENAI's API URL

To replace the default OPENAI's API URL (`"https://api.openai.com/v1"`), you can set the `OPENAI_API_URL` environment variable with the URL you need to use.
E.g., `Sys.setenv("OPENAI_API_URL" = "https://api.chatanywhere.com.cn")`.

### ChatGPT Model Tweaks

ChatGPT model parameters can be tweaked by using environment variables.
Expand Down
11 changes: 11 additions & 0 deletions man/get_verbosity.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/parse_response.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.