Skip to content

Commit

Permalink
Ollama improvements (#283)
Browse files Browse the repository at this point in the history
* Add basic tests
* Add note about input tokens limitation (Fixes #276)
  • Loading branch information
hadley authored Jan 28, 2025
1 parent 7a61c30 commit 32f8497
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
11 changes: 11 additions & 0 deletions R/provider-ollama.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#'
#' * Tool calling is not supported with streaming (i.e. when `echo` is
#' `"text"` or `"all"`)
#' * Models can only use 2048 input tokens, and there's no way
#' to get them to use more, except by creating a custom model with a
#' different default.
#' * Tool calling generally seems quite weak, at least with the models I have
#' tried it with.
#'
Expand Down Expand Up @@ -58,6 +61,14 @@ chat_ollama <- function(system_prompt = NULL,
)
}

chat_ollama_test <- function(..., model = "llama3.3") {
if (!has_ollama()) {
testthat::skip("ollama not found")
}

chat_ollama(..., model = model)
}

ollama_models <- function(base_url = "http://localhost:11434") {
req <- request(base_url)
req <- req_url_path(req, "api/tags")
Expand Down
3 changes: 3 additions & 0 deletions man/chat_ollama.Rd

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

1 change: 1 addition & 0 deletions man/ellmer-package.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-provider-ollama.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Getting started --------------------------------------------------------

test_that("can make simple request", {
chat <- chat_ollama_test("Be as terse as possible; no punctuation")
resp <- chat$chat("What is 1 + 1?", echo = FALSE)
expect_match(resp, "2")
expect_equal(chat$last_turn()@tokens >= 1, c(TRUE, TRUE))
})

test_that("can make simple streaming request", {
chat <- chat_ollama_test("Be as terse as possible; no punctuation")
resp <- coro::collect(chat$stream("What is 1 + 1?"))
expect_match(paste0(unlist(resp), collapse = ""), "2")
})

# Common provider interface -----------------------------------------------

# Currently no other tests because I can't find a model that returns reliable
# results and is reasonably performant.

0 comments on commit 32f8497

Please sign in to comment.