Skip to content

Commit 4110b2a

Browse files
committed
transition to tidyverse/ellmer#735 and content_image_file()
1 parent 36e471d commit 4110b2a

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

DESCRIPTION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Imports:
2828
yaml
2929
Depends:
3030
R (>= 3.5)
31+
Remotes:
32+
tidyverse/ellmer#735
3133
LazyData: true
3234
URL: https://github.com/simonpcouch/bluffbench
3335
BugReports: https://github.com/simonpcouch/bluffbench/issues

R/bluff-solver.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ bluff_solver <- function(inputs, ..., solver_chat) {
3232
for (i in seq_along(inputs)) {
3333
input <- inputs[[i]]
3434

35-
predictive:::run_r_code(input$setup)
35+
run_r_code(input$setup)
3636

3737
ch_i <- solver_chat$clone()
3838
ch_i$register_tool(tool_create_ggplot)
@@ -41,7 +41,7 @@ bluff_solver <- function(inputs, ..., solver_chat) {
4141

4242
res[[i]] <- ch_i
4343

44-
predictive:::run_r_code(input$teardown)
44+
run_r_code(input$teardown)
4545

4646
cli::cli_progress_update()
4747
}

R/tool-create-plot.R

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
1-
#' Create a ggplot visualization tool
1+
run_ggplot_code <- function(code) {
2+
result <- tryCatch(
3+
run_r_code(code),
4+
error = function(e) {
5+
return(ellmer::ContentToolResult(error = conditionMessage(e)))
6+
}
7+
)
8+
9+
if (inherits(result, "ggplot")) {
10+
temp_file <- tempfile(fileext = ".png")
11+
ggplot2::ggsave(temp_file, plot = result, width = 7, height = 5, dpi = 150)
12+
return(ellmer::content_image_file(temp_file))
13+
}
14+
15+
ellmer::ContentToolResult(error = "Code did not return a ggplot object")
16+
}
17+
18+
run_r_code <- function(code) {
19+
eval(parse(text = code), envir = .GlobalEnv)
20+
}
21+
22+
#' ggplot visualization tool
223
#'
3-
#' An ellmer tool that wraps `predictive:::run_r_code()` to allow models to
4-
#' create ggplot visualizations. The tool is named "create_ggplot" and accepts
5-
#' R code that returns a ggplot object. This intentionally presents a narrow
6-
#' interface to discourage models from exploring data with arbitrary code.
24+
#' An ellmer tool that evaluates R code to create ggplot visualizations.
25+
#' The tool is named "create_ggplot" and accepts R code that returns a ggplot
26+
#' object. This intentionally presents a narrow interface to discourage models
27+
#' from exploring data with arbitrary code.
728
#'
829
#' @export
930
tool_create_ggplot <- ellmer::tool(
10-
predictive:::run_r_code,
31+
run_ggplot_code,
1132
name = "create_ggplot",
1233
description = "Create a ggplot visualization from the provided R code.",
1334
arguments = list(
1435
code = ellmer::type_string(
15-
"R code that begins with library(ggplot2) and then a call to the `ggplot()` function. Do _not_ run any code via this tool that could cause side effects in the global environment such as calling `data()` on an object."
36+
"R code that begins with library(ggplot2) and then a call to the `ggplot()` function. This code runs in the global environment--do _not_ run any code via this tool that could cause side effects in the global environment such as calling `data()` on an object."
1637
)
1738
)
1839
)

0 commit comments

Comments
 (0)