Skip to content

Commit

Permalink
Fix bug and add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed Aug 17, 2024
1 parent 12c20ee commit 749f5ee
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 26 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Add functions to validate messages for `chat()` function: `validate_message()`, `validate_messages()`.
- Add `encode_images_in_messages()` to encode images in messages for `chat()` function.
- Add `create_messages()` to create messages easily.
- Helper functions for managing messages accept `...` parameter to pass additional options.

# ollamar 1.2.0

Expand Down
2 changes: 1 addition & 1 deletion R/ollama.R
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ delete <- function(name, endpoint = "/api/delete", host = NULL) {



#' Pull/download a model from the Ollama library.
#' Pull/download a model from the Ollama library
#'
#' See https://ollama.com/library for a list of available models. Use the list_models() function to get the list of models already downloaded/installed on your machine. Cancelled pulls are resumed from where they left off, and multiple calls will share the same download progress.
#'
Expand Down
20 changes: 13 additions & 7 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,16 @@ delete_message <- function(x, position = -1) {
#' @export
#'
#' @examples
#' validate_message(create_message("Hello"))
#' validate_message(list(role = "user", content = "Hello"))
validate_message <- function(message) {

# if message is a list of messages, extract the first message
# likely created by create_message()
if (is.list(message) & all(c("role", "content") %in% names(message[[1]]))) {
message <- message[[1]]
}

if (!is.list(message)) {
stop("Message must be list.")
}
Expand Down Expand Up @@ -614,9 +622,9 @@ create_messages <- function(...) {
#' @export
#'
#' @examples
#' validate_messages(list(
#' list(role = "system", content = "Be friendly"),
#' list(role = "user", content = "Hello")
#' validate_messages(create_messages(
#' create_message("Be friendly", "system"),
#' create_message("Hello")
#' ))
validate_messages <- function(messages) {
status <- TRUE
Expand All @@ -642,10 +650,8 @@ validate_messages <- function(messages) {
#'
#' @examples
#' image <- file.path(system.file("extdata", package = "ollamar"), "image1.png")
#' messages <- list(
#' list(role = "user", content = "what is in the image?", images = image)
#' )
#' messages_updated <- encode_images_in_messages(messages)
#' message <- create_message(content = "what is in the image?", images = image)
#' message_updated <- encode_images_in_messages(message)
encode_images_in_messages <- function(messages) {
if (!validate_messages(messages)) {
stop("Invalid messages.")
Expand Down
7 changes: 2 additions & 5 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,14 @@ Internally, messages are represented as a `list` of many distinct `list` message
```{r eval=FALSE}
list( # main list containing all the messages
list(role = "user", content = "Hello!"), # first message as a list
list(role = "assistant", content = "Hi! How are you?"), # second message as a list
list(role = "user", content = "Who is the prime minister of the uk?"), # third message as a list
list(role = "assistant", content = "Rishi Sunak"), # fourth message as a list
list(role = "user", content = "List all the previous messages.") # fifth message as a list
list(role = "assistant", content = "Hi! How are you?") # second message as a list
)
```

To simplify the process of creating and managing messages, `ollamar` provides utility/helper functions to format and prepare messages for the `chat()` function.

- `create_messages()`: create messages to build a chat history
- `create_message()` creates the first message
- `create_message()` creates a chat history with a single message
- `append_message()` adds a new message to the end of the existing messages
- `prepend_message()` adds a new message to the beginning of the existing messages
- `insert_message()` inserts a new message at a specific index in the existing messages
Expand Down
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,7 @@ text). The example below shows how the messages/lists are presented.
``` r
list( # main list containing all the messages
list(role = "user", content = "Hello!"), # first message as a list
list(role = "assistant", content = "Hi! How are you?"), # second message as a list
list(role = "user", content = "Who is the prime minister of the uk?"), # third message as a list
list(role = "assistant", content = "Rishi Sunak"), # fourth message as a list
list(role = "user", content = "List all the previous messages.") # fifth message as a list
list(role = "assistant", content = "Hi! How are you?") # second message as a list
)
```

Expand All @@ -296,7 +293,7 @@ provides utility/helper functions to format and prepare messages for the
`chat()` function.

- `create_messages()`: create messages to build a chat history
- `create_message()` creates the first message
- `create_message()` creates a chat history with a single message
- `append_message()` adds a new message to the end of the existing
messages
- `prepend_message()` adds a new message to the beginning of the
Expand Down
6 changes: 2 additions & 4 deletions man/encode_images_in_messages.Rd

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

2 changes: 1 addition & 1 deletion man/pull.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/validate_message.Rd

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

6 changes: 3 additions & 3 deletions man/validate_messages.Rd

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

15 changes: 15 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ test_that("copy function works with basic input", {
expect_identical(messages5, messages4)

expect_true(validate_message(list(role = "user", content = "hello")))
expect_true(validate_message(create_message('hello')))
expect_error(validate_message(create_message('hello', 1)))
expect_error(validate_message(create_message(2, 1)))
expect_error(validate_message(""))
expect_error(validate_message(list(role = "user")))
expect_error(validate_message(list(content = "hello")))
Expand All @@ -85,6 +88,18 @@ test_that("copy function works with basic input", {
list(role = "user", content = 1)
)))

expect_true(validate_messages(create_messages(
create_message(content = "hello")
)))
expect_true(validate_messages(create_messages(
create_message(role = "system", content = "hello"),
create_message(role = "user", content = "hello")
)))
expect_error(validate_messages(create_messages(
create_message(role = 2, content = "hello"),
create_message(role = "user", content = 1)
)))

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

Expand Down

0 comments on commit 749f5ee

Please sign in to comment.