From ed3285464ad4d6f73d9115647d8a9a1da40c8105 Mon Sep 17 00:00:00 2001 From: Hause Lin Date: Sun, 28 Jul 2024 00:46:58 -0400 Subject: [PATCH] Add model_avail function --- .github/workflows/R-CMD-check.yaml | 4 ++-- NAMESPACE | 1 + R/ollama.R | 38 ++++++++++++++++++++++++++++++ _pkgdown.yml | 7 +++--- man/model_avail.Rd | 24 +++++++++++++++++++ 5 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 man/model_avail.Rd diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 40eeb3f..14159b7 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -2,9 +2,9 @@ # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: - branches: [main, master, dev, generate] + branches: [main, master] pull_request: - branches: [main, master, dev, generate] + branches: [main, master] name: R-CMD-check diff --git a/NAMESPACE b/NAMESPACE index ab4ef2e..8ddcf31 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -14,6 +14,7 @@ export(generate) export(image_encode_base64) export(insert_message) export(list_models) +export(model_avail) export(ohelp) export(prepend_message) export(pull) diff --git a/R/ollama.R b/R/ollama.R index ce24cfb..ac19234 100644 --- a/R/ollama.R +++ b/R/ollama.R @@ -582,6 +582,10 @@ embeddings <- function(model, prompt, normalize = TRUE, keep_alive = "5m", endpo #' # regular usage: ohelp() ohelp <- function(model = "codegemma:7b", ...) { + if (!model_avail(model)) { + return(invisible()) + } + cat("Say something or type /q to quit or end the conversation.\n\n") n_messages <- 0 @@ -610,3 +614,37 @@ ohelp <- function(model = "codegemma:7b", ...) { cat("Goodbye!\n") } + + + + + + + +#' Check if model is available locally. +#' +#' @param model A character string of the model name such as "llama3". +#' +#' @return A logical value indicating if the model exists. +#' @export +#' +#' @examplesIf test_connection()$status_code == 200 +#' model_avail("codegemma:7b") +#' model_avail("abc") +#' model_avail("llama3") +model_avail <- function(model) { + model <- tolower(model) + models <- sort(list_models("text")) + exist <- FALSE + for (m in models) { + mm <- tolower(strsplit(m, ":")[[1]][1]) + if (mm == model | m == model) { + exist <- TRUE + break + } + } + if (!exist) { + cat(paste("Model", model, "does not exist. Please check available models with list_models() or download the model with pull().\n")) + } + return(exist) +} diff --git a/_pkgdown.yml b/_pkgdown.yml index 099d494..7e37c5b 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -14,9 +14,6 @@ reference: - pull - embed - embeddings - - ohelp - - test_connection - - create_request - subtitle: Options available for the models desc: Functions to get information about the options available @@ -30,6 +27,10 @@ reference: desc: Functions that are useful for working with the Ollama API contents: - resp_process + - ohelp + - model_avail + - test_connection + - create_request - image_encode_base64 - create_message - append_message diff --git a/man/model_avail.Rd b/man/model_avail.Rd new file mode 100644 index 0000000..9928e4b --- /dev/null +++ b/man/model_avail.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ollama.R +\name{model_avail} +\alias{model_avail} +\title{Check if model is available locally.} +\usage{ +model_avail(model) +} +\arguments{ +\item{model}{A character string of the model name such as "llama3".} +} +\value{ +A logical value indicating if the model exists. +} +\description{ +Check if model is available locally. +} +\examples{ +\dontshow{if (test_connection()$status_code == 200) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +model_avail("codegemma:7b") +model_avail("abc") +model_avail("llama3") +\dontshow{\}) # examplesIf} +}