-
Notifications
You must be signed in to change notification settings - Fork 1
/
extractKeywords.R
98 lines (82 loc) · 3.21 KB
/
extractKeywords.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
#' Extract Keywords from Text
#'
#' This function extracts keywords from the input text. It uses the OpenAI GPT model
#' for text generation to assist in the extraction process. The function reads the input
#' from the clipboard and outputs the extracted keywords in a comma-separated format.
#'
#' @title extractKeywords
#' @description Extract keywords from input text and output them in a comma-separated format.
#' @param Model The OpenAI GPT model to use for text generation. Default is "gpt-4-0613".
#' @param verbose Logical flag to indicate whether to print the result. Default is TRUE.
#' @param SlowTone Logical flag to indicate the speed of the output. Default is FALSE.
#' @importFrom clipr read_clip
#' @importFrom assertthat assert_that is.string noNA
#' @return Prints the extracted keywords based on verbosity and tone speed settings.
#' @export extractKeywords
#' @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
#' extractKeywords(Model = "gpt-4-0613", verbose = TRUE, SlowTone = FALSE)
#' }
extractKeywords <- function(Model = "gpt-4-0613",
verbose = TRUE,
SlowTone = FALSE) {
# Read input from clipboard
input = paste0(clipr::read_clip(), collapse = " \n")
if(verbose){
cat("\n", "extractKeywords: ", "\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 for text generation
temperature = 1
# Create template for the prompt
template = "
You are an excellent and highly skilled assistant.
You need to extract keywords from the input text.
The keywords should be in comma separated format.
The language used is the same as the input text.
"
if(verbose){utils::setTxtProgressBar(pb, 1)}
template <- ngsub(template)
template1 = "
Please extract keywords from the following input text and provide keywords in comma-separated format.:
"
# 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 <- chat4R_history(history=history,
Model = Model,
temperature = temperature)
if(verbose){utils::setTxtProgressBar(pb, 3)}
# Print the result based on verbosity and tone speed
if(verbose) {
cat("\n\n")
cat("Result from extractKeywords :\n")
if(SlowTone) {
d <- ifelse(5/nchar(res) < 0.3, 5/nchar(res), 0.3) * stats::runif(1, min = 0.95, max = 1.05)
slow_print_v2(res, delay = d)
} else {
d <- ifelse(2.5/nchar(res) < 0.15, 2.5/nchar(res), 0.15) * stats::runif(1, min = 0.95, max = 1.05)
slow_print_v2(res, delay = d)
}
}
# Output into your clipboard
if(verbose){
cat("\n")
message("Finished!!")
}
return(clipr::write_clip(res))
}