Package com.google.genkit.ai.telemetry
Class ModelTelemetryHelper
java.lang.Object
com.google.genkit.ai.telemetry.ModelTelemetryHelper
ModelTelemetryHelper provides utilities for recording model telemetry.
This helper should be used when invoking models to automatically record metrics like latency, token counts, and error rates.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for model execution.static interfaceFunctional interface for streaming model execution. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic UsagecalculateBasicUsage(ModelRequest request, ModelResponse response) Calculates basic usage statistics from request and response.static ModelResponserunWithTelemetry(String modelName, String featureName, String path, ModelRequest request, ModelTelemetryHelper.ModelExecutor modelFn) Executes a model call with automatic telemetry recording.static ModelResponserunWithTelemetryStreaming(String modelName, String featureName, String path, ModelRequest request, ModelTelemetryHelper.StreamingModelExecutor modelFn) Executes a streaming model call with automatic telemetry recording.
-
Constructor Details
-
ModelTelemetryHelper
public ModelTelemetryHelper()
-
-
Method Details
-
runWithTelemetry
public static ModelResponse runWithTelemetry(String modelName, String featureName, String path, ModelRequest request, ModelTelemetryHelper.ModelExecutor modelFn) throws GenkitException Executes a model call with automatic telemetry recording.- Parameters:
modelName- the model namefeatureName- the feature/flow namepath- the span pathrequest- the model requestmodelFn- the function that executes the model- Returns:
- the model response
- Throws:
GenkitException- if model execution fails
-
calculateBasicUsage
Calculates basic usage statistics from request and response.- Parameters:
request- the model requestresponse- the model response- Returns:
- calculated usage statistics
-
runWithTelemetryStreaming
public static ModelResponse runWithTelemetryStreaming(String modelName, String featureName, String path, ModelRequest request, ModelTelemetryHelper.StreamingModelExecutor modelFn) throws GenkitException Executes a streaming model call with automatic telemetry recording.- Parameters:
modelName- the model namefeatureName- the feature/flow namepath- the span pathrequest- the model requestmodelFn- the function that executes the model with streaming- Returns:
- the model response
- Throws:
GenkitException- if model execution fails
-