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 14 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.

getter = function(self) {
paste0(unlist(lapply(self@contents, contents_text)), collapse = "")
}
getter = function(self) contents_text(self)
Copy link
Member

Choose a reason for hiding this comment

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

You didn't change anything to make this work, right? You just noticed that we can now simplify the implementation?

Copy link
Author

Choose a reason for hiding this comment

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

Right, I moved the implementation that was here into method(contents_text, Turn).

_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.

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
2 participants