Skip to content
Merged

test #63

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ docs
tests/testthat/.rds
t_dm_slide_FAS.rds
tmp.pptx

# Keys
.*_KEY
DEEPSEEK_KEY
PORTKEY_KEY
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ tests/testthat/Rplots.pdf
tests/testthat/t_dm_output.rds
tests/testthat/srep.pptx
docs/
*_KEY
PORTKEY_KEY
DEEPSEEK_KEY
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ S3method(to_flextable,default)
S3method(to_flextable,dgtsummary)
S3method(to_flextable,dlisting)
S3method(to_flextable,gtsummary)
export(adding_ai_footnotes)
export(autoslider_dose_format)
export(autoslider_error)
export(autoslider_format)
Expand Down Expand Up @@ -37,6 +38,8 @@ export(g_vs_slide)
export(generate_output)
export(generate_outputs)
export(generate_slides)
export(get_ellmer_chat)
export(get_prompt_list)
export(gt_t_dm_slide)
export(l_ae_slide)
export(lyt_to_side_by_side)
Expand Down
71 changes: 71 additions & 0 deletions R/ai.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
get_deepseek_key <- function(filename = "DEEPSEEK_KEY"){
scan(filename, what = "character", sep = NULL)
}

get_portkey_key <- function(filename = "PORTKEY_KEY"){
scan(filename, what = "character", sep = NULL)
}

get_system_prompt <- function(text = "you are a Clinical data scientist expert"){
return(text)
}

#'
#' @export
#'
get_ellmer_chat <- function(platform = "deepseek",
base_url = "https://api.deepseek.com",
api_key = get_deepseek_key(),
model = "deepseek-chat") {
chat <- NULL
if (platform == "deepseek") {
chat <- ellmer::chat_deepseek(
system_prompt = get_system_prompt(),
base_url = base_url,
api_key = api_key,
model=model)
} else if (platform == "galileo"){
chat <- chat_portkey(system_prompt = get_system_prompt(),
base_url = base_url,
api_key = api_key,
model = model)
}
return(chat)
}

#' @export
get_prompt_list <- function(filename){
prompt <- yaml::read_yaml(filename, eval.expr = TRUE)
structure(
.Data = prompt,
names = map_chr(prompt, `[[`, "output"),
class = union("spec", class(prompt))
)
}


integrate_prompt <- function(base_prompt, tlg) {
# let's do figures in the future
ret <- gsub("\\{table_text\\}", export_as_txt(tlg), base_prompt)
ret
}

#' @export
adding_ai_footnotes <- function(outputs, prompt_list, platform, base_url, api_key, model){
chat <- get_ellmer_chat(platform, base_url, api_key, model)
names_outputs <- names(outputs)
ret <- lapply(names_outputs, function(output_name) {
output <- outputs[[output_name]]
if (is(output, "autoslider_error")) {
return(output)
}
if (output_name %in% names(prompt_list)){
current_prompt <- integrate_prompt(prompt_list[[output_name]]$prompt, output@tbl)
output@footnotes <- c(output@footnotes, chat$chat(current_prompt)) #gather_ai_feedback()
}
output
})
print(ret)
names(ret) <- names_outputs
return(ret)
}
2 changes: 1 addition & 1 deletion autoslider.core.Rproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Version: 1.0
ProjectId: 05146cdf-6682-48bb-8f33-b4232d5de690
ProjectId: 9f3c33a8-faa3-4d7f-bb54-3995f3ae6b2b

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down
2 changes: 1 addition & 1 deletion inst/prompt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ As a Clinical data scientist expert, here is a demographic summary table from a

{table_text}

Please analyze the 'Race' category in this table. Specifically, compare the distribution of races between the '{group1_name}' column and the '{group2_name}' column.
Please analyze the 'Race' category in this table. Specifically, compare the distribution of races between the Placebo column and the treatment column.

Focus only on describing any noticeable differences or similarities in the counts and percentages for each race listed between these two specific groups, based *directly* on the data presented in the table text provided above.

Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/test_ai.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
test_that("Listing print correctly", {
testthat::skip_if_not_installed("filters")
testthat::skip_if_not_installed("ellmer")

# skip_if_too_deep(1)
filters::load_filters(file.path(
system.file(package = "autoslider.core"),
"filters.yml"
), overwrite = TRUE)

spec_file <- file.path(system.file(package = "autoslider.core"), "spec.yml")

full_spec <- spec_file %>%
read_spec()

outputs <- full_spec %>%
filter_spec(., program %in% c(
"t_dm_slide"
)) %>%
generate_outputs(datasets = testdata) %>%
decorate_outputs(
version_label = NULL,
for_test = TRUE
)
prompt_list <- get_prompt_list(filename = "~/autoslider.core/inst/prompt.yml")
outputs <- adding_ai_footnotes(outputs, prompt_list,
platform = "deepseek",
base_url = "https://api.deepseek.com",
api_key = get_deepseek_key("~/autoslider.core/DEEPSEEK_KEY"),
model = "deepseek-chat")
output_dir <- tempdir()
testthat::expect_output({
outputs %>%
generate_slides(outfile = paste0(output_dir, "/srep.pptx"), t_cpp = 250, t_lpp = 50)
})

testthat::expect_no_error({
outputs %>%
save_outputs(outfolder = output_dir)
})
})