Categories &

Functions List

Class Definition: toolFunction

llms: toolFunction

A toolFunction object for tool-calling capable models.

toolFunction is a scalar class object, which contains the full description of a tool function along with the handle to its corresponding Octave function, which should be executed after a tool call request from the model.

Source Code: toolFunction

The toolFunction class offers the following public methods:

toolFunction: tool = toolFunction (name, description, handle)

tool = toolFunction (name, description, handle) creates a toolFunction object, which comprises an identifier specified by name, a functionality description that can be understood by the LLM model specified in description and a function handle, specified in handle, which corresponds to an actual Octave function that will be evaluated along with any input parameters specified by the LLM’s tool calling response.

By default, toolFunction does not add any input parameters to the created object. Use the addParameters method to append any input input parameters that your function handle may require for its successful evaluation. Use the evalFunction to evaluate the underlying function handle according to the input arguments specified by the LLM.

toolFunction: tool = addParameters (tool, propName, propType, propDescription)
toolFunction: tool = addParameters (tool, propName, propType, propDescription, enum)
toolFunction: tool = addParameters (…, Name, Value)

addParameters appends the parameters of a single input argument into the toolFunction object so that the LLM can understand the context of the corresponding input argument of the underlying function handle when asking for its evaluation.

addParameters requires at least four input arguments (and may accept an optional fifth argument), which are as described below:

  1. tool (required) A toolFunction object that the parameters will be appended to.
  2. propName (required) A character vector specifying the name of the input argument in the undelying function handle to be evaluated.
  3. propType (required) A character vector specifying the data type of the value corresponding to the input argument specified above.
  4. propDescription (required) A character vector describing the input argument so that the LLM can understand what value to assign for evaluation.
  5. enum (optional) A cell array of character vectors specifying a list of acceptable values that the LLM may chooce from to supply as an input argument. Alternatively, enum can be a cell array of numeric or logical values.

Anything beyond the fourth argument may also be given as Name, Value pairs, which is the preferred form: the reader of a tool declaration is a language model, and a model handles named arguments reliably where it handles positional ones by luck. The same holds for whoever writes the declaration.

NameValue
"enum"As enum above.
"default"The value the parameter takes when the model’s tool call leaves it out. A parameter with a default is optional: it is omitted from the required list of the encoded schema, it is advertised to the model so that leaving it out is an informed choice, and a call that omits it is completed from the default rather than dispatched with a gap in its arguments. When "enum" is also given, the default must be one of its values.

Defaults are how optionality is expressed here, in preference to letting a call arrive with an argument missing. The function handle is called positionally, so an absent argument in the middle of the list would silently shift every later one into the wrong place; filling it from a declared default means the handle is always called with a complete list.

toolFunction: tool_output = evalFunction (tool, tool_call)

tool_output = evalFunction (tool, tool_call) evaluates the function handle of the toolFunction object specified by tool according to the input arguments described by the LLM’s tool calling response specified in tool_call, which can be a character vector containing the appropriate JSON string message or its equivalent to a scalar structure. The returned tool_output is a 1x2 cell array of character vectors, in which the first element contains the output of the evaluated toolFunction object and the second element contains its corresponding function name.

If tool_call names this tool but supplies arguments that do not match its parameters, the first element carries a diagnostic naming the expected and the supplied arguments instead of the function’s output. The function handle is not evaluated. This is deliberate: the model must be told what was wrong with its call, because a result it never receives is one it cannot correct, and it will otherwise repeat the call or invent an answer. The order of the supplied arguments is not significant, the members of a JSON object being unordered.

If tool_call names a different tool, an empty character vector is returned so that a caller can dispatch over several tools.