-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathbackend-ellmer.R
More file actions
91 lines (88 loc) · 2.15 KB
/
Copy pathbackend-ellmer.R
File metadata and controls
91 lines (88 loc) · 2.15 KB
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
#' @export
ch_submit.ch_ellmer <- function(
defaults,
prompt = NULL,
stream = TRUE,
prompt_build = TRUE,
preview = FALSE,
shiny = FALSE,
...) {
if (preview) {
return(prompt)
}
ch_ellmer_init(defaults)
if (prompt_build) {
prompt <- ch_ellmer_prompt(prompt, defaults)
}
ret <- NULL
if (!shiny) {
chat <- ch_env$ellmer_obj$stream(prompt)
coro::loop(for (chunk in chat) {
ret <- paste0(ret, chunk)
if (stream) {
ide_paste_text(chunk)
}
})
} else {
ch_app_output(reset = TRUE)
stream <- ch_env$ellmer_obj$stream_async(prompt)
coro::async(function() {
ch_app_status("busy")
for (chunk in coro::await_each(stream)) {
ch_app_output(append = chunk)
}
ch_app_status("idle")
})()
}
ret
}
ch_ellmer_init <- function(defaults = NULL, chat = NULL) {
if (!is.null(chat)) {
ch_env$ellmer_obj <- chat$clone()$set_turns(list())
}
if (!is.null(defaults)) {
new_code <- defaults$ellmer
system_msg <- defaults$system_msg
if (!is.null(new_code)) {
old_code <- ch_env$ellmer_code %||% ""
run_code <- FALSE
if (old_code != new_code) {
code_expr <- rlang::parse_expr(new_code)
ch_env$ellmer_obj <- rlang::eval_bare(code_expr)
}
}
} else {
system_msg <- NULL
}
if (!is.null(system_msg)) {
system_msg <- bulleted_list(system_msg)
ch_env$ellmer_obj$set_system_prompt(system_msg)
}
invisible()
}
ch_ellmer_prompt <- function(prompt, defaults) {
bulleted_list(
c(
process_prompt(defaults$prompt),
ch_context_data_files(defaults$max_data_files),
ch_context_data_frames(defaults$max_data_frames),
prompt
)
)
}
ch_ellmer_history <- function(x) {
defaults <- chattr_defaults()
if (defaults$mode %||% "" == "ellmer" &&
inherits(x, c("list", "ch_history"))
) {
new_history <- map(x, ~ {
ellmer::Turn(
role = .x$role,
contents = list(ellmer::ContentText(.x$contents)),
tokens = .x$tokens %||% c(0, 0, if (packageVersion("ellmer") > "0.2.1") 0)
)
})
ch_env$ellmer_obj$set_turns(new_history)
}
invisible()
}