Skip to content

Commit

Permalink
Merge pull request #190 from MichelNivard/bug-options-btn
Browse files Browse the repository at this point in the history
Fix: Shiny Chat app crashes when clicking on options button
  • Loading branch information
JamesHWade committed Apr 8, 2024
2 parents 8a65698 + a3533f5 commit 700bb96
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 44 deletions.
6 changes: 5 additions & 1 deletion R/mod_settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ mod_settings_server <- function(id) {
observe({
msg <- glue::glue("Fetching models for {input$service} service...")
showNotification(ui = msg, type = "message", duration = 3, session = session)

cli::cli_alert_info(msg)
models <- tryCatch(
{
get_available_models(input$service)
Expand All @@ -151,12 +151,15 @@ mod_settings_server <- function(id) {
type = "error",
session = session
)

cli::cli_alert_danger(e$message)
return(NULL)
}
)

if (length(models) > 0) {
showNotification(ui = "Got models!", duration = 3, type = "message", session = session)
cli::cli_alert_success("Got models!")

default_model <- getOption("gptstudio.model")

Expand All @@ -168,6 +171,7 @@ mod_settings_server <- function(id) {
)
} else {
showNotification(ui = "No models available", duration = 3, type = "error", session = session)
cli::cli_alert_danger("No models available")

updateSelectInput(
session = session,
Expand Down
113 changes: 70 additions & 43 deletions R/models.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,75 @@
#' @export
#'
#' @examples
#' get_available_endpoints()
#' get_available_models()
get_available_models <- function(service) {
if (service == "openai") {
models <-
request_base("models") %>%
req_perform() %>%
resp_body_json() %>%
purrr::pluck("data") %>%
purrr::map_chr("id")

models <- models %>%
stringr::str_subset("^gpt") %>%
stringr::str_subset("instruct", negate = TRUE) %>%
stringr::str_subset("vision", negate = TRUE) %>%
sort()

idx <- which(models == "gpt-3.5-turbo")
models <- c(models[idx], models[-idx])
return(models)
} else if (service == "huggingface") {
c("gpt2", "tiiuae/falcon-7b-instruct", "bigcode/starcoderplus")
} else if (service == "anthropic") {
c(
"claude-3-opus-20240229", "claude-3-sonnet-20240229", "claude-3-haiku-20240229",
"claude-2.1", "claude-instant-1.2"
)
} else if (service == "azure_openai") {
"Using ENV variables"
} else if (service == "perplexity") {
c(
"sonar-small-chat", "sonar-small-online", "sonar-medium-chat",
"sonar-medium-online", "codellama-70b-instruct", "mistral-7b-instruct",
"mixtral-8x7b-instruct"
)
} else if (service == "ollama") {
if (!ollama_is_available()) stop("Couldn't find ollama in your system")
ollama_list() %>%
purrr::pluck("models") %>%
purrr::map_chr("name")
} else if (service == "cohere") {
c("command", "command-light", "command-nightly", "command-light-nightly")
} else if (service == "google") {
get_available_models_google()
}
list_available_models(new_gptstudio_service(service))
}

list_available_models <- function(service) {
UseMethod("list_available_models")
}

new_gptstudio_service <- function(service_name = character()) {
stopifnot(rlang::is_scalar_character(service_name))
class(service_name) <- c(service_name, "gptstudio_service")

service_name
}

list_available_models.openai <- function(service) {
models <-
request_base("models") %>%
httr2::req_perform() %>%
httr2::resp_body_json() %>%
purrr::pluck("data") %>%
purrr::map_chr("id")

models <- models %>%
stringr::str_subset("^gpt") %>%
stringr::str_subset("instruct", negate = TRUE) %>%
stringr::str_subset("vision", negate = TRUE) %>%
sort()

idx <- which(models == "gpt-3.5-turbo")
models <- c(models[idx], models[-idx])
return(models)
}

list_available_models.huggingface <- function(service) {
c("gpt2", "tiiuae/falcon-7b-instruct", "bigcode/starcoderplus")
}

list_available_models.anthropic <- function(service) {
c(
"claude-3-opus-20240229", "claude-3-sonnet-20240229", "claude-3-haiku-20240229",
"claude-2.1", "claude-instant-1.2"
)
}

list_available_models.azure_openai <- function(service) {
"Using ENV variables"
}

list_available_models.perplexity <- function(service) {
c("sonar-small-chat", "sonar-small-online", "sonar-medium-chat",
"sonar-medium-online", "codellama-70b-instruct", "mistral-7b-instruct",
"mixtral-8x7b-instruct")
}

list_available_models.ollama <- function(service) {
if (!ollama_is_available()) stop("Couldn't find ollama in your system")
ollama_list() %>%
purrr::pluck("models") %>%
purrr::map_chr("name")
}

list_available_models.cohere <- function(service) {
c("command", "command-light", "command-nightly", "command-light-nightly")
}

list_available_models.google <- function(service) {
get_available_models_google()
}


0 comments on commit 700bb96

Please sign in to comment.