-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsupportIdeaGeneration.R
111 lines (95 loc) · 4.07 KB
/
supportIdeaGeneration.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#' Support Idea Generation from Selected Text or Clipboard Input
#'
#' This feature helps you generate ideas or concepts based on input from your selected text or clipboard.
#' It uses the OpenAI GPT model for text generation to assist in the idea generation process.
#' The function reads the input from the clipboard.
#'
#' @title supportIdeaGeneration: Support Idea Generation from Selected Text or Clipboard.
#' @description Assist in generating ideas or concepts.
#' @param Model The OpenAI GPT model to use for text generation. Default is "gpt-4o-mini".
#' @param SelectedCode Logical flag to indicate whether to use the selected text in RStudio editor. Default is TRUE.
#' @param verbose Logical flag to indicate whether to display the generated text. Default is TRUE.
#' @param SlowTone Logical flag to indicate whether to print the text slowly. Default is FALSE.
#' @importFrom rstudioapi isAvailable getActiveDocumentContext insertText
#' @importFrom clipr read_clip
#' @importFrom assertthat assert_that is.string noNA
#' @importFrom stats runif
#' @return Prints the generated ideas or concepts based on the verbosity and tone speed settings.
#' @export supportIdeaGeneration
#' @author Satoshi Kume
#' @examples
#' \dontrun{
#' # Option 1
#' # Select some text in RStudio and then run the rstudio addins
#' # Option 2
#' # Copy the text into your clipboard then execute
#' supportIdeaGeneration()
#' }
supportIdeaGeneration <- function(Model = "gpt-4o-mini",
SelectedCode = TRUE,
verbose = TRUE,
SlowTone = FALSE) {
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", "supportIdeaGeneration: ", "\n")
pb <- utils::txtProgressBar(min = 0, max = 3, style = 3)
}
# Assertions for input validation
assertthat::assert_that(
assertthat::is.string(input),
assertthat::noNA(input),
Sys.getenv("OPENAI_API_KEY") != ""
)
# Initialize temperature
temperature = 1
if(verbose){utils::setTxtProgressBar(pb, 1)}
# Create template for the prompt
template = "
You are an exceptional assistant and an exceptionally skilled R programmer.
You do not have to respond at the R code level.
You need to take a given idea as input and articulate the idea or concept in written form.
An idea is often the starting point for a new product, service, or solution.
A concept is the development of those ideas into a concrete plan or design.
You are the navigator, providing the direction to follow and how to translate it into a program.
You provide only the summary and bulleted details of the deliverables.
The language used in the output deliverables must be the same as the language of the following input.
"
template1 = "
Please provide ideas and concepts concisely and comprehensively for further discussion using the following input.:
"
# Substitute arguments into the prompt
template1s <- paste0(template1, paste0(input), sep=" ")
# Create prompt history
history <- list(list('role' = 'system', 'content' = template),
list('role' = 'user', 'content' = template1s))
if(verbose){utils::setTxtProgressBar(pb, 2)}
# Execute the chat model
res <- as.character(chat4R_history(history=history,
Model = Model,
temperature = temperature))
if(verbose){
utils::setTxtProgressBar(pb, 3)
cat("\n\n")
}
# Output
if(SelectedCode){
rstudioapi::insertText(text = as.character(res))
} else {
# Print the result based on verbosity and tone speed
if(verbose) {
if(SlowTone) {
d <- ifelse(20/nchar(res) < 0.3, 20/nchar(res), 0.3)*stats::runif(1, min = 0.95, max = 1.05)
slow_print_v2(res, delay = d)
} else {
d <- ifelse(10/nchar(res) < 0.15, 10/nchar(res), 0.15)*stats::runif(1, min = 0.95, max = 1.05)
slow_print_v2(res, delay = d)
}
}
return(clipr::write_clip(res))
}
}