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 5 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion vignettes/streaming-async.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,76 @@ chat$chat_async("How's your day going?") %...>% print()
#> I'm just a computer program, so I don't have feelings, but I'm here to help you with any questions you have.
```

TODO: Shiny example
#### Shiny example

The following shiny app uses `chat_async` to query the OpenAI API. The server function shows a loading element (spinner) while it is waiting for the API to return the answer. Then the recent response is displayed.
The `%...>%` operator from the `promises` package is used to push the asynchronous chat response and render it, once it is ready. It also hides the loading element.

```
library(shiny)
library(bslib)
library(elmer)

Choose a reason for hiding this comment

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

please update this to library(ellmer)

library(promises)
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

system_prompt = "You like chatting about star trek, mostly TNG and onwards (not TOS). Answers should be concise and star trek inspired."
)

# The user interface is simple
ui <- page_sidebar(
title = "Interactive chat with async",
sidebar = sidebar(
title = "Controls",
useShinyjs(),
textInput("user_query", "Enter query:"),
actionButton("ask_chat", label = "Ask the chat")
),
card(
card_header("The chat's response"),
hidden(
div("I'm thinking...",
div(class = "spinner-border text-primary", role = "status",
span(class = "sr-only")),
id = "loading")),
uiOutput("chat_response")
)
)

server <- function(input, output) {

# Once the user requests chat gpt output:
observeEvent(input$ask_chat, {

# Do something to show you're thinking
showElement("loading")

# Call the chat_async function
result_async <- chat$chat_async("Answer this question:",
input$user_query)

# Using the promises notation, this will run once the chat result comes through
result_async %...>% {
# Update the modal once the result is available
hideElement("loading") # results are in - hide the "loading".
output$chat_response <- renderUI({
markdown(.) # The `.` is the chat result, markdown is just used to turn content into html.
})
} %...!% {
# Error handling
output$chat_response <- renderUI({
p("Something went wrong... :(")
})
}
})
}

# Run the app
shinyApp(ui = ui, server = server)
```

TODO: Extend example to stream_async, simplify example.

### Asynchronous streaming

Expand Down