-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
159d0db
commit 8189945
Showing
3 changed files
with
83 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
library(testthat) | ||
library(ollamar) | ||
|
||
test_that("chat function works with basic input", { | ||
skip_if_not(test_connection()$status_code == 200, "Ollama server not available") | ||
|
||
messages <- list( | ||
list(role = "user", content = "Hello! How are you?") | ||
) | ||
|
||
result <- chat("llama3", messages, output = "df") | ||
|
||
expect_s3_class(result, "data.frame") | ||
expect_true(all(c("model", "role", "content", "created_at") %in% names(result))) | ||
expect_equal(result$model[1], "llama3") | ||
expect_equal(result$role[1], "assistant") | ||
expect_true(nchar(result$content[1]) > 0) | ||
}) | ||
|
||
test_that("chat function handles streaming correctly", { | ||
skip_if_not(test_connection()$status_code == 200, "Ollama server not available") | ||
|
||
messages <- list( | ||
list(role = "user", content = "Count to 5") | ||
) | ||
|
||
result <- chat("llama3", messages, stream = TRUE, output = "text") | ||
|
||
expect_type(result, "character") | ||
expect_true(nchar(result) > 0) | ||
expect_match(result, "1.*2.*3.*4.*5", all = FALSE) | ||
}) | ||
|
||
test_that("chat function respects output parameter", { | ||
skip_if_not(test_connection()$status_code == 200, "Ollama server not available") | ||
|
||
messages <- list( | ||
list(role = "user", content = "Hello! How are you?") | ||
) | ||
|
||
result_jsonlist <- chat("llama3", messages, output = "jsonlist") | ||
expect_type(result_jsonlist, "list") | ||
expect_true(length(result_jsonlist) > 0) | ||
|
||
result_df <- chat("llama3", messages, output = "df") | ||
expect_s3_class(result_df, "data.frame") | ||
|
||
result_text <- chat("llama3", messages, output = "text") | ||
expect_type(result_text, "character") | ||
}) | ||
|
||
test_that("chat function handles multiple messages", { | ||
skip_if_not(test_connection()$status_code == 200, "Ollama server not available") | ||
|
||
messages <- list( | ||
list(role = "user", content = "Hello!"), | ||
list(role = "assistant", content = "Hi! How can I help you?"), | ||
list(role = "user", content = "What's the capital of France?") | ||
) | ||
|
||
result <- chat("llama3", messages, output = "df") | ||
|
||
expect_s3_class(result, "data.frame") | ||
expect_equal(nrow(result), 1) # Expecting one response | ||
expect_match(result$content[1], "Paris", ignore.case = TRUE) | ||
}) | ||
|
||
test_that("chat function handles additional options", { | ||
skip_if_not(test_connection()$status_code == 200, "Ollama server not available") | ||
|
||
messages <- list( | ||
list(role = "user", content = "Tell me a very short joke") | ||
) | ||
|
||
result_normal <- chat("llama3", messages, output = "text") | ||
result_creative <- chat("llama3", messages, output = "text", temperature = 2.0) | ||
|
||
expect_type(result_normal, "character") | ||
expect_type(result_creative, "character") | ||
expect_false(result_normal == result_creative) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +0,0 @@ | ||
test_that("multiplication works", { | ||
expect_equal(2 * 2, 4) | ||
}) | ||