Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Dec 18, 2024
1 parent 0d0d48d commit 7a9ceee
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/testthat/test-provider-gemini.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,26 @@ test_that("can use images", {
test_images_inline(chat_fun)
test_images_remote_error(chat_fun)
})

# chunk merging ----------------------------------------------------------

test_that("can merge text output", {
# output from "tell me a joke" with text changed
messages <- c(
'{"candidates": [{"content": {"parts": [{"text": "a"}],"role": "model"}}],"usageMetadata": {"promptTokenCount": 5,"totalTokenCount": 5},"modelVersion": "gemini-1.5-flash"}',
'{"candidates": [{"content": {"parts": [{"text": "b"}],"role": "model"}}],"usageMetadata": {"promptTokenCount": 5,"totalTokenCount": 5},"modelVersion": "gemini-1.5-flash"}',
'{"candidates": [{"content": {"parts": [{"text": "c"}],"role": "model"},"finishReason": "STOP"}],"usageMetadata": {"promptTokenCount": 5,"candidatesTokenCount": 17,"totalTokenCount": 22},"modelVersion": "gemini-1.5-flash"}'
)
chunks <- lapply(messages, jsonlite::parse_json)

out <- merge_gemini_chunks(chunks[[1]], chunks[[2]])
out <- merge_gemini_chunks(out, chunks[[3]])

expect_equal(out$candidates[[1]]$content$parts[[1]]$text, "abc")
expect_equal(out$usageMetadata, list(
promptTokenCount = 5,
candidatesTokenCount = 17,
totalTokenCount = 22
))
expect_equal(out$candidates[[1]]$finishReason, "STOP")
})

0 comments on commit 7a9ceee

Please sign in to comment.