Skip to content

Commit

Permalink
Add tests for generate
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed Jul 28, 2024
1 parent 2f0421e commit dc45fb7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
11 changes: 5 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ resp_process <- function(resp, output = c("df", "jsonlist", "raw", "resp", "text

# process non-stream response below
if (output == "raw") {
return(httr2::resp_raw(resp))
return(rawToChar(resp$body))
} else if (output == "jsonlist") {
return(httr2::resp_body_json(resp))
}
Expand Down Expand Up @@ -167,12 +167,11 @@ resp_process_stream <- function(resp, output) {
if (output == "text") {
return(paste0(df_response$response, collapse = ""))
}
} else if (grepl("api/chat", resp$url)) { # process chat endpoint
return(NULL) # TODO fill in
} else if (grepl("api/chat", resp$url)) { # process chat endpoint
return(NULL) # TODO fill in
} else if (grepl("api/tags", resp$url)) { # process tags endpoint {
return(NULL) # TODO fill in
return(NULL) # TODO fill in
}

}


Expand All @@ -188,7 +187,7 @@ resp_process_stream <- function(resp, output) {
#' @export
#'
#' @examples
#' image_path <- file.path(system.file('extdata', package = "ollamar"), "image1.png")
#' image_path <- file.path(system.file("extdata", package = "ollamar"), "image1.png")
#' image_encode_base64(image_path)
image_encode_base64 <- function(image_path) {
if (!file.exists(image_path)) {
Expand Down
62 changes: 62 additions & 0 deletions tests/testthat/test-test_generate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
library(testthat)
library(ollamar)

test_that("generate function works with different outputs and resp_process", {
skip_if_not(test_connection()$status_code == 200, "Ollama server not available")

# not streaming
expect_s3_class(generate("llama3", "The sky is..."), "httr2_response")
expect_s3_class(generate("llama3", "The sky is...", output = "resp"), "httr2_response")
expect_s3_class(generate("llama3", "The sky is...", output = "df"), "data.frame")
expect_type(generate("llama3", "The sky is...", output = "jsonlist"), "list")
expect_type(generate("llama3", "The sky is...", output = "text"), "character")
expect_type(generate("llama3", "The sky is...", output = "raw"), "character")

# streaming
expect_s3_class(generate("llama3", "The sky is...", stream = TRUE), "httr2_response")
expect_s3_class(generate("llama3", "The sky is...", stream = TRUE, output = "resp"), "httr2_response")
expect_s3_class(generate("llama3", "The sky is...", stream = TRUE, output = "df"), "data.frame")
expect_type(generate("llama3", "The sky is...", stream = TRUE, output = "jsonlist"), "list")
expect_type(generate("llama3", "The sky is...", stream = TRUE, output = "text"), "character")
expect_type(generate("llama3", "The sky is...", stream = TRUE, output = "raw"), "character")

## resp_process
# not streaming
result <- generate("llama3", "The sky is...")
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")

# streaming
result <- generate("llama3", "The sky is...", stream = TRUE)
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")
})

test_that("generate function works with additional options", {
skip_if_not(test_connection()$status_code == 200, "Ollama server not available")

expect_s3_class(generate("llama3", "The sky is...", num_predict = 1, temperature = 0), "httr2_response")
expect_error(generate("llama3", "The sky is...", abc = 1, sdf = 2))
})


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

image_path <- file.path(system.file("extdata", package = "ollamar"), "image1.png")

result <- generate("benzie/llava-phi-3:latest", "What is in the image?", images = image_path)
expect_s3_class(result, "httr2_response")
expect_type(resp_process(result, "text"), "character")
expect_match(tolower(resp_process(result, "text")), "watermelon")

expect_error(generate("benzie/llava-phi-3:latest", "What is in the image?", images = "incorrect_path.png"))
})

0 comments on commit dc45fb7

Please sign in to comment.