fromFunction<Input extends Object, Output extends Object> static method

Tool<Object, ToolOptions, Object> fromFunction<Input extends Object, Output extends Object>({
  1. required String name,
  2. required String description,
  3. required Map<String, dynamic> inputJsonSchema,
  4. bool strict = false,
  5. required FutureOr<Output> func(
    1. Input input
    ),
  6. Input getInputFromJson(
    1. Map<String, dynamic> json
    )?,
  7. bool returnDirect = false,
  8. Output handleToolError(
    1. ToolException
    )?,
})
override

Creates a Tool from a function.

  • name is the unique name of the tool that clearly communicates its purpose.
  • description is used to tell the model how/when/why to use the tool. You can provide few-shot examples as a part of the description.
  • inputJsonSchema is the schema to parse and validate tool's input.
  • strict whether to enable strict schema adherence when generating the tool call (only supported by some providers).
  • func is the function that will be called when the tool is run. arguments.
  • getInputFromJson is a function that parses the input JSON to the tool's input type. By default, it assumes the input values is under the key 'input'. Define your own deserialization logic if the input is not a primitive type or is under a different key.
  • returnDirect whether to return the tool's output directly. Setting this to true means that after the tool is called, the AgentExecutor will stop looping.
  • handleToolError is a function that handles the content of the ToolException thrown by the tool.

Implementation

static Tool fromFunction<Input extends Object, Output extends Object>({
  required final String name,
  required final String description,
  required final Map<String, dynamic> inputJsonSchema,
  final bool strict = false,
  required final FutureOr<Output> Function(Input input) func,
  Input Function(Map<String, dynamic> json)? getInputFromJson,
  final bool returnDirect = false,
  final Output Function(ToolException)? handleToolError,
}) {
  return _ToolFunc<Input, Output>(
    name: name,
    description: description,
    inputJsonSchema: inputJsonSchema,
    strict: strict,
    function: func,
    getInputFromJson: getInputFromJson ?? (json) => json['input'] as Input,
    returnDirect: returnDirect,
    handleToolError: handleToolError,
  );
}