-
Notifications
You must be signed in to change notification settings - Fork 26
/
history.R
446 lines (408 loc) · 12.7 KB
/
history.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
get_history_path <- function(file_name = "chat_history", local = FALSE) {
if (local) {
file.path(
tools::R_user_dir("gpttools", which = "data"),
glue::glue("local/{file_name}.parquet")
)
} else {
file.path(
tools::R_user_dir("gpttools", which = "data"),
glue::glue("{file_name}.parquet")
)
}
}
#' Read chat history from a file
#'
#' @param file_name Name of the chat history file to read from. Default is
#' "chat_history".
#' @param local Whether to read from history made with local or OpenAI
#' embeddings.
#'
#' @return A dataframe containing the chat history or NULL if the file doesn't
#' exist.
#' @export
#'
#' @examples
#' \dontrun{
#' # Read chat history from the default file
#' history <- read_history()
#' }
read_history <- function(file_name = "chat_history", local) {
file_path <- get_history_path(file_name, local = local)
if (file.exists(file_path)) {
arrow::read_parquet(file_path)
} else {
NULL
}
}
#' Delete chat history files
#'
#' This function interactively deletes chat history files stored in the user's
#' directory. It lists all the .parquet files in the gpttools data directory and
#' prompts the user for confirmation before deleting each file.
#'
#' @param local Whether to delete history made with local or OpenAI embeddings.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' # Interactively delete chat history files
#' delete_history()
#' }
delete_history <- function(local = FALSE) {
if (!interactive()) {
invisible()
}
history_files <- get_history_path(local = local)
purrr::map(history_files, \(x) {
delete_file <- ui_yeah("Do you want to delete {basename(x)}?")
if (delete_file) {
file.remove(x)
} else {
cli_alert_info("{x} was **not** deleted.")
}
})
invisible()
}
create_history <- function(file_name = "chat_history",
local = FALSE,
overwrite = FALSE) {
file_path <- get_history_path(file_name, local = local)
if (!dir.exists(tools::file_path_sans_ext(file_path))) {
dir.create(tools::file_path_sans_ext(file_path),
recursive = TRUE, showWarnings = FALSE
)
}
if (!file.exists(file_path) || overwrite) {
query_history <- tibble::tibble(
id = integer(),
timestamp = Sys.time(),
role = character(),
content = character(),
hash = character()
)
arrow::write_parquet(query_history, file_path)
}
}
save_user_history <- function(file_name = "chat_history",
role,
content,
local = FALSE,
model = NULL,
overwrite = FALSE) {
create_history(file_name, overwrite = overwrite, local = local)
history <- read_history(file_name, local = local)
embedding <- get_query_embedding(content, local = local, model = model)
new_entry <- tibble::tibble(
id = max(0, history$id, na.rm = TRUE) + 1,
timestamp = Sys.time(),
role = role,
content = content,
embedding = list(embedding),
hash = hash_md5(content)
)
new_entry |>
rbind(history) |>
arrow::write_parquet(get_history_path(file_name, local = local))
}
get_query_embedding <- function(query, local = FALSE, model = NULL) {
if (local) {
create_text_embeddings(query,
model = model
) |>
dplyr::pull(embedding) |>
unlist()
} else {
create_openai_embedding(input_text = query) |>
dplyr::pull(embedding) |>
unlist()
}
}
get_query_context <- function(query_embedding, full_context, k) {
check_context(full_context)
get_top_matches(full_context, query_embedding, k = k)
}
check_context <- function(context) {
if (rlang::is_null(context)) {
cli_alert_warning(
"You specified that context should be added but none was provided."
)
} else if (!is.data.frame(context)) {
cli_alert_warning(
"You passed a {class(context)} to but a data.frame was expected."
)
}
}
#' chat_with_context
#'
#' This function allows you to chat with a chatbot that answers questions based
#' on the provided context and chat history. It uses GPT-4 architecture to
#' generate responses.
#'
#' @param query The input query to be processed.
#' @param service Name of the AI service to use, defaults to openai.
#' @param model Name of the openai model to use, defaults to gpt-3.5-turbo
#' @param index Index to look for context.
#' @param add_context Whether to add context to the query. Options are
#' `"always"`, `"sometimes"`, and `"never"`. The default is `"sometimes"`.
#' @param chat_history Chat history dataframe for reference.
#' @param history_name Name of the file where chat history is stored.
#' @param session_history Session history data for reference.
#' @param add_history Whether to add chat history to the query or not. Default
#' is TRUE.
#' @param task Task type, either "Context Only" or "Permissive Chat". Default is
#' "Context Only".
#' @param k_context Number of top context matches to consider. Default is 4.
#' @param k_history Number of top chat history matches to consider. Default is
#' 4.
#' @param save_history Whether to save the chat history or not. Default is TRUE.
#' @param overwrite Whether to overwrite the history file or not. Default is
#' FALSE.
#' @param local Whether to use the local model or not. Default is FALSE.
#' @param embedding_model A model object to use for embedding. Only needed if
#' local is TRUE. Default is NULL.
#' @param stream Whether to stream the response or not. Default is FALSE.
#' @param rv A reactive value to store the response. Default is NULL.
#'
#' @return A list containing the prompt, context, and answer.
#' @export
#'
#' @examplesIf rlang::is_interactive()
#' rlang::is_interactive()
#' query <- "What is the capital of France?"
#' result <- chat_with_context(query = query, context = context)
chat_with_context <- function(query,
service = "openai",
model = "gpt-4-turbo-preview",
index = NULL,
add_context = TRUE,
chat_history = NULL,
history_name = "chat_history",
session_history = NULL,
add_history = TRUE,
task = "Context Only",
k_context = 4,
k_history = 4,
save_history = TRUE,
overwrite = FALSE,
local = FALSE,
embedding_model = NULL,
stream = FALSE,
rv = NULL) {
arg_match(task, c("Context Only", "Permissive Chat"))
if (add_context == "sometimes") {
check_context <- TRUE
} else if (add_context == "never") {
check_context <- FALSE
add_context <- FALSE
} else if (add_context == "always") {
check_context <- FALSE
add_context <- TRUE
}
if (rlang::is_true(check_context)) {
need_context <- is_context_needed(
user_prompt = query,
service = service,
model = model
)
} else {
need_context <- TRUE
}
if (rlang::is_true(add_context) || rlang::is_true(add_history)) {
cli_alert_info("Creating embedding from query.")
query_embedding <- get_query_embedding(query,
local = local,
model = embedding_model
)
}
if (rlang::is_true(add_context) && rlang::is_true(need_context)) {
cli_alert_info("Attempting to add context to query.")
full_context <-
get_query_context(
query_embedding,
index,
k_context
)
context <-
full_context |>
dplyr::select(source, link, chunks) |>
purrr::pmap(\(source, link, chunks) {
glue::glue("Source: {source}
Link: {link}
Text: {chunks}")
}) |>
unlist() |>
paste(collapse = "\n\n")
} else {
full_context <- "No context provided."
context <- "No additional context provided."
}
if (rlang::is_true(add_history) && rlang::is_true(need_context)) {
cli_alert_info("Attempting to add chat history to query.")
cli_alert_info("Chat history: {class(chat_history)}")
if (rlang::is_null(chat_history)) {
related_history <- "No related history found."
} else {
related_history <-
get_query_context(
query_embedding,
chat_history,
k_history
) |>
dplyr::distinct(content) |>
dplyr::pull(content) |>
paste(collapse = "\n\n")
}
} else {
cli_alert_info("Not attempting to add chat history to query.")
related_history <- "No related history found."
}
prompt_instructions <-
switch(task,
"Context Only" =
list(
list(
role = "system",
content =
glue(
"You are a helpful chat bot that answers questions based on
the context provided by the user. If the user does not
provide related context and you need context to respond
accurately, say \"I am not able to answer that question.
Maybe try rephrasing your question in a different way.\""
)
)
),
"Permissive Chat" =
list(
list(
role = "system",
content =
glue(
"You are a helpful chat bot that answers questions based on
the context provided by the user."
)
)
)
)
prompt_context <- list(
list(
role = "system",
content = "You provide succinct, concise, and accurate responses."
),
list(
role = "user",
content = glue("---\nContext:\n{context}\n---")
),
list(
role = "user",
content = glue("---\nRelated Chat History:\n{related_history}\n---")
)
)
prompt_query <- list(
list(
role = "user",
content = glue("{query}")
)
)
session_history <-
purrr::map(session_history, \(x) {
if (x$role == "system") {
NULL
} else if (stringr::str_detect(
x$content,
pattern = "---\nContext:\n|---\nRelated Chat History:\n"
)
) {
NULL
} else {
x
}
}) |>
purrr::compact()
prompt <- c(
session_history,
prompt_instructions,
prompt_context,
prompt_query
)
simple_prompt <- prompt |>
purrr::map_chr(.f = "content") |>
paste(collapse = "\n\n")
cli_alert_info("Service: {service}")
cli_alert_info("Model: {model}")
cli_alert_info("Stream: {stream}")
if (rlang::is_true(stream)) {
cli_alert_info("Attempting to stream chat.")
if (rlang::is_null(rv)) {
answer <-
stream_chat_openai(
prompt = simple_prompt,
element_callback = create_handler(service)
)
} else {
stream_chat(
prompt = simple_prompt,
service = service,
r = rv,
output_id = "streaming",
where = "shiny"
)
answer <- rv$response
}
} else {
answer <- chat(
prompt = simple_prompt,
service = service,
model = model,
stream = FALSE
)
}
if (save_history) {
purrr::map(prompt, \(x) {
save_user_history(
file_name = history_name,
role = "system",
content = x$content,
local = local,
model = embedding_model,
overwrite = overwrite
)
})
save_user_history(
file_name = history_name,
role = "assistant",
content = answer,
local = local,
model = embedding_model,
overwrite = overwrite
)
}
prompt_without_context <-
c(session_history, prompt_query)
list(prompt_without_context, full_context, answer)
}
is_context_needed <- function(user_prompt,
service = getOption("gpttools.service"),
model = getOption("gpttools.model")) {
prompt <-
glue::glue("Consider if additional context or history would be useful to
accurately respond to this user prompt. Useful context may include
information like package documentation, textbook excerpts, or
other relevant details.
Respond with TRUE if such context is likely to enhance the
response. Respond with FALSE only if the
query seems straightforward and well within your existing
knowledge base.
Most queries benefit from additional context.
Respond ONLY with TRUE or FALSE.
\n\n{user_prompt}")
chat(
prompt = prompt,
service = service,
model = model,
stream = FALSE
) |>
as.logical()
}