forked from simonpcouch/pal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpal-class.R
59 lines (55 loc) · 1.56 KB
/
pal-class.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
Pal <- R6::R6Class(
"Pal",
public = list(
initialize = function(role, fn, ..., .ns) {
self$role <- role
args <- list(...)
default_args <- getOption(".pal_args", default = list())
args <- modifyList(default_args, args)
args$system_prompt <- get(
paste0(".pal_prompt_", role),
envir = pal_env()
)
Chat <- rlang::eval_bare(rlang::call2(fn, !!!args, .ns = .ns))
private$Chat <- Chat
.stash_last_pal(self)
},
chat = function(expr) {
request <- deparse(substitute(expr))
private$.chat(request)
},
stream = function(expr) {
request <- deparse(substitute(expr))
private$.stream(request)
},
role = NULL,
print = function(...) {
model <- private$Chat[[".__enclos_env__"]][["private"]][["provider"]]@model
cli::cli_h3(
"A {.field {self$role}} pal using {.field {model}}."
)
}
),
private = list(
Chat = NULL,
.chat = function(request, call = caller_env()) {
if (identical(request, "")) {
cli::cli_abort("Please supply a non-empty chat request.", call = call)
}
structure(
private$Chat$chat(paste0(request, collapse = "\n")),
class = c("pal_response", "character")
)
},
.stream = function(request, call = caller_env()) {
if (identical(request, "")) {
cli::cli_abort("Please supply a non-empty chat request.", call = call)
}
private$Chat$stream(paste0(request, collapse = "\n"))
}
)
)
#' @export
print.pal_response <- function(x, ...) {
cat(x)
}