-
Notifications
You must be signed in to change notification settings - Fork 3
Azure OpenAI OAuth - R Shiny chat app #202
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
34871de
initial version
joshyam-k 7983550
adjust tags and requiredFeatures
joshyam-k d293074
adjust naming conventions to better reflect the purpose of the app
joshyam-k 271f481
surface setup screen if no oauth integration is configured
joshyam-k 85bcecd
fix link and local dev setup
joshyam-k ed34cc6
sync main
joshyam-k 85d2e4a
revamp app and add more extensive readme
joshyam-k 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 hidden or 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
source("renv/activate.R") |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
/.posit/ |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# R Shiny Chat App | ||
|
||
An R Shiny app that uses `ellmer` and `shinychat` to create an interactive chat interface that answers questions about the `palmerpenguins` dataset. The app demonstrates the OAuth credential handoff made possible by the Azure OpenAI OAuth integration which gives the shiny app access to Azure OpenAI resources. | ||
|
||
# Setup | ||
|
||
An Azure OpenAI OAuth Integration must be configured by the Posit Connect administrator using application specific fields from the Azure administrator. | ||
|
||
Alternatively, if testing this shiny app locally, the environment variable `AZURE_OPENAI_API_KEY` must be set. | ||
|
||
# Usage | ||
|
||
Deploy the app to Connect. | ||
|
||
**Note**: | ||
|
||
Only members of the "Connect" group have been assigned the proper data action role to use the Azure OpenAI `gpt-4o-mini` deployment that this app is set up to interact with. Because of this, the primary purpose of the app is to serve as a blueprint for what utilizing the Azure OpenAI OAuth integration might look like. | ||
|
||
To adapt this shiny app to a different azure endpoint and model deployment, the `deployment_id`, `api_version`, and `endpoint` arguments passed to `ellmer::chat_azure_openai()`, must be respecified. |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
library(shiny) | ||
library(shinychat) | ||
library(ellmer) | ||
library(palmerpenguins) | ||
library(connectapi) | ||
library(ggplot2) | ||
library(bslib) | ||
|
||
data(penguins) | ||
|
||
ui <- bslib::page_fluid( | ||
theme = bslib::bs_theme(bootswatch = "flatly"), | ||
bslib::layout_columns( | ||
col_widths = c(2, 8, 2), | ||
NULL, | ||
bslib::card( | ||
bslib::card_header("Palmer Penguins Chat Assistant"), | ||
shinychat::chat_ui("chat", height = "300px"), | ||
width = "100%", | ||
class = "shadow" | ||
), | ||
NULL | ||
) | ||
) | ||
|
||
server <- function(input, output, session) { | ||
|
||
# Get Connect OAuth credentials using connectapi | ||
get_oauth_credentials <- function() { | ||
|
||
if (Sys.getenv("POSIT_PRODUCT") == "CONNECT") { | ||
|
||
client <- connectapi::connect() | ||
|
||
user_session_token <- session$request$HTTP_POSIT_CONNECT_USER_SESSION_TOKEN | ||
oauth_response <- connectapi::get_oauth_credentials(client, user_session_token) | ||
credentials <- list(Authorization = paste("Bearer", oauth_response$access_token)) | ||
joshyam-k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} else { | ||
|
||
# Local development mode | ||
api_key <- Sys.getenv("AZURE_OPENAI_API_KEY") | ||
if (api_key != "") { | ||
credentials <- list("api-key" = api_key) | ||
} else { | ||
credentials <- NULL | ||
} | ||
|
||
} | ||
|
||
credentials | ||
|
||
} | ||
|
||
# Create Azure OpenAI chat function | ||
penguin_chat <- function() { | ||
|
||
credentials <- get_oauth_credentials() | ||
|
||
# Create a custom chat function for Azure OpenAI | ||
azure_chat <- ellmer::chat_azure_openai( | ||
deployment_id = "gpt-4o-mini", | ||
api_version = "2024-12-01-preview", | ||
endpoint = "https://8ul4l3wq0g.openai.azure.com", | ||
joshyam-k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
credentials = credentials, | ||
system_prompt = paste0( | ||
"You are a data assistant helping with the Palmer Penguins dataset. ", | ||
"The dataset contains measurements for Adelie, Chinstrap, and Gentoo penguins observed on islands in the Palmer Archipelago. ", | ||
"Answer questions about the dataset concisely and accurately. ", | ||
"The dataset structure is: \n", paste(capture.output(str(penguins)), collapse = "\n") | ||
) | ||
) | ||
} | ||
|
||
shiny::observeEvent(input$chat_user_input, { | ||
|
||
current_chat <- penguin_chat() | ||
|
||
stream <- current_chat$stream_async(input$chat_user_input) | ||
shinychat::chat_append("chat", stream) | ||
|
||
|
||
}) | ||
|
||
} | ||
|
||
shiny::shinyApp(ui, server) |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
library/ | ||
local/ | ||
cellar/ | ||
lock/ | ||
python/ | ||
sandbox/ | ||
staging/ |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.