From e5837039b831f7a19a1d6afbbe18f0932991f2cf Mon Sep 17 00:00:00 2001 From: jcrodriguez1989 Date: Mon, 15 May 2023 12:22:01 -0300 Subject: [PATCH 1/8] Feature: New verbosity level added --- R/addins.R | 4 ++-- R/get_verbosity.R | 9 +++++++++ R/gpt_get_completions.R | 4 ++-- R/parse_response.R | 11 +++++++++-- man/get_verbosity.Rd | 11 +++++++++++ man/parse_response.Rd | 4 +++- 6 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 R/get_verbosity.R create mode 100644 man/get_verbosity.Rd diff --git a/R/addins.R b/R/addins.R index 4f2925f..5950eb0 100644 --- a/R/addins.R +++ b/R/addins.R @@ -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`") @@ -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) diff --git a/R/get_verbosity.R b/R/get_verbosity.R new file mode 100644 index 0000000..f6f7294 --- /dev/null +++ b/R/get_verbosity.R @@ -0,0 +1,9 @@ +#' Get Verbosity Level +#' +get_verbosity <- function() { + suppressWarnings(max( + as.logical(Sys.getenv("OPENAI_VERBOSE", TRUE)), + as.numeric(Sys.getenv("OPENAI_VERBOSE", TRUE)), + na.rm = TRUE + )) +} diff --git a/R/gpt_get_completions.R b/R/gpt_get_completions.R index 1e627f6..85e1695 100644 --- a/R/gpt_get_completions.R +++ b/R/gpt_get_completions.R @@ -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") @@ -78,7 +78,7 @@ 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), 0))) ), list(list(role = "user", content = "continue")) ) diff --git a/R/parse_response.R b/R/parse_response.R index c09890c..0d74722 100644 --- a/R/parse_response.R +++ b/R/parse_response.R @@ -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 = "") diff --git a/man/get_verbosity.Rd b/man/get_verbosity.Rd new file mode 100644 index 0000000..bd965ec --- /dev/null +++ b/man/get_verbosity.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_verbosity.R +\name{get_verbosity} +\alias{get_verbosity} +\title{Get Verbosity Level} +\usage{ +get_verbosity() +} +\description{ +Get Verbosity Level +} diff --git a/man/parse_response.Rd b/man/parse_response.Rd index 644f2be..4635f83 100644 --- a/man/parse_response.Rd +++ b/man/parse_response.Rd @@ -4,10 +4,12 @@ \alias{parse_response} \title{Parse OpenAI API Response} \usage{ -parse_response(raw_responses) +parse_response(raw_responses, verbosity = get_verbosity()) } \arguments{ \item{raw_responses}{The raw response object returned by the OpenAI API.} + +\item{verbosity}{The verbosity level for this function.} } \value{ Returns a character vector containing the text content of the response. From f276f237114735559dc49e4377137bd2da432ebe Mon Sep 17 00:00:00 2001 From: jcrodriguez1989 Date: Mon, 15 May 2023 12:24:42 -0300 Subject: [PATCH 2/8] Minor documentation changes --- R/get_verbosity.R | 1 + R/gpt_get_completions.R | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/R/get_verbosity.R b/R/get_verbosity.R index f6f7294..c25b541 100644 --- a/R/get_verbosity.R +++ b/R/get_verbosity.R @@ -1,6 +1,7 @@ #' 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)), diff --git a/R/gpt_get_completions.R b/R/gpt_get_completions.R index 85e1695..761c64d 100644 --- a/R/gpt_get_completions.R +++ b/R/gpt_get_completions.R @@ -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), 0))) + messages, + list(list(role = "assistant", content = parse_response(list(post_res), verbosity = 0))) ), list(list(role = "user", content = "continue")) ) From 31c60c70caf253f818e701d88b311595c72b8819 Mon Sep 17 00:00:00 2001 From: jcrodriguez1989 Date: Thu, 9 Nov 2023 08:42:30 +0000 Subject: [PATCH 3/8] Feature: Switch OPENAI's API URL --- R/chatgpt-package.R | 2 ++ R/gpt_get_completions.R | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/R/chatgpt-package.R b/R/chatgpt-package.R index f316fd8..5148f39 100644 --- a/R/chatgpt-package.R +++ b/R/chatgpt-package.R @@ -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") diff --git a/R/gpt_get_completions.R b/R/gpt_get_completions.R index 1e627f6..52744cc 100644 --- a/R/gpt_get_completions.R +++ b/R/gpt_get_completions.R @@ -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), From 0a82b950af6485387ef346e0a16e0ec57d6a12d8 Mon Sep 17 00:00:00 2001 From: jcrodriguez1989 Date: Thu, 9 Nov 2023 10:15:17 +0000 Subject: [PATCH 4/8] Updated README --- README.Rmd | 5 +++++ README.md | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/README.Rmd b/README.Rmd index 44114fe..7a65f03 100644 --- a/README.Rmd +++ b/README.Rmd @@ -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. diff --git a/README.md b/README.md index 79a8a50..c18c5c2 100644 --- a/README.md +++ b/README.md @@ -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. From 6cc47927a963c6a56d75eb7d9eb39a33625c6e1f Mon Sep 17 00:00:00 2001 From: jcrodriguez1989 Date: Thu, 9 Nov 2023 10:21:12 +0000 Subject: [PATCH 5/8] Release v0.2.4 --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 30a1fd6..e47f0e4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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"), From 3544fd056ae52f4f5b042ff87a73c525c1d30c9d Mon Sep 17 00:00:00 2001 From: jcrodriguez1989 Date: Thu, 9 Nov 2023 10:25:35 +0000 Subject: [PATCH 6/8] Updated GH action --- .github/workflows/code-review-gpt.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/code-review-gpt.yaml b/.github/workflows/code-review-gpt.yaml index b7e17e7..cff1a3c 100644 --- a/.github/workflows/code-review-gpt.yaml +++ b/.github/workflows/code-review-gpt.yaml @@ -33,7 +33,7 @@ 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) + diff <- system(paste("git diff", GITHUB_HEAD_REF, GITHUB_BASE_REF), intern = TRUE) prompt <- paste( "Between sextuple quotes is a GitHub PR.", "Please perform a code review on it.", From 5917b02ce704db3ee21c2086dfe590cfdab86432 Mon Sep 17 00:00:00 2001 From: jcrodriguez1989 Date: Thu, 9 Nov 2023 10:30:27 +0000 Subject: [PATCH 7/8] Updated GH action --- .github/workflows/code-review-gpt.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/code-review-gpt.yaml b/.github/workflows/code-review-gpt.yaml index cff1a3c..68d624b 100644 --- a/.github/workflows/code-review-gpt.yaml +++ b/.github/workflows/code-review-gpt.yaml @@ -33,9 +33,10 @@ 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_HEAD_REF, GITHUB_BASE_REF), intern = TRUE) + git_diff_cmnd <- paste("git diff", GITHUB_BASE_REF, GITHUB_HEAD_REF) + diff <- system(git_diff_cmnd, intern = TRUE) prompt <- paste( - "Between sextuple quotes is a GitHub PR.", + "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.", From 96df4f8c557deb9fc98ee560c1c1fe44e92a50d8 Mon Sep 17 00:00:00 2001 From: jcrodriguez1989 Date: Thu, 9 Nov 2023 10:35:55 +0000 Subject: [PATCH 8/8] Updated GH action --- .github/workflows/code-review-gpt.yaml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/code-review-gpt.yaml b/.github/workflows/code-review-gpt.yaml index 68d624b..435bcab 100644 --- a/.github/workflows/code-review-gpt.yaml +++ b/.github/workflows/code-review-gpt.yaml @@ -33,10 +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") - git_diff_cmnd <- paste("git diff", GITHUB_BASE_REF, GITHUB_HEAD_REF) + 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 created with `", git_diff_cmnd, "` command.", + 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.",