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

Add support for viewer-based credentials for the Cortex chatbot #150

Closed
wants to merge 2 commits into from

Conversation

atheriel
Copy link
Collaborator

@atheriel atheriel commented Nov 8, 2024

Note: this is stacked on top of #145. Ignore the first commit.

This commit ports odbc's recently-added support for "viewer-based credentials" on Posit Connect to elmer. Specifically, passing a Shiny session object to chat_cortex() now allows Cortex chatbots to use the viewer's Snowflake credentials on Connect rather than requiring a service account or the publisher to embed their personal credentials.

For example:

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

stage <- "CORTEX_ANALYST_DEMO.REVENUE_TIMESERIES.RAW_DATA"
model_file <- "revenue_timeseries.yaml"

ui <- bslib::page_fluid(chat_ui("chat", "What questions can I ask?"))

server <- function(input, output, session) {
  chat <- chat_cortex(
    model_file = glue::glue("@{stage}/{model_file}"),
    account = "myaccount",
    session = session
  )

  chat_append("chat", glue::glue(
    "❄️ Hi, I'm the [Snowflake Cortex Analyst](https://docs.snowflake.com/en/user-guide/snowflake-cortex/cortex-analyst)!
I can answer questions about the `{model_file}` data model or generate SQL
queries for you."
  ))

  observeEvent(input$chat_user_input, {
    chat_append("chat", chat$stream_async(input$chat_user_input))
  })
}

shinyApp(ui, server)

Unit tests that cover basic functionality are included, but there is no integration test per se that uses a real Connect server.

This commit drops all of the Snowflake-specific authentication logic in
favour of using the new `snowflakeauth` package. This has a big
advantage in that it works with Snowflake connections and credentials
created by the Snowflake Python packages and their CLI.

However, this is a breaking change in that (1) some environment
variables that previously furnished credentials no longer do; and (2)
the signature of `chat_cortex()` has changed. The reason to drop the
environment variable support is that these specific environment
variables are not used by Snowflake's Python tooling -- so I think this
is the right choice.

Existing unit tests do cover regressions in the Authorization headers we
pass to Snowflake, though I did have to modify them a bit to account for
the API changes.

Signed-off-by: Aaron Jacobs <aaron.jacobs@posit.co>
This commit ports `odbc`'s recently-added support for "viewer-based
credentials" on Posit Connect to `elmer`. Specifically, passing a Shiny
session object to `chat_cortex()` now allows Cortex chatbots to use the
*viewer's* Snowflake credentials on Connect rather than requiring a
service account or the publisher to embed their personal credentials.

For example:

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

    stage <- "CORTEX_ANALYST_DEMO.REVENUE_TIMESERIES.RAW_DATA"
    model_file <- "revenue_timeseries.yaml"

    ui <- bslib::page_fluid(chat_ui("chat", "What questions can I ask?"))

    server <- function(input, output, session) {
      chat <- chat_cortex(
        model_file = glue::glue("@{stage}/{model_file}"),
        account = "myaccount",
        session = session
      )

      chat_append("chat", glue::glue(
        "❄️ Hi, I'm the [Snowflake Cortex Analyst](https://docs.snowflake.com/en/user-guide/snowflake-cortex/cortex-analyst)!
    I can answer questions about the `{model_file}` data model or generate SQL
    queries for you."
      ))

      observeEvent(input$chat_user_input, {
        chat_append("chat", chat$stream_async(input$chat_user_input))
      })
    }

    shinyApp(ui, server)

Unit tests that cover basic functionality are included, but there is no
integration test per se that uses a real Connect server.

Signed-off-by: Aaron Jacobs <aaron.jacobs@posit.co>
@atheriel
Copy link
Collaborator Author

Superseded by #183.

@atheriel atheriel closed this Nov 27, 2024
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.

1 participant