From 41d5e116bface0465c14eb278e455e1e5a3d6946 Mon Sep 17 00:00:00 2001 From: calderonsamuel Date: Wed, 6 Dec 2023 23:45:31 -0500 Subject: [PATCH] add a function to check is ollama is running in host --- R/ollama_url.R | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/R/ollama_url.R b/R/ollama_url.R index 537fd9a..58abd2f 100644 --- a/R/ollama_url.R +++ b/R/ollama_url.R @@ -1,9 +1,27 @@ ollama_api_url <- function() { - Sys.getenv("OLLAMA_HOST", "http://localhost:11434/api") + Sys.getenv("OLLAMA_HOST", "http://localhost:11434") } ollama_set_task <- function(task) { ollama_api_url() %>% - httr2::request() %>% + httr2::request() %>% + httr2::req_url_path_append("api") %>% httr2::req_url_path_append(task) } + +ollama_check_running <- function() { + request <- ollama_api_url() %>% + httr2::request() + + rlang::try_fetch({ + httr2::req_perform(request) %>% + httr2::resp_body_string() + }, error = function(cnd) { + if(inherits(cnd, "httr2_failure")) { + cli::cli_abort("Couldn't connect to Ollama in {.url {ollama_api_url()}}. Is it running there?", parent = cnd) + } else { + cnd + } + }) + return(TRUE) +}