Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(contents_text): Add contents_*() helper functions #170

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

gadenbuie
Copy link

@gadenbuie gadenbuie commented Nov 20, 2024

Fixes #167

Adds contents_text(), contents_markdown() and contents_html() generics that can be applied to Turn and Content objects.

turns <- list(
  user_turn("What's this image?", content_image_url("https://placehold.co/200x200")),
  Turn("assistant", "It's a placeholder image.")
)

lapply(turns, contents_text)
#> [[1]]
#> [1] "What's this image?"
#> 
#> [[2]]
#> [1] "It's a placeholder image."
lapply(turns, contents_markdown)
#> [[1]]
#> [1] "What's this image?\n\n![](https://placehold.co/200x200)"
#> 
#> [[2]]
#> [1] "It's a placeholder image."
contents_html(turns[[1]])
#> [1] "<p>What's this image?</p>\n\n<img src=\"https://placehold.co/200x200\">"

Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a reasonable idea and implementation to me. You just need to fix the build failure and add some basic tests.

R/turns.R Show resolved Hide resolved
_pkgdown.yml Outdated Show resolved Hide resolved
R/content.R Outdated Show resolved Hide resolved
R/content.R Outdated Show resolved Hide resolved
#' contents_html(turns[[1]])
#' }
#'
#' @param content The [Turn] or [Content] object to be converted into text.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you pass turns?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added a method for contents_markdown() for Chat instances. I see a lot of utility in that particular output format and less utility in the HTML or text variants. That said I'd be happy to add those if you want.

turns <- list(
  user_turn("What's this image?", content_image_url("https://placehold.co/200x200")),
  Turn("assistant", "It's a placeholder image.")
)

chat <- Chat$new(Provider("https://example.com"), turns = turns)
cat(contents_markdown(chat))
#> ## User
#> 
#> What's this image?
#> 
#> ![](https://placehold.co/200x200)
#> 
#> ## Assistant
#> 
#> It's a placeholder image.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related: is there a way to create a chat_echo() (or maybe chat_repeater()) chat instance that just returns the user input and doesn't call any APIs? Carson made an example like this for Shiny and I've found it to be very useful for debugging and examples when I'm not ready for real model usage yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be pretty straightforward with a new provider.

@gadenbuie
Copy link
Author

@hadley this is ready for another look. Sorry for the commit noise, I only just now realized I wasn't able to run checks locally because I had out-of-date credentials. Of course when I finally got to ✅ in the last commit there was an unrelated CI failure and I can't request a rerun.

@cpsievert
Copy link
Contributor

cpsievert commented Nov 26, 2024

I love this idea in general, but also had a couple thoughts on this PR:

  1. It'd be nice if these content extractors had the ability to get "additional" content (i.e., the stuff you get with $chat(echo="all")), mainly for debugging (but also educational) purposes.
  2. It feels like the main way people would want to use this is to generate an md/html file of the whole conversation? For that reason, instead of generics, I think for chatlas I might do something like chat.export(filename="output.html", content="all"), which might internally call a new turn.get_content("all").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Additional generics to convert turns and turn contents into other text-based formats
3 participants