Skip to content

Commit

Permalink
improve shiny reactivity and add xml extract input option
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanpieper committed Oct 5, 2024
1 parent 2efcbc2 commit 66d7ecb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
9 changes: 5 additions & 4 deletions R/batchLLM.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ batchLLM <- function(df,
max_tokens = 500,
batch_delay = "random",
batch_size = 10,
extract_XML = TRUE,
extract_XML = FALSE,
attempts = 1,
log_name = "batchLLM-log",
hash_algo = "crc32c",
Expand Down Expand Up @@ -490,7 +490,7 @@ get_batches <- function(df_name = NULL, log_name = "batchLLM-log") {
}

if (!df_name %in% names(batch_log$data)) {
stop(paste0("No data found in ", log_name, ".rds for the specified df_name."))
stop(paste0("No data found in ", log_name, ".rds for the specified df_name"))
}

output <- batch_log$data[[df_name]]$output
Expand Down Expand Up @@ -589,8 +589,9 @@ scrape_metadata <- function(df_name = NULL, log_name = "batchLLM-log") {

#' @title Interact with Anthropic's Claude API
#'
#' @description This function is from the [claudeR](https://github.com/yrvelez/claudeR) repository by [yrvelez](https://github.com/yrvelez) on GitHub (not currently available on CRAN).
#'
#' @description This function provides an interface to interact with Claude AI models via Anthropic's API, allowing for flexible text generation based on user inputs.
#' This function was adapted from the [claudeR](https://github.com/yrvelez/claudeR) repository by [yrvelez](https://github.com/yrvelez) on GitHub (MIT License).
#'
#' @param api_key Your API key for authentication.
#' @param prompt A string vector for Claude-2, or a list for Claude-3 specifying the input for the model.
#' @param model The model to use for the request. Default is the latest Claude-3 model.
Expand Down
58 changes: 49 additions & 9 deletions R/batchLLM_shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ batchLLM_shiny <- function() {
)
)
}

labelWithInfo <- function(label, tooltip) {
tagList(
span(label),
Expand Down Expand Up @@ -133,7 +133,7 @@ batchLLM_shiny <- function() {
width = 4,
box(
width = NULL,
title = "Upload Data File",
title = "Upload Data",
solidHeader = TRUE,
fileInput("datafile", "Choose CSV or Excel File", accept = c(".csv", ".xlsx"))
),
Expand All @@ -143,7 +143,7 @@ batchLLM_shiny <- function() {
solidHeader = TRUE,
pickerInput(
inputId = "df_name",
label = "Select Data:",
label = "Data:",
choices = df_objects,
options = list(`live-search` = TRUE)
),
Expand Down Expand Up @@ -182,6 +182,13 @@ batchLLM_shiny <- function() {
selected = "none",
justified = TRUE
),
radioGroupButtons(
inputId = "extract_XML",
label = labelWithInfo("XML Extract:", "Requests a response in XML tags and removes unwanted text and preamble. May produce an unintended XML structure in longer responses."),
choices = c("False" = "FALSE", "True" = "TRUE"),
selected = "FALSE",
justified = TRUE
),
actionButton(
inputId = "run_batchLLM",
label = "Run batchLLM"
Expand Down Expand Up @@ -253,19 +260,22 @@ batchLLM_shiny <- function() {
session$userData$beliefs <- beliefs
}

all_objects <- reactive({
c(ls(envir = .GlobalEnv), ls(envir = session$userData))
})
all_objects <- reactiveVal(c())

observe({
req(input$datafile)

current_objects <- c(ls(envir = .GlobalEnv), ls(envir = session$userData))
all_objects(current_objects)

df_objects <- Filter(function(x) {
obj <- if (exists(x, envir = session$userData)) {
get(x, envir = session$userData)
} else {
get(x, envir = .GlobalEnv)
}
is.data.frame(obj) || inherits(obj, "data.frame")
}, all_objects())
}, current_objects)

updatePickerInput(
session = session,
Expand All @@ -281,7 +291,7 @@ batchLLM_shiny <- function() {
df <- switch(ext,
csv = readr::read_csv(file),
xlsx = readxl::read_excel(file),
stop("Invalid file; Please upload a .csv or .xlsx file")
showNotification("Invalid file. Please upload a .csv or .xlsx file.", type = "error")
)
df_name <- file_path_sans_ext(input$datafile$name)
session$userData[[df_name]] <- df
Expand Down Expand Up @@ -445,6 +455,18 @@ batchLLM_shiny <- function() {
}
})

observeEvent(input$datafile, {
req(input$datafile)
df <- selected_data()
if (!is.null(df) && ncol(df) > 0) {
updatePickerInput(
session = session,
inputId = "col_name",
selected = names(df)[1]
)
}
})

metadata_trigger <- reactiveVal(0)

current_metadata <- reactive({
Expand Down Expand Up @@ -472,6 +494,16 @@ batchLLM_shiny <- function() {
observeEvent(input$run_batchLLM, {
req(input$df_name, input$col_name)

if (is.null(input$col_name) || input$col_name == "") {
showNotification("Please select a column from the dataset.", type = "error")
return()
}

if (is.null(input$batch_size) || input$batch_size < 1) {
showNotification("Please enter a valid batch size (> 1).", type = "error")
return()
}

if (is.null(input$prompt) || trimws(input$prompt) == "") {
showNotification("Please enter a system prompt.", type = "error")
return()
Expand Down Expand Up @@ -521,6 +553,7 @@ batchLLM_shiny <- function() {
prompt = input$prompt,
batch_delay = input$toggle_delay,
batch_size = input$batch_size,
extract_XML = as.logical(input$extract_XML),
model = config$model,
temperature = config$temperature,
max_tokens = config$max_tokens,
Expand Down Expand Up @@ -549,7 +582,14 @@ batchLLM_shiny <- function() {

output$data_results <- renderDataTable(
{
data_to_show <- if (!is.null(result())) result() else selected_data()
data_to_show <- if (!is.null(input$datafile)) {
selected_data()
} else if (!is.null(result())) {
result()
} else {
selected_data()
}

create_exportable_datatable(data_to_show, "data")
},
server = FALSE
Expand Down
2 changes: 1 addition & 1 deletion man/batchLLM.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/claudeR.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ Batch process large language model (LLM) text completions by looping across the

## 📦 Installation

CRAN:
Production (CRAN):

``` r
install.packages("batchLLM")
```

GitHub:
Development (GitHub):

``` r
install.packages("devtools")
Expand Down

0 comments on commit 66d7ecb

Please sign in to comment.