-
Notifications
You must be signed in to change notification settings - Fork 1
/
TextSummary.R
267 lines (220 loc) · 8.51 KB
/
TextSummary.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#' Summarize Long Text
#'
#' @title Summarize Long Text
#' @description This function summarizes a long text using LLM.
#' The development of this function started with the idea that it might be interesting
#' to perform a copy-and-paste, sentence summarization and aims to be an evangelist for
#' copy-and-paste LLM execution. It is recommended to run this function with GPT-4, but it is not cost effective and slow.
#' This is still an experimental feature.
#' @param text A character vector containing the text to be summarized.
#' If not provided, the function will attempt to read from the clipboard.
#' @param nch Integer specifying the number of characters at which to split the input text for processing.
#' @param verbose A logical flag to print the message. Default is TRUE.
#' @param returnText A logical flag to return summarized text results. Default is FALSE.
#' @importFrom clipr read_clip write_clip
#' @importFrom utils menu
#' @importFrom assertthat assert_that is.string is.count
#' @return The summarized text is placed into the clipboard and the function returns the result of \code{clipr::write_clip}.
#' @export TextSummary
#' @author Satoshi Kume
#' @examples
#' \dontrun{
#' TextSummary(text = c("This is a long text to be summarized.",
#' "It spans multiple sentences and goes into much detail."),
#' nch = 10)
#' }
TextSummary <- function(text = clipr::read_clip(),
nch = 2000,
verbose = TRUE,
returnText = FALSE){
choices1 <- c("GPT-3.5", "GPT-4 (0613)", "Another LLM")
selection1 <- utils::menu(choices1, title = "Which language model do you prefer?")
if (selection1 == 1) {
Model = "gpt-3.5-turbo"
} else if (selection1 == 2) {
Model = "gpt-4-0613"
} else if (selection1 == 3) {
return(message("No valid selection made."))
} else {
return(message("No valid selection made."))
}
choices2 <- c("High Compression Rate", "Middle Compression Rate", "Low Compression Rate")
selection2 <- utils::menu(choices2, title = "What is the summary rate of the text?")
if (selection2 == 1) {
Summary_block = nch*0.1
} else if (selection2 == 2) {
Summary_block = nch*0.25
} else if (selection2 == 3) {
Summary_block = nch*0.4
} else {
return(message("No valid selection made."))
}
#temperature
temperature = 1
# Validate input types and values
assertthat::assert_that(assertthat::is.string(text[1]))
assertthat::assert_that(assertthat::is.count(nch), nch > 0)
assertthat::assert_that(assertthat::is.count(Summary_block), Summary_block > 0)
assertthat::assert_that(assertthat::is.string(Model))
assertthat::assert_that(assertthat::is.number(temperature), temperature >= 0, temperature <= 1)
# Preprocessing
text0 <- paste0(text, collapse = " ")
text0 <- gsub('\", \n\"', ' ', text0)
text0 <- gsub('[(][0-9][0-9][:][0-9][0-9][)]', '', text0)
text0 <- gsub('[(][0-9][:][0-9][0-9][:][0-9][0-9][)]', '', text0)
# Splitting the text
len <- nchar(text0)
if(len <= nch){
#nch <- len
text1 <- text0
}else{
val <- round(seq(1, len, length.out = ceiling(len/nch)), 0)
# Define the start and end indices for substr
start_indices <- val[1:(length(val)-1)]
end_indices <- val[2:(length(val))]
# Use sapply to apply substr function
text1 <- sapply(seq_len(length(start_indices)),
function(i) substr(text0, start_indices[i], end_indices[i]))
#sum(nchar(unlist(text1)))
}
if(verbose){
cat("\n")
cat("Text nchar:", len, "\n")
cat("Text block:", length(text1), "\n")
}
template0 = "
You are a great assistant and an excellent co-pilot.
Your response should always be both response speed and accuracy.
You summarize and itemize the user's input. Your output is only the summarized text.
You must strictly reproduce and reconsider every detail without being overly concise in your writing.
The language used in the summary is the same as the input text.
"
# Template creation
template1 = "
Please summarize the following text within %s characters.:
"
# Substituting arguments into the prompt
template1s <- sprintf(template1, Summary_block)
# Prompt creation
pr <- paste0(template1s, text1, sep=" ")
# Variable creation
result <- list()
history <- list(list('role' = 'system', 'content' = template0))
if(returnText){
cat("\n")
cat("TextSummary ( nchar:", nchar(text0), "): ", "\n")
pb <- utils::txtProgressBar(min = 0, max = length(pr), style = 3)
}
# Execution
if(verbose){cat("\n")}
for(n in seq_len(length(pr))){
#n <- 1
if(verbose){cat("Text: ", n, "\n")}
history[[length(history) + 1]] <- list('role' = 'user', 'content' = pr[n])
retry_count <- 0
while (retry_count <= 2) {
res <- chat4R_history(history = history,
Model = Model,
temperature = temperature)
if(nchar(res) < Summary_block + 100){ break }
retry_count <- retry_count + 1
}
if(verbose){cat(res, "\n")}
history[[length(history) + 1]] <- list('role' = 'assistant', 'content' = res)
history <- history[sapply(history[1:length(history)], function(x) x$role) != "user"]
#output
result[[n]] <- res
if(returnText){utils::setTxtProgressBar(pb, n)}
}
# Put into the clipboard
if(returnText){
return(unlist(result))
}else{
txt2 <- paste0(result, collapse = " ")
if(verbose){
cat("\n")
cat("Summarized text nchar:", nchar(txt2), "\n")
message("Finished!!")
}
return(clipr::write_clip(txt2))
}
}
.TextSummary_v1 <- function(text,
nch = 2000,
verbose = TRUE){
Model = "gpt-3.5-turbo"
Summary_block = nch*0.1
temperature = 1
# Validate input types and values
assertthat::assert_that(assertthat::is.string(text[1]))
assertthat::assert_that(assertthat::is.count(nch), nch > 0)
assertthat::assert_that(assertthat::is.count(Summary_block), Summary_block > 0)
assertthat::assert_that(assertthat::is.string(Model))
assertthat::assert_that(assertthat::is.number(temperature), temperature >= 0, temperature <= 1)
# Preprocessing
text0 <- paste0(text, collapse = " ")
text0 <- gsub('\", \n\"', ' ', text0)
text0 <- gsub('[(][0-9][0-9][:][0-9][0-9][)]', '', text0)
text0 <- gsub('[(][0-9][:][0-9][0-9][:][0-9][0-9][)]', '', text0)
# Splitting the text
len <- nchar(text0)
if(len <= nch){
#nch <- len
text1 <- text0
}else{
val <- round(seq(1, len, length.out = ceiling(len/nch)), 0)
# Define the start and end indices for substr
start_indices <- val[1:(length(val)-1)]
end_indices <- val[2:(length(val))]
# Use sapply to apply substr function
text1 <- sapply(seq_len(length(start_indices)),
function(i) substr(text0, start_indices[i], end_indices[i]))
#sum(nchar(unlist(text1)))
}
if(verbose){
cat("\n\n")
cat("Text nchar:", len, "\n")
cat("Text block:", length(text1), "\n")
}
template0 = "
You are a great assistant and an excellent co-pilot.
Your response should always be both response speed and accuracy.
You summarize and itemize the user's input. Your output is only the summarized text.
You must strictly reproduce and reconsider every detail without being overly concise in your writing.
The language used in the summary is the same as the input text.
"
# Template creation
template1 = "
Please summarize the following text within %s characters.:
"
# Substituting arguments into the prompt
template1s <- sprintf(template1, Summary_block)
# Prompt creation
pr <- paste0(template1s, text1, sep=" ")
# Variable creation
result <- list()
history <- list(list('role' = 'system', 'content' = template0))
# Execution
for(n in seq_len(length(pr))){
#n <- 1
if(verbose){cat("\n")}
if(verbose){cat("Text: ", n, "\n")}
history[[length(history) + 1]] <- list('role' = 'user', 'content' = pr[n])
retry_count <- 0
while (retry_count <= 2) {
res <- chat4R_history(history = history,
Model = Model,
temperature = temperature)
if(nchar(res) < Summary_block + 100){ break }
retry_count <- retry_count + 1
}
if(verbose){cat(res, "\n")}
history[[length(history) + 1]] <- list('role' = 'assistant', 'content' = res)
history <- history[sapply(history[1:length(history)], function(x) x$role) != "user"]
#output
result[[n]] <- res
}
# Put into the clipboard
#if(verbose)(message("Finished!!"))
return(paste0(unlist(result), collapse = " "))
}