Skip to content

Commit

Permalink
Add model_avail function
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed Jul 28, 2024
1 parent d3d0181 commit ed32854
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions R/ollama.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
7 changes: 4 additions & 3 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions man/model_avail.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ed32854

Please sign in to comment.