Skip to content

Commit

Permalink
Merge pull request #16 from jcrodriguez1989/develop
Browse files Browse the repository at this point in the history
Release v0.2.2
  • Loading branch information
jcrodriguez1989 authored Mar 22, 2023
2 parents 215303b + aefff06 commit 1c025f9
Show file tree
Hide file tree
Showing 25 changed files with 255 additions and 124 deletions.
3 changes: 2 additions & 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.1
Version: 0.2.2
Authors@R: c(
person(
given = "Juan Cruz", family = "Rodriguez", role = c("aut", "cre"),
Expand All @@ -18,6 +18,7 @@ Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
Imports:
clipr,
httr,
jsonlite,
miniUI,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

export(ask_chatgpt)
export(comment_code)
export(complete_code)
export(create_unit_tests)
export(create_variable_name)
export(document_code)
export(explain_code)
export(find_issues_in_code)
export(optimize_code)
export(refactor_code)
importFrom(clipr,read_clip)
importFrom(httr,POST)
importFrom(httr,add_headers)
importFrom(httr,content)
Expand Down
2 changes: 2 additions & 0 deletions R/addins.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ run_addin <- function(addin_name) {
# Select which addin has to be used.
addin_function <- switch(addin_name,
"comment_code" = comment_code,
"complete_code" = complete_code,
"create_unit_tests" = create_unit_tests,
"create_variable_name" = create_variable_name,
"document_code" = document_code,
Expand Down Expand Up @@ -43,6 +44,7 @@ run_addin <- function(addin_name) {
}

run_addin_comment_code <- function() run_addin("comment_code")
run_addin_complete_code <- function() run_addin("complete_code")
run_addin_create_unit_tests <- function() run_addin("create_unit_tests")
run_addin_create_variable_name <- function() run_addin("create_variable_name")
run_addin_document_code <- function() run_addin("document_code")
Expand Down
8 changes: 6 additions & 2 deletions R/comment_code.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#' ChatGPT: Comment Code
#'
#' @param code The code to be commented by ChatGPT.
#' @param code The code to be commented by ChatGPT. If not provided, it will use what's copied on
#' the clipboard.
#'
#' @examples
#' \dontrun{
#' cat(comment_code("for (i in 1:10) {\n print(i ** 2)\n}"))
#' }
#'
#' @importFrom clipr read_clip
#'
#' @return A character value with the response generated by ChatGPT.
#'
#' @export
#'
comment_code <- function(code) {
comment_code <- function(code = clipr::read_clip(allow_non_interactive = TRUE)) {
code <- paste(gsub('"', "'", code), collapse = "\n")
prompt <- paste0('Add inline comments to the following R code: "', code, '"')
parse_response(gpt_get_completions(prompt))
}
21 changes: 21 additions & 0 deletions R/complete_code.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#' ChatGPT: Complete Code
#'
#' @param code The code to be completed by ChatGPT. If not provided, it will use what's copied on
#' the clipboard.
#'
#' @examples
#' \dontrun{
#' cat(complete_code("# A function to square each element of a vector\nsquare_each <- function("))
#' }
#'
#' @importFrom clipr read_clip
#'
#' @return A character value with the response generated by ChatGPT.
#'
#' @export
#'
complete_code <- function(code = clipr::read_clip(allow_non_interactive = TRUE)) {
code <- paste(gsub('"', "'", code), collapse = "\n")
prompt <- paste0('Complete the following R code: "', code, '"')
parse_response(gpt_get_completions(prompt))
}
11 changes: 8 additions & 3 deletions R/create_unit_tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
#'
#' Create `{testthat}` test cases for the code.
#'
#' @param code The code for which to create unit tests by ChatGPT.
#' @param code The code for which to create unit tests by ChatGPT. If not provided, it will use
#' what's copied on the clipboard.
#'
#' @examples
#' \dontrun{
#' cat(create_unit_tests("squared_numbers <- function(numbers) {\n numbers ^ 2\n}"))
#' }
#'
#' @importFrom clipr read_clip
#'
#' @return A character value with the response generated by ChatGPT.
#'
#' @export
#'
create_unit_tests <- function(code) {
create_unit_tests <- function(code = clipr::read_clip(allow_non_interactive = TRUE)) {
code <- paste(gsub('"', "'", code), collapse = "\n")
prompt <- paste0(
'Create a full testthat file, with test cases for the following R code: "', code, '"'
'Using testthat 3e, version over 3.0.0, create a full testthat file, with test cases for the ',
'following R code: "', code, '"'
)
parse_response(gpt_get_completions(prompt))
}
8 changes: 6 additions & 2 deletions R/create_variable_name.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#' ChatGPT: Create Variable Name
#'
#' @param code The code for which to give a variable name to its result.
#' @param code The code for which to give a variable name to its result. If not provided, it will
#' use what's copied on the clipboard.
#'
#' @examples
#' \dontrun{
#' cat(create_variable_name("sapply(1:10, function(i) i ** 2)"))
#' }
#'
#' @importFrom clipr read_clip
#'
#' @return A character value with the response generated by ChatGPT.
#'
#' @export
#'
create_variable_name <- function(code) {
create_variable_name <- function(code = clipr::read_clip(allow_non_interactive = TRUE)) {
code <- paste(gsub('"', "'", code), collapse = "\n")
prompt <- paste0('Give a good variable name to the result of the following R code: "', code, '"')
parse_response(gpt_get_completions(prompt))
}
8 changes: 6 additions & 2 deletions R/document_code.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#' ChatGPT: Document Code (in roxygen2 format)
#'
#' @param code The code to be documented by ChatGPT.
#' @param code The code to be documented by ChatGPT. If not provided, it will use what's copied on
#' the clipboard.
#'
#' @examples
#' \dontrun{
#' cat(document_code("square_numbers <- function(numbers) numbers ** 2"))
#' }
#'
#' @importFrom clipr read_clip
#'
#' @return A character value with the response generated by ChatGPT.
#'
#' @export
#'
document_code <- function(code) {
document_code <- function(code = clipr::read_clip(allow_non_interactive = TRUE)) {
code <- paste(gsub('"', "'", code), collapse = "\n")
prompt <- paste0('Document, in roxygen2 format, this R function: "', code, '"')
parse_response(gpt_get_completions(prompt))
}
8 changes: 6 additions & 2 deletions R/explain_code.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#' ChatGPT: Explain Code
#'
#' @param code The code to be explained by ChatGPT.
#' @param code The code to be explained by ChatGPT. If not provided, it will use what's copied on
#' the clipboard.
#'
#' @examples
#' \dontrun{
#' cat(explain_code("for (i in 1:10) {\n print(i ** 2)\n}"))
#' }
#'
#' @importFrom clipr read_clip
#'
#' @return A character value with the response generated by ChatGPT.
#'
#' @export
#'
explain_code <- function(code) {
explain_code <- function(code = clipr::read_clip(allow_non_interactive = TRUE)) {
code <- paste(gsub('"', "'", code), collapse = "\n")
prompt <- paste0('Explain the following R code: "', code, '"')
parse_response(gpt_get_completions(prompt))
}
8 changes: 6 additions & 2 deletions R/find_issues_in_code.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#' ChatGPT: Find Issues in Code
#'
#' @param code The code to be analyzed by ChatGPT.
#' @param code The code to be analyzed by ChatGPT. If not provided, it will use what's copied on
#' the clipboard.
#'
#' @examples
#' \dontrun{
#' cat(find_issues_in_code("i <- 0\nwhile (i < 0) {\n i <- i - 1\n}"))
#' }
#'
#' @importFrom clipr read_clip
#'
#' @return A character value with the response generated by ChatGPT.
#'
#' @export
#'
find_issues_in_code <- function(code) {
find_issues_in_code <- function(code = clipr::read_clip(allow_non_interactive = TRUE)) {
code <- paste(gsub('"', "'", code), collapse = "\n")
prompt <- paste0('Find issues or bugs in the following R code: "', code, '"')
parse_response(gpt_get_completions(prompt))
}
12 changes: 8 additions & 4 deletions R/gpt_get_completions.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,22 @@ gpt_get_completions <- function(prompt, openai_api_key = Sys.getenv("OPENAI_API_
),
list(role = "user", content = prompt)
)
content(POST(
post_res <- POST(
"https://api.openai.com/v1/chat/completions",
add_headers("Authorization" = paste("Bearer", openai_api_key)),
content_type_json(),
body = toJSON(c(params, list(messages = messages)), auto_unbox = TRUE)
))
)
} else {
content(POST(
post_res <- POST(
"https://api.openai.com/v1/completions",
add_headers("Authorization" = paste("Bearer", openai_api_key)),
content_type_json(),
body = toJSON(c(params, list(prompt = prompt)), auto_unbox = TRUE)
))
)
}
if (!post_res$status_code %in% 200:299) {
stop(content(post_res))
}
content(post_res)
}
8 changes: 6 additions & 2 deletions R/optimize_code.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#' ChatGPT: Optimize Code
#'
#' @param code The code to be optimized by ChatGPT.
#' @param code The code to be optimized by ChatGPT. If not provided, it will use what's copied on
#' the clipboard.
#'
#' @examples
#' \dontrun{
#' cat(optimize_code("i <- 10\nwhile (i > 0) {\n i <- i - 1\n print(i)\n}"))
#' }
#'
#' @importFrom clipr read_clip
#'
#' @return A character value with the response generated by ChatGPT.
#'
#' @export
#'
optimize_code <- function(code) {
optimize_code <- function(code = clipr::read_clip(allow_non_interactive = TRUE)) {
code <- paste(gsub('"', "'", code), collapse = "\n")
prompt <- paste0('Optimize the following R code: "', code, '"')
parse_response(gpt_get_completions(prompt))
}
8 changes: 6 additions & 2 deletions R/refactor_code.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
#' ChatGPT: Refactor Code
#'
#' @param code The code to be refactored by ChatGPT.
#' @param code The code to be refactored by ChatGPT. If not provided, it will use what's copied on
#' the clipboard.
#'
#' @examples
#' \dontrun{
#' cat(refactor_code("i <- 10\nwhile (i > 0) {\n i <- i - 1\n print(i)\n}"))
#' }
#'
#' @importFrom clipr read_clip
#'
#' @return A character value with the response generated by ChatGPT.
#'
#' @export
#'
refactor_code <- function(code) {
refactor_code <- function(code = clipr::read_clip(allow_non_interactive = TRUE)) {
code <- paste(gsub('"', "'", code), collapse = "\n")
prompt <- paste0('Refactor the following R code, returning valid R code: "', code, '"')
parse_response(gpt_get_completions(prompt))
}
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ examples_usage <- sapply(exported_functions, function(exported_function) {
function_example <- function_example[grepl("^##D ", function_example)]
function_example <- gsub("^##D ", "", function_example)
example_result <- capture.output(eval(parse(text = function_example)))
example_result <- paste0(example_result, collapse = "\n")
example_result <- gsub("`|'", "", paste0(example_result, collapse = "\n"))
paste0(
paste0("#### `", exported_function, "`\n\n``` r\n"),
paste0("> ", function_example, "\n"),
Expand Down
Loading

0 comments on commit 1c025f9

Please sign in to comment.