|
1 | 1 | package com.launchdarkly.sdk.server.ai; |
2 | 2 |
|
| 3 | +import com.launchdarkly.sdk.server.ai.datamodel.LDAITrackingTypes.FeedbackKind; |
| 4 | +import com.launchdarkly.sdk.server.ai.datamodel.LDAITrackingTypes.JudgeResult; |
| 5 | +import com.launchdarkly.sdk.server.ai.datamodel.LDAITrackingTypes.Metrics; |
| 6 | +import com.launchdarkly.sdk.server.ai.datamodel.LDAITrackingTypes.MetricSummary; |
| 7 | +import com.launchdarkly.sdk.server.ai.datamodel.LDAITrackingTypes.TokenUsage; |
| 8 | +import com.launchdarkly.sdk.server.ai.datamodel.LDAITrackingTypes.TrackData; |
| 9 | + |
| 10 | +import java.time.Duration; |
| 11 | +import java.util.List; |
| 12 | +import java.util.concurrent.Callable; |
| 13 | +import java.util.function.Function; |
| 14 | + |
3 | 15 | /** |
4 | | - * Reports events related to a single AI run of an {@link AIConfig}. |
| 16 | + * Reports metrics related to a single AI run of an {@link AIConfig}. |
5 | 17 | * <p> |
6 | | - * A tracker is obtained from a retrieved config via {@link AIConfig#createTracker()}. Each tracker |
7 | | - * corresponds to one AI run and is used to record metrics such as model usage, duration, and |
8 | | - * feedback against the AI Config it was created from. |
| 18 | + * A tracker is obtained from a retrieved config via {@link AIConfig#createTracker()}, or |
| 19 | + * reconstructed across process boundaries via |
| 20 | + * {@link LDAIClient#createTracker(String, com.launchdarkly.sdk.LDContext)}. Each tracker corresponds |
| 21 | + * to one AI run; every event it emits shares a {@code runId} (a UUIDv4) so LaunchDarkly can |
| 22 | + * correlate them in metrics views. Start a new run by calling {@link AIConfig#createTracker()} again. |
9 | 23 | * <p> |
10 | | - * <strong>This interface is an intentional placeholder.</strong> The metric- and feedback-reporting |
11 | | - * methods (and resumption-token support) are introduced in a later step of the AI SDK build-out; it |
12 | | - * is defined here so that the public config types expose a stable {@code createTracker()} surface. |
13 | | - * The only implementation in this release is an internal no-op. |
| 24 | + * <strong>Thread-safety.</strong> Implementations are safe to share across threads. The |
| 25 | + * "record-once" metrics ({@link #trackDuration}, {@link #trackTimeToFirstToken}, |
| 26 | + * {@link #trackSuccess}/{@link #trackError}, {@link #trackFeedback}, {@link #trackTokens}) each emit |
| 27 | + * at most once per tracker even under concurrent calls; later calls are ignored and logged. |
| 28 | + * {@link #trackToolCall}/{@link #trackToolCalls} and {@link #trackJudgeResult} may be called any |
| 29 | + * number of times and emit on every call. |
14 | 30 | */ |
15 | 31 | public interface LDAIConfigTracker { |
| 32 | + /** |
| 33 | + * Returns the correlation data attached to every event this tracker emits. |
| 34 | + * |
| 35 | + * @return the track data, never {@code null} |
| 36 | + */ |
| 37 | + TrackData getTrackData(); |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns a URL-safe Base64 token that encodes this tracker's {@code runId}, {@code configKey}, |
| 41 | + * {@code variationKey}, and {@code version}. |
| 42 | + * <p> |
| 43 | + * Pass it to {@link LDAIClient#createTracker(String, com.launchdarkly.sdk.LDContext)} to |
| 44 | + * reconstruct a tracker in another process so deferred events (for example user feedback) still |
| 45 | + * correlate with the original run. |
| 46 | + * |
| 47 | + * @return the resumption token, never {@code null} |
| 48 | + */ |
| 49 | + String getResumptionToken(); |
| 50 | + |
| 51 | + /** |
| 52 | + * Records the duration of the generation. |
| 53 | + * <p> |
| 54 | + * Records at most once per tracker; later calls are ignored. Negative durations (for example from |
| 55 | + * clock skew) are clamped to zero. |
| 56 | + * |
| 57 | + * @param duration the generation duration; must not be {@code null} |
| 58 | + */ |
| 59 | + void trackDuration(Duration duration); |
| 60 | + |
| 61 | + /** |
| 62 | + * Runs the given operation, recording its duration even if it throws. |
| 63 | + * <p> |
| 64 | + * This does not record success or error; use {@link #trackMetricsOf} for that. Because |
| 65 | + * {@link #trackDuration} records at most once, calling this twice on the same tracker re-runs the |
| 66 | + * operation but emits no second duration event. |
| 67 | + * |
| 68 | + * @param operation the operation to time |
| 69 | + * @param <T> the operation's result type |
| 70 | + * @return the operation's result |
| 71 | + * @throws Exception if the operation throws |
| 72 | + */ |
| 73 | + <T> T trackDurationOf(Callable<T> operation) throws Exception; |
| 74 | + |
| 75 | + /** |
| 76 | + * Records the time to first token for a streaming generation. |
| 77 | + * <p> |
| 78 | + * Records at most once per tracker; later calls are ignored. Negative values are clamped to zero. |
| 79 | + * |
| 80 | + * @param duration the time to first token; must not be {@code null} |
| 81 | + */ |
| 82 | + void trackTimeToFirstToken(Duration duration); |
| 83 | + |
| 84 | + /** |
| 85 | + * Records that the generation succeeded. |
| 86 | + * <p> |
| 87 | + * Success and error share state: only the first of {@link #trackSuccess}/{@link #trackError} |
| 88 | + * recorded on a tracker takes effect; later calls are ignored. |
| 89 | + */ |
| 90 | + void trackSuccess(); |
| 91 | + |
| 92 | + /** |
| 93 | + * Records that the generation failed. |
| 94 | + * <p> |
| 95 | + * Success and error share state: only the first of {@link #trackSuccess}/{@link #trackError} |
| 96 | + * recorded on a tracker takes effect; later calls are ignored. |
| 97 | + */ |
| 98 | + void trackError(); |
| 99 | + |
| 100 | + /** |
| 101 | + * Records end-user feedback about the generation. |
| 102 | + * <p> |
| 103 | + * Records at most once per tracker; later calls are ignored. |
| 104 | + * |
| 105 | + * @param kind the feedback sentiment; must not be {@code null} |
| 106 | + */ |
| 107 | + void trackFeedback(FeedbackKind kind); |
| 108 | + |
| 109 | + /** |
| 110 | + * Records token usage for the generation. |
| 111 | + * <p> |
| 112 | + * Records at most once per tracker; later calls are ignored. Negative counts are clamped to zero, |
| 113 | + * and an individual count is only emitted when it is greater than zero. |
| 114 | + * |
| 115 | + * @param tokens the token usage; must not be {@code null} |
| 116 | + */ |
| 117 | + void trackTokens(TokenUsage tokens); |
| 118 | + |
| 119 | + /** |
| 120 | + * Records a single tool invocation. May be called any number of times. |
| 121 | + * |
| 122 | + * @param toolKey the identifier of the invoked tool; must not be {@code null} |
| 123 | + */ |
| 124 | + void trackToolCall(String toolKey); |
| 125 | + |
| 126 | + /** |
| 127 | + * Records several tool invocations. May be called any number of times. |
| 128 | + * |
| 129 | + * @param toolKeys the identifiers of the invoked tools; must not be {@code null} |
| 130 | + */ |
| 131 | + void trackToolCalls(List<String> toolKeys); |
| 132 | + |
| 133 | + /** |
| 134 | + * Records a judge evaluation result. May be called any number of times. |
| 135 | + * <p> |
| 136 | + * No event is emitted when the result was not sampled, did not succeed, or carries no metric key |
| 137 | + * or score. A {@code null} score is treated as "no score" and is distinct from {@code 0.0}. |
| 138 | + * |
| 139 | + * @param result the judge result; must not be {@code null} |
| 140 | + */ |
| 141 | + void trackJudgeResult(JudgeResult result); |
| 142 | + |
| 143 | + /** |
| 144 | + * Runs the given operation, recording its duration and then its outcome and metrics. |
| 145 | + * <p> |
| 146 | + * The operation is timed via {@link #trackDurationOf}. If it throws, an error is recorded and the |
| 147 | + * exception is rethrown. Otherwise the extractor is applied to the result; if the extractor |
| 148 | + * throws, an error is recorded and the exception is rethrown. On success the extracted metrics |
| 149 | + * drive {@link #trackSuccess}/{@link #trackError}, {@link #trackTokens}, and |
| 150 | + * {@link #trackToolCalls}. |
| 151 | + * |
| 152 | + * @param metricsExtractor extracts {@link Metrics} from the operation's result |
| 153 | + * @param operation the AI operation to run |
| 154 | + * @param <T> the operation's result type |
| 155 | + * @return the operation's result |
| 156 | + * @throws Exception if the operation or the extractor throws |
| 157 | + */ |
| 158 | + <T> T trackMetricsOf(Function<? super T, Metrics> metricsExtractor, Callable<T> operation) |
| 159 | + throws Exception; |
| 160 | + |
| 161 | + /** |
| 162 | + * Returns an immutable snapshot of the metrics recorded on this tracker so far. |
| 163 | + * |
| 164 | + * @return the metric summary, never {@code null} |
| 165 | + */ |
| 166 | + MetricSummary getSummary(); |
16 | 167 | } |
0 commit comments