-
Notifications
You must be signed in to change notification settings - Fork 1
/
proofreadEnglishText.R
93 lines (80 loc) · 3.84 KB
/
proofreadEnglishText.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#' Proofread English Text During R Package Development via RStudio API
#'
#' This function provides a feature to proofread English text during the development of an R package.
#' It can either take the selected text from the RStudio editor or read from the clipboard, executes the proofreading, and returns the result to the user's clipboard or replaces the selected text.
#' The user can then paste and check the result if read from the clipboard. The function adheres to R package policies and carefully proofreads the English text.
#' Execution with GPT-4 is recommended.
#'
#' @title Proofread English Text
#' @description A function to proofread English text or text in different languages during R package development.
#' It translates the input into English if necessary and returns meticulously checked English text.
#' @param Model A string specifying the model to be used for proofreading, defaulting to "gpt-4-0314".
#' Currently, "gpt-4", "gpt-4-0314" and "gpt-4-0613" can be selected as gpt-4 models.
#' Execution with GPT-4 is recommended.
#' @param SelectedCode A logical value indicating whether to read the selected text from the RStudio editor (TRUE) or from the clipboard (FALSE). Defaults to TRUE.
#' @param verbose Logical flag to print the progress. Default is TRUE.
#' @importFrom assertthat assert_that is.string noNA
#' @importFrom clipr read_clip write_clip
#' @importFrom rstudioapi isAvailable getActiveDocumentContext insertText
#' @return The proofread text, which is also written to the clipboard if SelectedCode is FALSE, or replaces the selected text if SelectedCode is TRUE.
#' @export proofreadEnglishText
#' @author Satoshi Kume
#' @examples
#' \dontrun{
#' # Proofreading selected text in RStudio
#' proofreadEnglishText(Model = "gpt-4-0613", SelectedCode = TRUE)
#' # Proofreading text from the clipboard
#' proofreadEnglishText(Model = "gpt-4-0613", SelectedCode = FALSE)
#' }
proofreadEnglishText <- function(Model = "gpt-4",
SelectedCode = TRUE,
verbose = TRUE) {
if(SelectedCode){
assertthat::assert_that(rstudioapi::isAvailable())
input = rstudioapi::getActiveDocumentContext()$selection[[1]]$text
}else{
input = paste0(clipr::read_clip(), collapse = " \n")
}
if(verbose){
cat("\n", "proofreadEnglishText: ", "\n")
pb <- utils::txtProgressBar(min = 0, max = 3, style = 3)
}
# Assertions
assertthat::assert_that(
assertthat::is.string(input),
assertthat::noNA(input),
assertthat::is.string(Model),
Sys.getenv("OPENAI_API_KEY") != ""
)
temperature = 1
if(verbose){utils::setTxtProgressBar(pb, 1)}
# Template creation
template = "
You are an excellent R package builder's assistant and a skilled multilingual translator.
You can accept English text or text in different languages as input.
If necessary, translate the input into English, and use a backslash n to start a new line.
According to the R package policy, you should only output meticulously checked English text without any additional explanation.
"
template1 = "
Please proofread the following input text in English without altering the meaning of the text, and only output the final product.:
"
# Bind into the prompt
template1s <- paste0(template1, paste0(input, collapse = " "), sep=" ")
# Prompt creation
history <- list(list('role' = 'system', 'content' = template),
list('role' = 'user', 'content' = template1s))
if(verbose){utils::setTxtProgressBar(pb, 2)}
# Execution
res <- chat4R_history(history=history,
Model = Model,
temperature = temperature)
#str(res)
if(verbose){utils::setTxtProgressBar(pb, 3)}
if(SelectedCode){
rstudioapi::insertText(text = as.character(res))
#return(message("Finished!!"))
}else{
# Put into the clipboard
return(clipr::write_clip(res))
}
}