When a request offers tools (the tools argument of chat_completion())
and the model decides to call one, the reply has no text content and
finish_reason "tool_calls". This function returns those calls of the
first choice, one row per call. Running the tools is up to you; how to
send their results back is described below.
Arguments
- response
A list object representing the parsed response of a call to
chat_completion().
Value
A data.frame with the character columns id (the call id to quote
in the tool's reply), type ("function"), name (the function's name)
and arguments (its arguments as a JSON string, e.g. for
jsonlite::fromJSON()), with no rows if the reply calls no tool.
Sending tool results back
To let the model answer with a tool's result, call chat_completion()
again, with the same tools, and a data.frame msgs that holds the
conversation so far plus two kinds of rows:
the assistant's tool-call reply: role
"assistant", contentNA(it is left out of the request), and in a list columntool_callsthe reply's calls exactly as returned,resp$choices$message$tool_calls[[1]];one row per call with role
"tool", the tool's result as a string incontent(e.g. JSON), and the call'sid(fromtool_calls()) in a columntool_call_id.
The other rows hold NULL in tool_calls and NA in tool_call_id. A
plain list of message lists is not accepted as msgs. See the examples.
Examples
# a tool-call reply, parsed as chat_completion() parses it
reply <- list(choices = list(list(
index = 0,
message = list(role = "assistant", content = NULL, tool_calls = list(
list(id = "call_1", type = "function", `function` = list(
name = "get_weather", arguments = '{"city": "Bern"}'))
)),
finish_reason = "tool_calls"
)))
resp <- jsonlite::fromJSON(
jsonlite::toJSON(reply, auto_unbox = TRUE, null = "null"))
calls <- tool_calls(resp)
calls$name
#> [1] "get_weather"
jsonlite::fromJSON(calls$arguments)
#> $city
#> [1] "Bern"
#>
# sending the tool's result back: the conversation so far, the
# assistant's tool calls as returned, and the result for the call's id
followup <- data.frame(
role = c("user", "assistant", "tool"),
content = c("What's the weather in Bern?", NA,
'{"temp_c": 4, "sky": "cloudy"}')
)
followup$tool_calls <- list(NULL, resp$choices$message$tool_calls[[1]],
NULL)
followup$tool_call_id <- c(NA, NA, calls$id[1])
followup[, c("role", "content", "tool_call_id")]
#> role content tool_call_id
#> 1 user What's the weather in Bern? <NA>
#> 2 assistant <NA> <NA>
#> 3 tool {"temp_c": 4, "sky": "cloudy"} call_1
if (FALSE) { # \dontrun{
# needs an API key, see openai_api_key()
weather <- list(type = "function", `function` = list(
name = "get_weather",
description = "Current weather in a city",
parameters = list(
type = "object",
properties = list(city = list(type = "string")),
required = list("city")
)
))
resp <- chat_completion("What's the weather in Bern?", tools = weather,
tool_choice = "required")
calls <- tool_calls(resp)
# ... run get_weather() with jsonlite::fromJSON(calls$arguments[1]),
# build `followup` as above, then:
answer <- chat_completion(followup, tools = weather)
messages_content(answer)
} # }
