Skip to content

Commit

Permalink
Merge pull request #82 from mlverse/updates
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
edgararuiz authored Apr 1, 2024
2 parents e1d1720 + 39ef9d1 commit 8cb448c
Show file tree
Hide file tree
Showing 43 changed files with 721 additions and 1,648 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: chattr
Title: Integrates LLM's with the RStudio IDE
Version: 0.0.0.9008
Version: 0.0.0.9009
Authors@R: c(
person("Edgar", "Ruiz", , "[email protected]", role = c("aut", "cre")),
person(given = "Posit Software, PBC", role = c("cph", "fnd"))
Expand Down
19 changes: 3 additions & 16 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
# Generated by roxygen2: do not edit by hand

S3method(app_init_message,cl_openai)
S3method(app_init_message,ch_openai)
S3method(app_init_message,default)
S3method(ch_submit,ch_llamagpt)
S3method(ch_submit,ch_openai)
S3method(ch_submit,test_backend)
S3method(ch_test,ch_llamagpt)
S3method(ch_test,ch_openai_chat_completions)
S3method(ch_test,ch_openai_completions)
S3method(ch_test,ch_openai_github_copilot_chat)
S3method(openai_completion,ch_openai_chat_completions)
S3method(openai_completion,ch_openai_completions)
S3method(openai_completion,ch_openai_github_copilot_chat)
S3method(openai_prompt,ch_openai)
S3method(openai_prompt,ch_openai_completions)
S3method(openai_request,ch_openai)
S3method(openai_request,ch_openai_github_copilot_chat)
S3method(openai_stream_content,ch_openai_chat_completions)
S3method(openai_stream_content,ch_openai_completions)
S3method(openai_stream_content,ch_openai_github_copilot_chat)
S3method(openai_token,ch_openai)
S3method(openai_token,ch_openai_github_copilot_chat)
S3method(print,ch_history)
S3method(print,ch_model)
S3method(print,ch_request)
export(ch_history)
export(ch_submit)
export(ch_submit_job)
export(ch_submit_job_stop)
export(ch_test)
export(chattr)
export(chattr_app)
Expand Down Expand Up @@ -63,6 +49,7 @@ importFrom(purrr,walk)
importFrom(rlang,"%||%")
importFrom(rlang,abort)
importFrom(rlang,is_interactive)
importFrom(rlang,is_na)
importFrom(rlang,is_named)
importFrom(utils,capture.output)
importFrom(utils,head)
Expand Down
126 changes: 47 additions & 79 deletions R/app_server.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
app_server <- function(input, output, session) {
ch_env$content_hist <- NULL
ch_env$current_stream <- NULL
style <- app_theme_style()
r_file_stream <- tempfile()
r_file_complete <- tempfile()
ch_env$stream_output <- ""
app_add_history(input)
auto_invalidate <- reactiveTimer(100)
session$sendCustomMessage(type = "refocus", message = list(NULL))

insertUI(
selector = "#tabs",
where = "beforeBegin",
Expand All @@ -14,9 +15,23 @@ app_server <- function(input, output, session) {
)
)

observeEvent(input$options, showModal(app_ui_modal()))
output$stream <- renderText({
auto_invalidate()
if (ch_r_state() == "busy") {
ch_env$stream_output <- paste0(ch_env$stream_output, ch_r_output())
markdown(ch_env$stream_output)
}
})

output$provider <- renderText({
defaults <- chattr_defaults()
defaults$label
})

app_add_history(style, input)
observeEvent(
input$options,
showModal(app_ui_modal())
)

observeEvent(input$saved, {
chattr_defaults(
Expand All @@ -30,72 +45,24 @@ app_server <- function(input, output, session) {
})

observeEvent(input$submit, {
if (input$prompt != "" && is.null(ch_env$current_stream)) {
if (input$prompt != "" && ch_r_state() == "idle") {
ch_history_append(user = input$prompt)
app_add_user(input$prompt)

updateTextAreaInput(
inputId = "prompt",
value = ""
)

updateTextAreaInput(inputId = "prompt", value = "")
session$sendCustomMessage(type = "refocus", message = list(NULL))
}
})

observeEvent(input$submit, {
if (input$prompt != "" && is.null(ch_env$current_stream)) {
ch_submit_job(
ch_r_submit(
prompt = input$prompt,
defaults = chattr_defaults(type = "chat"),
prompt_build = TRUE,
r_file_complete = r_file_complete,
r_file_stream = r_file_stream
defaults = chattr_defaults(type = "chat")
)
}
})

auto_invalidate <- reactiveTimer(100)

observe({
auto_invalidate()
if (file_exists(r_file_complete)) {
out <- app_server_file_complete(r_file_complete)
Sys.sleep(0.01)
app_add_assistant(out, input)
}
})

output$stream <- renderText({
auto_invalidate()
if (file_exists(r_file_stream)) {
app_server_file_stream(r_file_stream)
markdown(ch_env$current_stream)
}
})

output$provider <- renderText({
defaults <- chattr_defaults()
defaults$label
})

observe({
auto_invalidate()
error <- r_session_error()
if (!is.null(error)) {
stopApp()
print(error)
abort("Streaming returned error")
}
})

observeEvent(input$open, {
file <- try(file.choose(), silent = TRUE)
ext <- path_ext(file)
if (ext == "rds") {
rds <- readRDS(file)
ch_history_set(rds)
app_add_history(style, input)
ch_history_set(readRDS(file))
app_add_history(input)
removeModal()
}
})
Expand All @@ -109,9 +76,29 @@ app_server <- function(input, output, session) {
})

observeEvent(input$close, stopApp())

observe({
auto_invalidate()
if (ch_r_state() == "idle" && ch_env$stream_output != "") {
Sys.sleep(0.02)
ch_history_append(assistant = ch_env$stream_output)
app_add_assistant(ch_env$stream_output, input)
ch_env$stream_output <- ""
}
})

observe({
auto_invalidate()
error <- ch_r_error()
if (!is.null(error)) {
stopApp()
print(error)
abort("Streaming returned error")
}
})
}

app_add_history <- function(style, input) {
app_add_history <- function(input) {
th <- ch_history()
for (i in seq_along(th)) {
curr <- th[[i]]
Expand Down Expand Up @@ -202,22 +189,3 @@ app_split_content <- function(content) {
}
)
}

app_server_file_complete <- function(r_file_complete) {
Sys.sleep(0.02)
out <- readRDS(r_file_complete)
ch_history_append(assistant = out)
file_delete(r_file_complete)
ch_env$current_stream <- NULL
out
}

app_server_file_stream <- function(r_file_stream) {
current_stream <- r_file_stream %>%
readRDS() %>%
try(silent = TRUE)
if (!inherits(current_stream, "try-error")) {
ch_env$current_stream <- current_stream
}
invisible()
}
69 changes: 14 additions & 55 deletions R/backend-llamagpt.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,27 @@ ch_submit.ch_llamagpt <- function(
stream = NULL,
prompt_build = TRUE,
preview = FALSE,
r_file_stream = NULL,
r_file_complete = NULL,
...) {
if (ui_current_markdown()) {
return(invisible())
}

prompt <- ide_build_prompt(
prompt = prompt,
defaults = defaults,
preview = preview
)

if (prompt_build) {
new_prompt <- paste0(prompt, "(", defaults$prompt[[1]], ")")
} else {
new_prompt <- prompt
}

ret <- NULL
if (preview) {
ret <- as_ch_request(new_prompt, defaults)
ret <- new_prompt
} else {
ch_llamagpt_session(defaults, r_file_stream, r_file_complete)

ch_llamagpt_session(defaults)
ch_llamagpt_prompt(new_prompt)

if (defaults$type == "default") {
ui <- ui_current()
} else {
ui <- defaults$type
}

ret <- ch_llamagpt_output(ui, r_file_stream, r_file_complete)
ret <- ch_llamagpt_output(stream = stream)
}

ret
}

# ----------------------------- Session ----------------------------------------

ch_llamagpt_session <- function(
defaults = chattr_defaults(),
r_file_stream = NULL,
r_file_complete = NULL,
testing = FALSE) {
init_session <- FALSE
if (is.null(ch_env$llamagpt$session)) {
Expand All @@ -71,7 +47,7 @@ ch_llamagpt_session <- function(
stdin = "|"
)
if (!testing) {
ch_llamagpt_printout(defaults, r_file_stream)
ch_llamagpt_printout(defaults)
}
}
ch_env$llamagpt$session
Expand All @@ -83,11 +59,9 @@ ch_llamagpt_prompt <- function(prompt) {
}

ch_llamagpt_output <- function(
stream_to,
stream_file = NULL,
output_file = NULL,
timeout = 1000,
output = NULL) {
output = NULL,
stream = FALSE,
timeout = 1000) {
all_output <- NULL
stop_stream <- FALSE
timeout <- timeout / 0.01
Expand All @@ -104,24 +78,13 @@ ch_llamagpt_output <- function(
output <- substr(output, 1, nchar(output) - 2)
stop_stream <- TRUE
}
if (stream) {
cat(output)
}
all_output <- paste0(all_output, output)

if (stream_to == "console") cat(output)
if (stream_to == "script") ide_paste_text(output)
if (stream_to == "chat") saveRDS(all_output, stream_file, compress = FALSE)

output <- NULL

if (stop_stream) {
if (stream_to == "chat") {
if (!is.null(output_file)) {
saveRDS(all_output, output_file, compress = FALSE)
}
file_delete(stream_file)
return(NULL)
} else {
return(NULL)
}
return(all_output)
}
}
}
Expand All @@ -137,23 +100,19 @@ ch_llamagpt_stop <- function() {
ch_llamagpt_args <- function(defaults) {
args <- defaults$model_arguments
args$model <- path_expand(defaults$model)

imap(
args,
~ c(paste0("--", .y), .x)
) %>%
reduce(c)
}

ch_llamagpt_printout <- function(
defaults,
r_file_stream = NULL,
output = NULL) {
ch_llamagpt_printout <- function(defaults, output = NULL) {
if (defaults$type == "chat") {
ch_llamagpt_output("chat", r_file_stream)
ch_llamagpt_output()
} else {
cli_h2("chattr")
cli_h3("Initializing model")
ch_llamagpt_output("console", output = output)
cat(ch_llamagpt_output(output))
}
}
Loading

0 comments on commit 8cb448c

Please sign in to comment.