Skip to content

Commit

Permalink
Add ps endpoint #18
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed Jul 29, 2024
1 parent a82e7c3 commit 6e3907f
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export(list_models)
export(model_avail)
export(ohelp)
export(prepend_message)
export(ps)
export(pull)
export(resp_process)
export(search_options)
Expand Down
38 changes: 38 additions & 0 deletions R/ollama.R
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,44 @@ embeddings <- function(model, prompt, normalize = TRUE, keep_alive = "5m", endpo



#' List running models
#'
#' @param output The output format. Default is "df". Supported formats are "df", "resp", "jsonlist", "raw", and "text".
#' @param endpoint The endpoint to list the running models. Default is "/api/ps".
#' @param host The base URL to use. Default is NULL, which uses Ollama's default base URL.
#'
#' @references
#' [API documentation](https://github.com/ollama/ollama/blob/main/docs/api.md#list-running-models)
#'
#' @return A response in the format specified in the output parameter.
#' @export
#'
#' @examplesIf test_connection()$status_code == 200
#' ps("text")
ps <- function(output = c("df", "resp", "jsonlist", "raw", "text"), endpoint = "/api/ps", host = NULL) {
output <- output[1]
if (!output %in% c("df", "resp", "jsonlist", "raw", "text")) {
stop("Invalid output format specified. Supported formats: 'df', 'resp', 'jsonlist', 'raw', 'text'")
}
req <- create_request(endpoint, host)
req <- httr2::req_method(req, "GET")
tryCatch(
{
resp <- httr2::req_perform(req)
return(resp_process(resp = resp, output = output))
},
error = function(e) {
stop(e)
}
)
}










Expand Down
28 changes: 27 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ resp_process <- function(resp, output = c("df", "jsonlist", "raw", "resp", "text
}

# endpoints that should never be processed with resp_process_stream
endpoints_without_stream <- c("api/tags", "api/delete", "api/show")
endpoints_without_stream <- c("api/tags", "api/delete", "api/show", "api/ps")

# process stream resp separately
stream <- FALSE
Expand Down Expand Up @@ -182,6 +182,32 @@ resp_process <- function(resp, output = c("df", "jsonlist", "raw", "resp", "text
}
} else if (grepl("api/show", resp$url)) {
if (output %in% c("df", "text")) stop("Output format not supported for this endpoint: Only 'jsonlist' and 'raw' are supported.")
} else if (grepl("api/ps", resp$url)) {
json_body <- httr2::resp_body_json(resp)$models
df_response <- tibble::tibble(
name = character(length(json_body)),
size = character(length(json_body)),
parameter_size = character(length(json_body)),
quantization_level = character(length(json_body)),
digest = character(length(json_body)),
expires_at = character(length(json_body)),
)

for (i in seq_along(json_body)) {
df_response[i, "name"] <- json_body[[i]]$name
size <- json_body[[i]]$size / 10^9
df_response[i, "size"] <- ifelse(size > 1, paste0(round(size, 1), " GB"), paste0(round(size * 1000), " MB"))
df_response[i, "parameter_size"] <- json_body[[i]]$details$parameter_size
df_response[i, "quantization_level"] <- json_body[[i]]$details$quantization_level
df_response[i, "digest"] <- json_body[[i]]$details$parameter_size
df_response[i, "expires_at"] <- json_body[[i]]$expires_at
}

if (output == "df") {
return(data.frame(df_response))
} else if (output == "text") {
return(df_response$name)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ reference:
- pull
- embed
- embeddings
- ps

- subtitle: Utility functions
desc: Functions to work with the Ollama API
Expand Down
33 changes: 33 additions & 0 deletions man/ps.Rd

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

24 changes: 23 additions & 1 deletion tests/testthat/test-ps.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
library(testthat)
library(ollamar)

test_that("ps function works with basic input", {
test_that("ps list running models endpoint", {
skip_if_not(test_connection()$status_code == 200, "Ollama server not available")

# load models first
g1 <- generate('llama3', "tell me a 5 word story")

result <- ps()
expect_true(nrow(result) > 1)
expect_true(all(c("name", "size", "parameter_size", "quantization_level", "digest", "expires_at") %in% names(result)))
expect_s3_class(ps("df"), "data.frame")
expect_s3_class(ps("resp"), "httr2_response")
expect_type(ps("jsonlist"), "list")
expect_type(ps("text"), "character")
expect_type(ps("raw"), "character")

# resp_process
result <- ps("resp")
expect_s3_class(result, "httr2_response")
expect_s3_class(resp_process(result, "resp"), "httr2_response")
expect_s3_class(resp_process(result, "df"), "data.frame")
expect_type(resp_process(result, "jsonlist"), "list")
expect_type(resp_process(result, "text"), "character")
expect_type(resp_process(result, "raw"), "character")

})

0 comments on commit 6e3907f

Please sign in to comment.