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

Add method(s) for appending a turn to the chat #168

Open
gadenbuie opened this issue Nov 20, 2024 · 1 comment
Open

Add method(s) for appending a turn to the chat #168

gadenbuie opened this issue Nov 20, 2024 · 1 comment

Comments

@gadenbuie
Copy link

When using elmer with shinychat, users may have to construct both the message for shinychat::chat_append_message() and then separately call chat$stream() with the same message, or parts of it.

Here's an example app:

library(elmer)
library(shiny)
library(shinychat)

ui <- bslib::page_fluid(
  chat_ui("chat"),
  actionButton("ask_photo", "Ask question with image")
)

server <- function(input, output, session) {
  chat <- chat_openai(
    model = "gpt-4o",
  )
  
  observeEvent(input$ask_photo, {
    turn <- Turn(
      "user",
      list(
        ContentText("What photographic choices were made here, and why do you think the photographer chose them?"),
        content_image_file("photo.jpg")
      )
    )

    chat_append_message(
      "chat",
      list(
        role = "user",
        content = turn@text
      )
    )

    chat_append(
      "chat",
      chat$stream_async(!!!turn@contents)
    )
  })
}

shinyApp(ui, server)

It'd be helpful if there were a way to take the constructed turn and pass it directly to a chat method. Ideally even multiple turns at once to support advanced use cases.

@hadley
Copy link
Member

hadley commented Nov 21, 2024

I wonder if there's some way to make something like this work:

server <- function(input, output, session) {
  chat <- chat_openai(model = "gpt-4o")
  
  observeEvent(input$ask_photo, {
    stream <- chat$stream_async(
      ContentText("What photographic choices were made here, and why do you think the photographer chose them?"),
      content_image_file(system.file("httr2.png", package = "elmer"))
    )

    chat_append_message("chat", chat$last_turn("user"))
    chat_append_message("chat", stream)
  })
}

Currently that doesn't work because the user turn is only set after the stream is completed.

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

No branches or pull requests

2 participants