Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed Aug 17, 2024
1 parent c043c16 commit 48a6212
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 45 deletions.
43 changes: 21 additions & 22 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,11 @@ generate("benzie/llava-phi-3", "What is in the image?", images = "image.png", ou

### Chat

Generate the next message in a chat (see [API doc](https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-chat-completion)). See the [Notes section](#notes) for utility/helper functions to help you format/prepare the messages for the functions/API calls.
Generate the next message in a chat (see [API doc](https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-chat-completion)). See the [Notes section](#notes) for details on how chat messages and chat history are represented/formatted.


```{r eval=FALSE}
messages <- list(
list(role = "user", content = "Who is the prime minister of the uk?")
)
messages <- create_message("what is the capital of australia") # default role is user
resp <- chat("llama3.1", messages) # default returns httr2 response object
resp # <httr2_response>
resp_process(resp, "text") # process the response to return text/vector output
Expand All @@ -143,34 +142,27 @@ chat("llama3.1", messages, output = "jsonlist") # list
chat("llama3.1", messages, output = "raw") # raw string
chat("llama3.1", messages, stream = TRUE) # stream output and return httr2 response object
# list of messages
messages <- list(
list(role = "user", content = "Hello!"),
list(role = "assistant", content = "Hi! How are you?"),
list(role = "user", content = "Who is the prime minister of the uk?"),
list(role = "assistant", content = "Rishi Sunak"),
list(role = "user", content = "List all the previous messages.")
# create chat history
messages <- create_messages(
create_message("end all your sentences with !!!", role = "system"),
create_message("Hello!"), # default role is user
create_message("Hi, how can I help you?!!!", role = "assistant"),
create_message("What is the capital of Australia?"),
create_message("Canberra!!!", role = "assistant"),
create_message("what is your name?")
)
cat(chat("llama3.1", messages, output = "text")) # print the formatted output
# image prompt
messages <- list(
list(role = "user", content = "What is in the image?", images = "image.png")
)
messages <- create_message("What is in the image?", images = "image.png")
# use a vision/multi-modal model
chat("benzie/llava-phi-3", messages, output = "text")
```

#### Streaming responses

```{r eval=FALSE}
messages <- list(
list(role = "user", content = "Hello!"),
list(role = "assistant", content = "Hi! How are you?"),
list(role = "user", content = "Who is the prime minister of the uk?"),
list(role = "assistant", content = "Rishi Sunak"),
list(role = "user", content = "List all the previous messages.")
)
messages <- create_message("Tell me a 1-paragraph story.")
# use "llama3.1" model, provide list of messages, return text/vector output, and stream the output
chat("llama3.1", messages, output = "text", stream = TRUE)
Expand Down Expand Up @@ -258,6 +250,7 @@ list( # main list containing all the messages

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
- `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
Expand All @@ -268,7 +261,7 @@ To simplify the process of creating and managing messages, `ollamar` provides ut
- if there are 5 messages, the positions are 1 (-5), 2 (-4), 3 (-3), 4 (-2), 5 (-1)

```{r eval=FALSE}
# create first message
# create a chat history with one message
messages <- create_message(content = "Hi! How are you? (1ST MESSAGE)", role = "assistant")
# or simply, messages <- create_message("Hi! How are you?", "assistant")
messages[[1]] # get 1st message
Expand All @@ -294,6 +287,12 @@ messages[[4]] # get 2nd message
# delete a message at a specific index/position (2nd position in the example below)
messages <- delete_message(messages, 2)
# create a chat history with multiple messages
messages <- create_messages(
create_message("You're a knowledgeable tour guide.", role = "system"),
create_message("What is the capital of Australia?") # default role is user
)
```

## Advanced usage
Expand Down
44 changes: 21 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,11 @@ generate("benzie/llava-phi-3", "What is in the image?", images = "image.png", ou

Generate the next message in a chat (see [API
doc](https://github.com/ollama/ollama/blob/main/docs/api.md#generate-a-chat-completion)).
See the [Notes section](#notes) for utility/helper functions to help you
format/prepare the messages for the functions/API calls.
See the [Notes section](#notes) for details on how chat messages and
chat history are represented/formatted.

``` r
messages <- list(
list(role = "user", content = "Who is the prime minister of the uk?")
)
messages <- create_message("what is the capital of australia") # default role is user
resp <- chat("llama3.1", messages) # default returns httr2 response object
resp # <httr2_response>
resp_process(resp, "text") # process the response to return text/vector output
Expand All @@ -167,34 +165,27 @@ chat("llama3.1", messages, output = "jsonlist") # list
chat("llama3.1", messages, output = "raw") # raw string
chat("llama3.1", messages, stream = TRUE) # stream output and return httr2 response object

# list of messages
messages <- list(
list(role = "user", content = "Hello!"),
list(role = "assistant", content = "Hi! How are you?"),
list(role = "user", content = "Who is the prime minister of the uk?"),
list(role = "assistant", content = "Rishi Sunak"),
list(role = "user", content = "List all the previous messages.")
# create chat history
messages <- create_messages(
create_message("end all your sentences with !!!", role = "system"),
create_message("Hello!"), # default role is user
create_message("Hi, how can I help you?!!!", role = "assistant"),
create_message("What is the capital of Australia?"),
create_message("Canberra!!!", role = "assistant"),
create_message("what is your name?")
)
cat(chat("llama3.1", messages, output = "text")) # print the formatted output

# image prompt
messages <- list(
list(role = "user", content = "What is in the image?", images = "image.png")
)
messages <- create_message("What is in the image?", images = "image.png")
# use a vision/multi-modal model
chat("benzie/llava-phi-3", messages, output = "text")
```

#### Streaming responses

``` r
messages <- list(
list(role = "user", content = "Hello!"),
list(role = "assistant", content = "Hi! How are you?"),
list(role = "user", content = "Who is the prime minister of the uk?"),
list(role = "assistant", content = "Rishi Sunak"),
list(role = "user", content = "List all the previous messages.")
)
messages <- create_message("Tell me a 1-paragraph story.")

# use "llama3.1" model, provide list of messages, return text/vector output, and stream the output
chat("llama3.1", messages, output = "text", stream = TRUE)
Expand Down Expand Up @@ -298,6 +289,7 @@ 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
- `append_message()` adds a new message to the end of the existing
messages
Expand All @@ -313,7 +305,7 @@ provides utility/helper functions to format and prepare messages for the
(-2), 5 (-1)

``` r
# create first message
# create a chat history with one message
messages <- create_message(content = "Hi! How are you? (1ST MESSAGE)", role = "assistant")
# or simply, messages <- create_message("Hi! How are you?", "assistant")
messages[[1]] # get 1st message
Expand All @@ -339,6 +331,12 @@ messages[[4]] # get 2nd message

# delete a message at a specific index/position (2nd position in the example below)
messages <- delete_message(messages, 2)

# create a chat history with multiple messages
messages <- create_messages(
create_message("You're a knowledgeable tour guide.", role = "system"),
create_message("What is the capital of Australia?") # default role is user
)
```

## Advanced usage
Expand Down

0 comments on commit 48a6212

Please sign in to comment.