Skip to contents

Converts an R function into a hal tool definition suitable for $register_tool(). Infers parameter types from function formals; override with the types argument for non-string parameters.

Usage

hal_tool(fn, name = NULL, description = NULL, types = NULL)

Arguments

fn

The R function to expose as a tool.

name

Tool name (defaults to the symbol name of fn).

description

Description shown to the LLM (when to call this tool).

types

Optional named list mapping argument names to JSON Schema type strings: "string", "number", "integer", "boolean", "array", "object".

Value

A list with name, description, fun, and parameters – ready for chat$register_tool().

Examples

my_sum <- function(x, y) x + y
tool <- hal_tool(my_sum, "add_numbers",
  description = "Add two numbers",
  types = list(x = "number", y = "number")
)
str(tool)
#> List of 4
#>  $ name       : chr "add_numbers"
#>  $ description: chr "Add two numbers"
#>  $ fun        :function (x, y)  
#>  $ parameters :List of 3
#>   ..$ type      : chr "object"
#>   ..$ properties:List of 2
#>   .. ..$ x:List of 1
#>   .. .. ..$ type: chr "number"
#>   .. ..$ y:List of 1
#>   .. .. ..$ type: chr "number"
#>   ..$ required  :List of 2
#>   .. ..$ : chr "x"
#>   .. ..$ : chr "y"
if (FALSE) { # \dontrun{
# register on the active session so the model can call it
hal_register_tools(list(tool))
} # }