-
Notifications
You must be signed in to change notification settings - Fork 47
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
adisarid
wants to merge
5
commits into
tidyverse:main
Choose a base branch
from
adisarid:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
819358b
Update streaming-async.Rmd
adisarid 2dd2d47
Update streaming-async.Rmd
adisarid 871f212
Merge branch 'hadley:main' into patch-1
adisarid cac3443
Update streaming-async.Rmd
adisarid c352a04
Updated package name to `ellmer`
adisarid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
library(promises) | ||
library(shinyjs) | ||
|
||
# Initialize chat | ||
chat <- chat_openai( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
)