From 4d0df7883e7859a63ae324d1f0bc477ad9a9d219 Mon Sep 17 00:00:00 2001 From: JBGruber Date: Sun, 21 Apr 2024 11:19:15 +0200 Subject: [PATCH] adds function `check_model_installed` --- NAMESPACE | 1 + NEWS.md | 4 ++++ R/utils.r | 30 ++++++++++++++++++++++++++++++ man/check_model_installed.Rd | 22 ++++++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 man/check_model_installed.Rd diff --git a/NAMESPACE b/NAMESPACE index f8b151f..1de0f57 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(chat) export(chat_history) +export(check_model_installed) export(copy_model) export(create_model) export(delete_model) diff --git a/NEWS.md b/NEWS.md index 5c7b162..34667a0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# rollama 0.1.0 + +* adds function `check_model_installed` + # rollama 0.0.3 * add option to query several models at once diff --git a/R/utils.r b/R/utils.r index e11ffda..2966a11 100644 --- a/R/utils.r +++ b/R/utils.r @@ -4,3 +4,33 @@ screen_answer <- function(x, model = NULL) { # "{i}" instead of i stops glue from evaluating code inside the answer for (i in pars) cli::cli_text("{i}") } + + +#' Check if one or several models are installed on the server +#' +#' @param model names of one or several models as character vector. +#' @param auto_pull if FALSE, the default, asks before downloading models. +#' @inheritParams query +#' +#' @return invisible TRUE/FALSE +#' @export +check_model_installed <- function(model, auto_pull = FALSE, server = NULL) { + models_df <- list_models(server = server) + model <- setdiff(model, models_df$name) + model_wo_vers <- gsub(":.*", "", models_df$name) + model <- setdiff(model, model_wo_vers) + if (length(model) > 0L && !auto_pull) { + cli::cli_alert_info("Model{?s} {model} not installed. Would you like to download {?it/them}?") + if (interactive()) auto_pull <- askYesNo("") + if (!auto_pull) { + cli::cli_alert_danger("Model{?s} {model} not installed.") + invisible(FALSE) + } + } + if (auto_pull) { + for (m in model) { + pull_model(m, server = server) + } + } + invisible(TRUE) +} diff --git a/man/check_model_installed.Rd b/man/check_model_installed.Rd new file mode 100644 index 0000000..550dcc6 --- /dev/null +++ b/man/check_model_installed.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.r +\name{check_model_installed} +\alias{check_model_installed} +\title{Check if one or several models are installed on the server} +\usage{ +check_model_installed(model, auto_pull = FALSE, server = NULL) +} +\arguments{ +\item{model}{names of one or several models as character vector.} + +\item{auto_pull}{if FALSE, the default, asks before downloading models.} + +\item{server}{URL to an Ollama server (not the API). Defaults to +"http://localhost:11434".} +} +\value{ +invisible TRUE/FALSE +} +\description{ +Check if one or several models are installed on the server +}