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

Fix: Shiny Chat app crashes when clicking on options button #190

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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") %>%

Check warning

Code scanning / lintr

no visible global function definition for 'request_base' Warning

no visible global function definition for 'request_base'

Check warning

Code scanning / lintr

no visible global function definition for '%>%' Warning

no visible global function definition for '%>%'

Check warning

Code scanning / lintr

no visible global function definition for '%>%' Warning

no visible global function definition for '%>%'

Check warning

Code scanning / lintr

no visible global function definition for '%>%' Warning

no visible global function definition for '%>%'

Check warning

Code scanning / lintr

no visible global function definition for '%>%' Warning

no visible global function definition for '%>%'
httr2::req_perform() %>%
httr2::resp_body_json() %>%
purrr::pluck("data") %>%
purrr::map_chr("id")

models <- models %>%

Check warning

Code scanning / lintr

no visible global function definition for '%>%' Warning

no visible global function definition for '%>%'

Check warning

Code scanning / lintr

no visible global function definition for '%>%' Warning

no visible global function definition for '%>%'

Check warning

Code scanning / lintr

no visible global function definition for '%>%' Warning

no visible global function definition for '%>%'

Check warning

Code scanning / lintr

no visible global function definition for '%>%' Warning

no visible global function definition for '%>%'
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",

Check notice

Code scanning / lintr

Lines should not be more than 80 characters. This line is 84 characters. Note

Lines should not be more than 80 characters. This line is 84 characters.
"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")

Check warning

Code scanning / lintr

no visible global function definition for 'ollama_is_available' Warning

no visible global function definition for 'ollama_is_available'
ollama_list() %>%

Check warning

Code scanning / lintr

no visible global function definition for 'ollama_list' Warning

no visible global function definition for 'ollama_list'

Check warning

Code scanning / lintr

no visible global function definition for '%>%' Warning

no visible global function definition for '%>%'

Check warning

Code scanning / lintr

no visible global function definition for '%>%' Warning

no visible global function definition for '%>%'
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()

Check warning

Code scanning / lintr

no visible global function definition for 'get_available_models_google' Warning

no visible global function definition for 'get_available_models_google'
}

Check notice

Code scanning / lintr

Trailing blank lines are superfluous. Note

Trailing blank lines are superfluous.

Check notice

Code scanning / lintr

Trailing blank lines are superfluous. Note

Trailing blank lines are superfluous.
Loading