Nothing
#' Unload a model from memory via REST API
#'
#' @param model Character. Unique identifier (\code{instance_id}) of the model
#' instance to unload. Must be one name, given as a single string. The
#' string must be valid in its declared encoding and not marked `"bytes"`.
#' A class, names, and the S4 bit are removed before the name is sent.
#' @param host Character. The host address of the local server. Defaults to
#' "http://localhost:1234".
#' @param token Character or `NULL`. An API token for a server that requires
#' authentication. `NULL` reads the `rlmstudio.token` option and then the
#' `RLMSTUDIO_API_TOKEN` environment variable. See [rlmstudio_token].
#' @param ... Additional arguments passed to the API request body.
#'
#' @note If you have loaded multiple instances of the same model using
#' \code{force = TRUE} in \code{lms_load()}, the server assigns them unique
#' instance identifiers (e.g., \code{"google/gemma-3-1b"} and
#' \code{"google/gemma-3-1b:2"}). Passing the base model name to
#' \code{lms_unload()} will only unload the primary instance. To unload
#' duplicate instances, you must provide their exact \code{instance_id}, or
#' use \code{lms_unload_all()} to clear everything.
#'
#' @seealso [LM Studio Unload Model
#' API](https://lmstudio.ai/docs/developer/rest/unload)
#'
#' @return Invisibly returns a character string representing the unloaded
#' \code{instance_id} upon success. It is a plain string, with no class,
#' names, or S4 bit.
#'
#' @inheritSection rlmstudio-conditions Server not running
#' @inheritSection rlmstudio-conditions API failure
#'
#' @export
#'
#' @examples
#' \dontrun{
#' lms_server_start(port = 1234)
#' lms_download("google/gemma-3-1b")
#' lms_load("google/gemma-3-1b")
#'
#' # Unload a single specific model
#' lms_unload("google/gemma-3-1b")
#' }
lms_unload <- function(
model,
host = "http://localhost:1234",
...,
token = NULL
) {
model <- rlm_check_id(model, "model")
stop_if_no_server(host)
# Build body and merge extra args from dots
body <- utils::modifyList(list(instance_id = model), list(...))
rlm_progress_step(
msg = "Unloading model: {.val {model}}...",
msg_done = "Model {.val {model}} unloaded successfully."
)
req <- lms_client(host, token = token) |>
httr2::req_url_path("api/v1/models/unload") |>
rlm_req_body(body) |>
httr2::req_error(is_error = \(resp) FALSE)
resp <- httr2::req_perform(req)
if (httr2::resp_status(resp) == 200) {
return(invisible(model))
}
rlm_abort_api(resp, "API Unload Failed", request_sends_token(req))
}
#' Unload all models from memory
#'
#' Retrieves a list of all currently loaded models and unloads them one by one.
#'
#' @param host Character. The host address of the local server. Defaults to
#' "http://localhost:1234".
#' @param token Character or `NULL`. An API token for a server that requires
#' authentication. `NULL` reads the `rlmstudio.token` option and then the
#' `RLMSTUDIO_API_TOKEN` environment variable. See [rlmstudio_token].
#' @param ... Additional arguments passed to the API request body for each
#' unload request.
#'
#' @seealso \code{\link{lms_unload}}
#'
#' @return Invisibly returns a character vector of the \code{instance_id}s that
#' were successfully unloaded. If no models were currently loaded, it
#' invisibly returns \code{NULL}.
#'
#' @details
#' This function calls [list_models()] to find the loaded instances, then calls
#' [lms_unload()] once for each one. It raises `rlmstudio_no_server` itself,
#' before the first call. It can raise `rlmstudio_api_error` through
#' [list_models()] and through [lms_unload()]. It can raise
#' `rlmstudio_bad_response` through [list_models()].
#'
#' @inheritSection rlmstudio-conditions Server not running
#' @inheritSection rlmstudio-conditions API failure
#' @inheritSection rlmstudio-conditions Malformed response
#' @inheritSection rlmstudio-conditions Malformed model list
#'
#' @export
#'
#' @examples
#' \dontrun{
#' lms_server_start(port = 1234)
#' lms_download("google/gemma-3-1b")
#' lms_load("google/gemma-3-1b")
#'
#' # Unload all currently loaded models to clear VRAM
#' lms_unload_all()
#' }
lms_unload_all <- function(host = "http://localhost:1234", ..., token = NULL) {
stop_if_no_server(host)
# Fetch currently active models
active_models <- list_models(
loaded = TRUE,
detailed = TRUE,
quiet = TRUE,
host = host,
token = token
)
# Check if there is anything to unload
if (nrow(active_models) == 0 || is.null(active_models$loaded_instances)) {
rlm_alert_info("No models are currently loaded.")
return(invisible(NULL))
}
# list_models() has checked that every loaded instance carries an `id` that
# is a string with content, so the ids are read by that name alone.
loaded_keys <- unlist(lapply(active_models$loaded_instances, function(x) {
x[["id"]]
}))
if (length(loaded_keys) == 0) {
rlm_alert_info("No models are currently loaded.")
return(invisible(NULL))
}
rlm_alert_info(
"Found {length(loaded_keys)} loaded model instance{?s}. Unloading now..."
)
# Loop through and unload each specific instance
for (instance_id in loaded_keys) {
lms_unload(model = instance_id, host = host, ..., token = token)
}
rlm_alert_success("All models unloaded successfully.")
return(invisible(loaded_keys))
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.