Package com.google.genkit.ai.middleware
Class ModelParams
java.lang.Object
com.google.genkit.ai.middleware.ModelParams
-
Constructor Summary
ConstructorsConstructorDescriptionModelParams(ModelRequest request, Consumer<ModelResponseChunk> streamCallback) Creates ModelParams. -
Method Summary
Modifier and TypeMethodDescriptionReturns the model request about to be sent.Returns the streaming callback, or null if not streaming.withRequest(ModelRequest request) Returns a new ModelParams with the given request, preserving the stream callback.withStreamCallback(Consumer<ModelResponseChunk> streamCallback) Returns a new ModelParams with the given stream callback, preserving the request.
-
Constructor Details
-
ModelParams
Creates ModelParams.- Parameters:
request- the model request about to be sentstreamCallback- the streaming callback, or null if not streaming
-
-
Method Details
-
getRequest
Returns the model request about to be sent. -
getStreamCallback
Returns the streaming callback, or null if not streaming. -
withRequest
Returns a new ModelParams with the given request, preserving the stream callback. -
withStreamCallback
Returns a new ModelParams with the given stream callback, preserving the request.This is the primary mechanism for middleware to transform streaming output. A
wrapModelmiddleware can wrap the original callback with one that intercepts, buffers, splits, combines, or filters chunks before forwarding them to the original callback.Example — "smooth stream" middleware that splits large chunks:
@Override public ModelResponse wrapModel(ActionContext ctx, ModelParams params, ModelNext next) { Consumer<ModelResponseChunk> original = params.getStreamCallback(); if (original == null) return next.apply(ctx, params); Consumer<ModelResponseChunk> smoothed = chunk -> { String text = chunk.getText(); // Split into smaller pieces and forward each for (String piece : splitText(text, 20)) { original.accept(ModelResponseChunk.text(piece)); } }; return next.apply(ctx, params.withStreamCallback(smoothed)); }- Parameters:
streamCallback- the new streaming callback, or null to disable streaming
-