Skip to content

Commit

Permalink
Make it easy to send a plot (#37)
Browse files Browse the repository at this point in the history
And include an example using it to generate some alt-text.

Fixes #6
  • Loading branch information
hadley authored Sep 24, 2024
1 parent 986cedd commit b3dedb3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

S3method(print,Chat)
export(content_image_file)
export(content_image_plot)
export(content_image_url)
export(create_tool_metadata)
export(new_chat_openai)
Expand Down
27 changes: 27 additions & 0 deletions R/content.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
#' content_image_url("https://www.r-project.org/Rlogo.png"),
#' content_image_file(system.file("httr2.png", package = "elmer"))
#' )
#'
#' plot(waiting ~ eruptions, data = faithful)
#' chat <- new_chat_openai(echo = TRUE)
#' chat$chat(
#' "Describe this plot in one paragraph, as suitable for inclusion in
#' alt-text. You should briefly describe the plot type, the axes, and
#' 2-5 major visual patterns.",
#' content_image_plot()
#' )
content_image_url <- function(url, detail = c("auto", "low", "high")) {
# TODO: Allow vector input?
check_string(url)
Expand Down Expand Up @@ -106,6 +115,24 @@ content_image_file <- function(path, content_type = "auto", resize = "low") {
content_image_url(data_uri)
}

#' @rdname content_image_url
#' @export
#' @param width,height Width and height in pixels.
content_image_plot <- function(width = 768, height = 768) {
plot <- recordPlot()
old <- dev.cur()

path <- tempfile("elmer-plot-", fileext = ".png")
defer(unlink(path))

png(path, width = width, height = height)
replayPlot(plot)
dev.off()

dev.set(old)

content_image_file(path, "image/png", resize = "high")
}

# Define allowed types - add new types here in the future
allowed_input_types <- c("text", "image_url")
Expand Down
5 changes: 5 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ key_get <- function(name, error_call = caller_env()) {
key_exists <- function(name) {
!identical(Sys.getenv(name), "")
}

defer <- function(expr, env = caller_env(), after = FALSE) {
thunk <- as.call(list(function() expr))
do.call(on.exit, list(thunk, TRUE, after), envir = env)
}
14 changes: 14 additions & 0 deletions man/content_image_url.Rd

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

0 comments on commit b3dedb3

Please sign in to comment.