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

Easily link elmer chat object with a shinychat UI instance #14

Open
gadenbuie opened this issue Nov 21, 2024 · 0 comments
Open

Easily link elmer chat object with a shinychat UI instance #14

gadenbuie opened this issue Nov 21, 2024 · 0 comments

Comments

@gadenbuie
Copy link

I originally opened this in tidyverse/ellmer#169 but think shinychat is better positioned to solve the problem.

In many cases, shinychat users will want to directly hook up the chat UI with the chat instance and keep the history in sync in both. In particular, I'd like to see the step from using elmer alone in scripts to using it in Shiny be much smaller.

I'll repeat from the original issue below:

In script or interactive environments, the elmer's chat$chat() interface is very friendly:

library(elmer)

chat <- elmer::chat_openai(model = "gpt-4o")

chat$chat(
  "What photographic choices were made here, and why do you think the photographer chose them?",
  content_image_file("photo.jpg")
)

chat$chat("Come up with an artsy, pretentious, minimalistic, abstract title for this photo.")

But when combined with Shiny there's at least the minimal boilerplate of hooking up an observer for input${id}_user_input, and the boilerplate picks up when you start doing some server-side interaction with the chat messages. Here's an example app where the above turns are handled via action buttons, but the user can still talk with the model

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

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

server <- function(input, output, session) {
  chat <- chat_openai(
    model = "gpt-4o",
  )

  observeEvent(input$chat_user_input, {
    stream <- chat$stream_async(input$chat_user_input)
    chat_append("chat", stream)
  })
  
  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)
    )
  })

  observeEvent(input$ask_follow_up, {
    q <- "Come up with an artsy, pretentious, minimalistic, abstract title for this photo."

    chat_append_message("chat", list(role = "user", content = q))
    chat_append("chat", chat$stream_async(q))
  })
}

shinyApp(ui, server)
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

1 participant