fromFunction<Input extends Object, Output extends Object> static method
Tool<Object, ToolOptions, Object>
fromFunction<Input extends Object, Output extends Object>({
- required String name,
- required String description,
- required Map<
String, dynamic> inputJsonSchema, - bool strict = false,
- required FutureOr<
Output> func(- Input input
- Input getInputFromJson()?,
- bool returnDirect = false,
- Output handleToolError()?,
override
Creates a Tool from a function.
nameis the unique name of the tool that clearly communicates its purpose.descriptionis used to tell the model how/when/why to use the tool. You can provide few-shot examples as a part of the description.inputJsonSchemais the schema to parse and validate tool's input.strictwhether to enable strict schema adherence when generating the tool call (only supported by some providers).funcis the function that will be called when the tool is run. arguments.getInputFromJsonis 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.returnDirectwhether to return the tool's output directly. Setting this to true means that after the tool is called, the AgentExecutor will stop looping.handleToolErroris 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) {
if (json.containsKey('input')) {
return json['input'] as Input;
}
return json as Input;
},
returnDirect: returnDirect,
handleToolError: handleToolError,
);
}