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

Added a shiny app example in streaming-async.Rmd using chat_async #131

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

Conversation

adisarid
Copy link

Added shiny app example for chat_async usage

Added shiny app example for chat_async usage
Removed redundant use of `future` library call
library(shinyjs)

# Initialize chat
chat <- chat_openai(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Definitely should not put the chat object at this scope, should be inside the observeEvent

@jcheng5
Copy link
Collaborator

jcheng5 commented Oct 23, 2024

Bit simpler version? input_task_button shows simple progress and also prevents double-clicking. And I got rid of the explicit error handling, I think Shiny's built-in behavior is enough.

library(shiny)
library(bslib)
library(elmer)
library(promises)

ui <- page_sidebar(
  title = "Interactive chat with async",
  sidebar = sidebar(
    title = "Controls",
    useShinyjs(),
    textInput("user_query", "Enter query:"),
    input_task_button("ask_chat", label = "Ask the chat")
  ),
  card(
    card_header("The chat's response"),
    uiOutput("chat_response")
  )
)

server <- function(input, output) {
  output$chat_response <- renderUI({
    # Start the chat fresh each time, as the UI is not a multi-turn conversation
    chat <- chat_openai(
      system_prompt = "You like chatting about star trek, mostly TNG and onwards (not TOS). Answers should be concise and star trek inspired."
    )
    # Asynchronously get the (Markdown) results and render to HTML
    chat$chat_async("Answer this question:", input$user_query) %...>% markdown()
  }) |> bindEvent(input$ask_chat)
}

shinyApp(ui = ui, server = server)

Incorporated changes suggested by @jcheng5 moved chat init to within server function and using the `input_task_button` instead of previous shinyjs.
@adisarid
Copy link
Author

Great suggestions!, I have updated the PR accordingly. I have also removed the useShinyjs from the ui (it is not needed after your update), thanks.

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.

2 participants