|
3 | 3 | #' @param manager An OllamaModelManager object |
4 | 4 | #' @param new_model Character, model name input from user (can be partial) of the model to pull |
5 | 5 | #' @param base_url Local Ollama base URL |
| 6 | +#' @param exclude_pattern Character, a regex pattern to exclude certain models from the list of |
| 7 | +#' available models, e.g. "babbage|curie|dall-e|davinci|text-embedding|tts|whisper" |
6 | 8 | #' |
7 | 9 | #' @return An object of class LocalLlmApi, or a list with an "error" attribute if construction fails. |
8 | 10 | #' @export |
9 | 11 | new_LocalLlmApi <- function( |
10 | 12 | manager, |
11 | 13 | new_model = "", |
12 | | - base_url = Sys.getenv("OLLAMA_BASE_URL", unset = "http://localhost:11434") |
| 14 | + base_url = Sys.getenv("OLLAMA_BASE_URL", unset = "http://localhost:11434"), |
| 15 | + exclude_pattern = "" |
13 | 16 | ) { |
14 | 17 | if (!is_ollama_running(url = base_url)) { |
15 | 18 | api <- list() |
@@ -45,7 +48,8 @@ new_LocalLlmApi <- function( |
45 | 48 | list( |
46 | 49 | url = base_url, |
47 | 50 | provider = "Ollama", |
48 | | - manager = manager |
| 51 | + manager = manager, |
| 52 | + exclude_pattern = exclude_pattern |
49 | 53 | ), |
50 | 54 | class = c("LocalLlmApi", "LlmApi") |
51 | 55 | ) |
@@ -84,10 +88,17 @@ print.LocalLlmApi <- function(x, ...) { |
84 | 88 | #' @export |
85 | 89 | get_llm_models.LocalLlmApi <- function(x, ...) { |
86 | 90 | local_models <- x$manager$local_models |
| 91 | + exclude_pattern <- x$exclude_pattern |
87 | 92 |
|
88 | | - # Extract categories |
89 | | - categories <- vapply(local_models, function(x) categorize_model(x), character(1)) |
| 93 | + # Extract models |
90 | 94 | models <- vapply(local_models, function(x) x, character(1)) |
| 95 | + |
| 96 | + # Filter models |
| 97 | + models <- models |> filter_model_list(exclude_pattern = exclude_pattern) |
| 98 | + |
| 99 | + # Extract categories |
| 100 | + categories <- vapply(models, function(x) categorize_model(x), character(1)) |
| 101 | + |
91 | 102 | models_list <- extract_named_model_list(models, categories) |
92 | 103 |
|
93 | 104 | return(models_list) |
@@ -127,11 +138,14 @@ send_prompt.LocalLlmApi <- function(api, prompt_config) { |
127 | 138 | body$num_predict <- prompt_config$max_tokens |
128 | 139 | } |
129 | 140 |
|
130 | | - req <- httr2::request(paste0(api$url, "/api/generate")) |> |
131 | | - httr2::req_body_json(body) |> |
132 | | - httr2::req_perform() |
| 141 | + resp <- request(paste0(api$url, "/api/generate")) |> |
| 142 | + req_body_json(body) |> |
| 143 | + try_send_request() |
133 | 144 |
|
134 | | - resp <- httr2::resp_body_json(req) |
| 145 | + # return early if there was an error |
| 146 | + if (!is.null(attr(resp, "error"))) { |
| 147 | + return(resp) |
| 148 | + } |
135 | 149 |
|
136 | 150 | result <- list( |
137 | 151 | choices = list( |
|
0 commit comments