From 5b5cdc430c189badaf72d80a93147b819261ec25 Mon Sep 17 00:00:00 2001 From: Hause Lin Date: Wed, 25 Dec 2024 22:39:56 -0500 Subject: [PATCH] Fix examples and edit get_tool_calls --- R/utils.R | 15 ++++++--------- tests/testthat/test-chat.R | 11 ++++++----- vignettes/ollamar.Rmd | 18 +++++++++++------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/R/utils.R b/R/utils.R index 5ec3cc7..23e30ff 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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 = ", "))) } } diff --git a/tests/testthat/test-chat.R b/tests/testthat/test-chat.R index 4ad798e..1ee1e34 100644 --- a/tests/testthat/test-chat.R +++ b/tests/testthat/test-chat.R @@ -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") }) diff --git a/vignettes/ollamar.Rmd b/vignettes/ollamar.Rmd index 4d2f725..8354e2e 100644 --- a/vignettes/ollamar.Rmd +++ b/vignettes/ollamar.Rmd @@ -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 @@ -328,25 +328,29 @@ 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") @@ -354,7 +358,7 @@ 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 ```