Package com.google.genkit.ai
Class Tool<I,O>
java.lang.Object
com.google.genkit.ai.Tool<I,O>
- Type Parameters:
I- the input typeO- the output type
- All Implemented Interfaces:
Action<I,,O, Void> Registerable
Tool represents a function that can be called by an AI model.
Tools allow models to interact with external systems and perform actions
during generation.
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <I,O> Tool.Builder <I, O> builder()Creates a builder for Tool.Gets the tool definition for use in model requests.getDesc()Returns the descriptor of the action containing metadata, schemas, etc.Gets the tool description.Gets the input class for JSON deserialization.Returns the JSON schema for the action's input type.Returns additional metadata for the action.getName()Returns the name of the action.Returns the JSON schema for the action's output type.getType()Returns the type of the action.voidRegisters this primitive with the given registry.Constructs a tool response for an interrupted tool request.Constructs a tool response for an interrupted tool request with metadata.Constructs a restart request for an interrupted tool.Constructs a restart request with replacement input.run(ActionContext ctx, I input) Runs the action with the given input.run(ActionContext ctx, I input, Consumer<Void> streamCallback) Runs the action with the given input and streaming callback.com.fasterxml.jackson.databind.JsonNoderunJson(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode input, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) Runs the action with JSON input and returns JSON output.ActionRunResult<com.fasterxml.jackson.databind.JsonNode> runJsonWithTelemetry(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode input, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) Runs the action with JSON input and returns the result with telemetry information.
-
Constructor Details
-
Tool
public Tool(String name, String description, Map<String, Object> inputSchema, Map<String, Object> outputSchema, Class<I> inputClass, BiFunction<ActionContext, I, O> handler) Creates a new Tool.- Parameters:
name- the tool namedescription- the tool descriptioninputSchema- the input JSON schemaoutputSchema- the output JSON schemainputClass- the input class for JSON deserializationhandler- the tool handler function
-
-
Method Details
-
builder
Creates a builder for Tool.- Type Parameters:
I- the input typeO- the output type- Returns:
- a new builder
-
getName
Description copied from interface:ActionReturns the name of the action. -
getType
Description copied from interface:ActionReturns the type of the action. -
getDesc
Description copied from interface:ActionReturns the descriptor of the action containing metadata, schemas, etc. -
run
Description copied from interface:ActionRuns the action with the given input. -
run
Description copied from interface:ActionRuns the action with the given input and streaming callback. -
runJson
public com.fasterxml.jackson.databind.JsonNode runJson(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode input, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) throws GenkitException Description copied from interface:ActionRuns the action with JSON input and returns JSON output. -
runJsonWithTelemetry
public ActionRunResult<com.fasterxml.jackson.databind.JsonNode> runJsonWithTelemetry(ActionContext ctx, com.fasterxml.jackson.databind.JsonNode input, Consumer<com.fasterxml.jackson.databind.JsonNode> streamCallback) throws GenkitException Description copied from interface:ActionRuns the action with JSON input and returns the result with telemetry information.- Specified by:
runJsonWithTelemetryin interfaceAction<I,O, Void> - Parameters:
ctx- the action contextinput- the JSON inputstreamCallback- callback for receiving streaming JSON chunks, may be null- Returns:
- the action result including telemetry data
- Throws:
GenkitException- if the action fails
-
getInputSchema
Description copied from interface:ActionReturns the JSON schema for the action's input type.- Specified by:
getInputSchemain interfaceAction<I,O, Void> - Returns:
- the input schema as a map, or null if not defined
-
getOutputSchema
Description copied from interface:ActionReturns the JSON schema for the action's output type.- Specified by:
getOutputSchemain interfaceAction<I,O, Void> - Returns:
- the output schema as a map, or null if not defined
-
getMetadata
Description copied from interface:ActionReturns additional metadata for the action.- Specified by:
getMetadatain interfaceAction<I,O, Void> - Returns:
- the metadata map
-
register
Description copied from interface:RegisterableRegisters this primitive with the given registry.- Specified by:
registerin interfaceRegisterable- Parameters:
registry- the registry to register with
-
getDescription
Gets the tool description.- Returns:
- the description
-
getInputClass
Gets the input class for JSON deserialization.- Returns:
- the input class, or null if not specified
-
getDefinition
Gets the tool definition for use in model requests.- Returns:
- the tool definition
-
respond
Constructs a tool response for an interrupted tool request.This method is used when resuming generation after an interrupt. It creates a tool response part that can be passed to
ResumeOptions.getRespond().Example usage:
// Get interrupt from response Part interrupt = response.getInterrupts().get(0); // Create response with user-provided data Part responseData = tool.respond(interrupt, userConfirmation); // Resume generation ModelResponse resumed = genkit.generate(GenerateOptions.builder().messages(response.getMessages()) .resume(ResumeOptions.builder().respond(responseData).build()).build());- Parameters:
interrupt- the interrupted tool request partoutput- the output data to respond with- Returns:
- a tool response part
-
respond
Constructs a tool response for an interrupted tool request with metadata.- Parameters:
interrupt- the interrupted tool request partoutput- the output data to respond withmetadata- optional metadata to include in the response- Returns:
- a tool response part
-
restart
Constructs a restart request for an interrupted tool.This method creates a tool request that will cause the tool to be re-executed. The resumed metadata will be passed to the tool handler.
Example usage:
// Get interrupt from response Part interrupt = response.getInterrupts().get(0); // Create restart request with confirmation metadata Part restartRequest = tool.restart(interrupt, Map.of("confirmed", true)); // Resume generation ModelResponse resumed = genkit.generate(GenerateOptions.builder().messages(response.getMessages()) .resume(ResumeOptions.builder().restart(restartRequest).build()).build());- Parameters:
interrupt- the interrupted tool request partresumedMetadata- metadata to pass to the tool handler's resumed context- Returns:
- a tool request part for restart
-
restart
Constructs a restart request with replacement input.- Parameters:
interrupt- the interrupted tool request partresumedMetadata- metadata to pass to the tool handler's resumed contextreplaceInput- optional new input to use instead of the original- Returns:
- a tool request part for restart
-