Skip to content

Commit

Permalink
Fix examples and edit get_tool_calls
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed Dec 26, 2024
1 parent 259cf5b commit 5b5cdc4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
15 changes: 6 additions & 9 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,22 @@ stream_handler <- function(x, env, endpoint) {
get_tool_calls <- function(resp) {
body <- httr2::resp_body_json(resp)
tools <- list()
tools_list <- list()
tools_called <- c()
tools_list <- c()
if (!is.null(body$message)) {
if (!is.null(body$message$tool_calls)) {
tools <- body$message$tool_calls

tools_list <- list()
if (length(tools) > 0) {
for (i in seq_along(tools)) {
func <- tools[[i]]$`function`
func_name <- func$name
tools_list[[i]] <- list()
names(tools_list)[[i]] <- func_name
tools_list[[func_name]] <- func
tools_list[[i]] <- func
tools_called <- c(tools_called, func_name)
}
}

# remove empty lists
tools_list <- tools_list[which(sapply(tools_list, length) != 0)]
message(paste0("Tools: ", paste0(names(tools_list), collapse = ", ")))
tools_called <- unique(tools_called)
message(paste0("Tools called: ", paste0(tools_called, collapse = ", ")))
}
}

Expand Down
11 changes: 6 additions & 5 deletions tests/testthat/test-chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,14 @@ test_that("chat function handles tools", {
resp <- chat("llama3.1", msg, tools = tools, output = "tools")
expect_equal(resp[[1]]$name, "add_two_numbers")

msg <- create_message("what is four times five?")
msg <- create_message("what is four multiplied by five?")
resp <- chat("llama3.1", msg, tools = tools, output = "tools")
expect_equal(resp[[1]]$name, "multiply_two_numbers")

msg <- create_message("three and four. sum the numbers then multiply the output by ten")
resp <- chat("llama3.1", msg, tools = tools, output = "tools")
expect_equal(resp[[1]]$name, "add_two_numbers")
expect_equal(resp[[2]]$name, "multiply_two_numbers")
# not a reliable test
# msg <- create_message("three and four. sum the numbers then multiply the output by ten")
# resp <- chat("llama3.1", msg, tools = tools, output = "tools")
# expect_equal(resp[[1]]$name, "add_two_numbers")
# expect_equal(resp[[2]]$name, "multiply_two_numbers")

})
18 changes: 11 additions & 7 deletions vignettes/ollamar.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ tool1 <- list(type = "function",
tool2 <- list(type = "function",
"function" = list(
name = "multiply_two_numbers", # function name
description = "add two numbers",
description = "multiply two numbers",
parameters = list(
type = "object",
required = list("a", "b"), # function parameters
Expand All @@ -328,33 +328,37 @@ tool2 <- list(type = "function",
)
```

Next call the `chat()` function with the `tools` parameter set to a list of your tools. Pass in a single tool.
Then call the `chat()` function with the `tools` parameter set to a list of your tools. Pass in a single tool.

```{r eval=FALSE}
msg <- create_message("what is three plus one?")
resp <- chat("llama3.1", msg, tools = list(tool1), output = "tools")
tool <- resp[[1]] # get the first tool/function
do.call(tool$name, tool$arguments) # call the tool function with arguments: add_two_numbers(3, 1)
# call the tool function with arguments: add_two_numbers(3, 1)
do.call(tool$name, tool$arguments)
```

Pass in multiple tools. The model will pick the best tool to use based on the context of the message.

```{r eval=FALSE}
msg <- create_message("what is three times four?")
msg <- create_message("what is three multiplied by four?")
resp <- chat("llama3.1", msg, tools = list(tool1, tool2), output = "tools")
tool <- resp[[1]] # get the first tool/function
do.call(tool$name, tool$arguments) # call the tool function with arguments: multiply_two_numbers(3, 4)
# call the tool function with arguments: multiply_two_numbers(3, 4)
do.call(tool$name, tool$arguments)
```

Pass in multiple tools and get the model to use multiple tools.
Pass in multiple tools and get the model to use multiple tools. Note that LLM responses are inherently stochastic, so sometimes the model might choose to call only one tool, and sometimes might call tools multiple times.

```{r eval=FALSE}
msg <- create_message("add three plus four. then multiply by ten")
resp <- chat("llama3.1", msg, tools = list(tool1, tool2), output = "tools")
# first tool/function: add_two_numbers(3, 4)
do.call(resp[[1]]$name, resp[[1]]$arguments) # 7
# multiply_two_numbers(7, 10)
# second tool/function: multiply_two_numbers(7, 10)
do.call(resp[[2]]$name, resp[[2]]$arguments) # 70
```

Expand Down

0 comments on commit 5b5cdc4

Please sign in to comment.