Skip to content

Commit

Permalink
Test list models
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed Jul 28, 2024
1 parent 4226f33 commit b24fc67
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
9 changes: 5 additions & 4 deletions R/ollama.R
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ delete <- function(name, endpoint = "/api/delete", host = NULL) {
#'
#' See https://ollama.com/library for a list of available models. Use the list_models() function to get the list of models already downloaded/installed on your machine.
#'
#' @param model A character string of the model name to download/pull, such as "llama3".
#' @param name A character string of the model name to download/pull, such as "llama3".
#' @param stream Enable response streaming. Default is TRUE.
#' @param insecure Allow insecure connections Only use this if you are pulling from your own library during development. Default is FALSE.
#' @param endpoint The endpoint to pull the model. Default is "/api/pull".
Expand All @@ -420,12 +420,12 @@ delete <- function(name, endpoint = "/api/delete", host = NULL) {
#' @examplesIf test_connection()$status_code == 200
#' pull("llama3")
#' pull("all-minilm", stream = FALSE)
pull <- function(model, stream = TRUE, insecure = FALSE, endpoint = "/api/pull", host = NULL) {
pull <- function(name, stream = TRUE, insecure = FALSE, endpoint = "/api/pull", host = NULL) {
req <- create_request(endpoint, host)
req <- httr2::req_method(req, "POST")

body_json <- list(model = model, stream = stream, insecure = insecure)
req <- httr2::req_body_json(req, body_json)
body_json <- list(name = name, stream = stream, insecure = insecure)
req <- httr2::req_body_json(req, body_json, stream = stream)

if (!stream) {
tryCatch(
Expand All @@ -446,6 +446,7 @@ pull <- function(model, stream = TRUE, insecure = FALSE, endpoint = "/api/pull",
env$accumulated_data <- raw()
wrapped_handler <- function(x) stream_handler(x, env, endpoint)
resp <- httr2::req_perform_stream(req, wrapped_handler, buffer_kb = 1)
resp$body <- env$accumulated_data
return(resp)
}

Expand Down
12 changes: 10 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,26 @@ stream_handler <- function(x, env, endpoint) {
#' resp_process(resp, "resp") # return input response object
#' resp_process(resp, "text") # return text/character vector
resp_process <- function(resp, output = c("df", "jsonlist", "raw", "resp", "text")) {

if (is.null(resp) || resp$status_code != 200) {
warning("Cannot process response")
return(NULL)
}

endpoints_to_skip <- c("api/pull")
for (endpoint in endpoints_to_skip) {
if (grepl(endpoint, resp$url)) {
return(resp)
}
}

output <- output[1]
if (output == "resp") {
return(resp)
}

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

# process stream resp separately
stream <- FALSE
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ See [Ollama's Github page](https://github.com/ollama/ollama) for more informatio
install.packages("ollamar")
```

4. ALternatively, for the **latest/development** version with more/latest features, you can install it like so:
4. Alternatively, for the **latest/development** version with more/latest features, you can install it like so:

``` r
devtools::install_github("hauselin/ollamar")
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use this library.
install.packages("ollamar")
```

4. ALternatively, for the **latest/development** version with
4. Alternatively, for the **latest/development** version with
more/latest features, you can install it like so:

``` r
Expand Down
4 changes: 2 additions & 2 deletions man/pull.Rd

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

19 changes: 19 additions & 0 deletions tests/testthat/test-pull.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
library(testthat)
library(ollamar)

test_that("pull function works", {
skip_if_not(test_connection()$status_code == 200, "Ollama server not available")

# wrong model
result <- pull('WRONGMODEL')
expect_s3_class(result, "httr2_response")
expect_equal(result$status_code, 200)
expect_vector(result$body)

# correct model
result <- pull('llama3')
expect_s3_class(result, "httr2_response")
expect_equal(result$status_code, 200)
expect_vector(result$body)
})

0 comments on commit b24fc67

Please sign in to comment.