Package com.google.genkit.ai.middleware
Class BaseGenerationMiddleware
java.lang.Object
com.google.genkit.ai.middleware.BaseGenerationMiddleware
- All Implemented Interfaces:
GenerationMiddleware
BaseGenerationMiddleware provides default pass-through implementations for all three hooks.
Extend this class and override only the hooks you need.
Example:
public class TimingMiddleware extends BaseGenerationMiddleware {
@Override
public String name() { return "timing"; }
@Override
public GenerationMiddleware newInstance() { return new TimingMiddleware(); }
@Override
public ModelResponse wrapModel(ActionContext ctx, ModelParams params, ModelNext next)
throws GenkitException {
long start = System.currentTimeMillis();
ModelResponse resp = next.apply(ctx, params);
System.out.println("Model call took " + (System.currentTimeMillis() - start) + "ms");
return resp;
}
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiontools()Returns additional tools to make available during generation.wrapGenerate(ActionContext ctx, GenerateParams params, GenerateNext next) Wraps each iteration of the generate tool loop.wrapModel(ActionContext ctx, ModelParams params, ModelNext next) Wraps each model API call.wrapTool(ActionContext ctx, ToolParams params, ToolNext next) Wraps each tool execution.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.google.genkit.ai.middleware.GenerationMiddleware
name, newInstance
-
Constructor Details
-
BaseGenerationMiddleware
public BaseGenerationMiddleware()
-
-
Method Details
-
wrapGenerate
public ModelResponse wrapGenerate(ActionContext ctx, GenerateParams params, GenerateNext next) throws GenkitException Description copied from interface:GenerationMiddlewareWraps each iteration of the generate tool loop.- Specified by:
wrapGeneratein interfaceGenerationMiddleware- Parameters:
ctx- the action contextparams- the generate parameters including the current request and iterationnext- the next function in the chain- Returns:
- the model response
- Throws:
GenkitException- if processing fails
-
wrapModel
public ModelResponse wrapModel(ActionContext ctx, ModelParams params, ModelNext next) throws GenkitException Description copied from interface:GenerationMiddlewareWraps each model API call.- Specified by:
wrapModelin interfaceGenerationMiddleware- Parameters:
ctx- the action contextparams- the model parameters including the requestnext- the next function in the chain- Returns:
- the model response
- Throws:
GenkitException- if processing fails
-
wrapTool
Description copied from interface:GenerationMiddlewareWraps each tool execution. May be called concurrently when multiple tools execute in parallel. Implementations must be safe for concurrent use.- Specified by:
wrapToolin interfaceGenerationMiddleware- Parameters:
ctx- the action contextparams- the tool parameters including the request part and resolved toolnext- the next function in the chain- Returns:
- the tool response part (includes part-level metadata)
- Throws:
GenkitException- if processing fails
-
tools
Description copied from interface:GenerationMiddlewareReturns additional tools to make available during generation. These tools are dynamically added when the middleware is used.- Specified by:
toolsin interfaceGenerationMiddleware- Returns:
- the list of additional tools, or empty list if none
-