diff --git a/R/utils.R b/R/utils.R index 63a7366..0f676d6 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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)) } @@ -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 } - } @@ -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)) { diff --git a/tests/testthat/test-test_generate.R b/tests/testthat/test-test_generate.R new file mode 100644 index 0000000..cf2fa0a --- /dev/null +++ b/tests/testthat/test-test_generate.R @@ -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")) +})