Skip to content

Commit 4df902d

Browse files
authored
feat: Add getSummary method to the tracker. (#698)
1 parent 7a93a45 commit 4df902d

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

packages/sdk/server-ai/__tests__/LDAIConfigTrackerImpl.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,39 @@ it('only tracks non-zero token counts', () => {
268268
expect.anything(),
269269
);
270270
});
271+
272+
it('returns empty summary when no metrics tracked', () => {
273+
const tracker = new LDAIConfigTrackerImpl(mockLdClient, configKey, versionKey, testContext);
274+
275+
const summary = tracker.getSummary();
276+
277+
expect(summary).toEqual({});
278+
});
279+
280+
it('summarizes tracked metrics', () => {
281+
const tracker = new LDAIConfigTrackerImpl(mockLdClient, configKey, versionKey, testContext);
282+
283+
tracker.trackDuration(1000);
284+
tracker.trackTokens({
285+
total: 100,
286+
input: 40,
287+
output: 60,
288+
});
289+
tracker.trackFeedback({ kind: LDFeedbackKind.Positive });
290+
tracker.trackSuccess();
291+
292+
const summary = tracker.getSummary();
293+
294+
expect(summary).toEqual({
295+
durationMs: 1000,
296+
tokens: {
297+
total: 100,
298+
input: 40,
299+
output: 60,
300+
},
301+
feedback: {
302+
kind: 'positive',
303+
},
304+
success: true,
305+
});
306+
});

packages/sdk/server-ai/src/LDAIConfigTrackerImpl.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { LDContext } from '@launchdarkly/js-server-sdk-common';
22

33
import { LDAIConfigTracker } from './api/config';
4+
import { LDAIMetricSummary } from './api/config/LDAIConfigTracker';
45
import { createBedrockTokenUsage, LDFeedbackKind, LDTokenUsage } from './api/metrics';
56
import { createOpenAiUsage } from './api/metrics/OpenAiUsage';
67
import { LDClientMin } from './LDClientMin';
78

89
export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
10+
private _trackedMetrics: LDAIMetricSummary = {};
11+
912
constructor(
1013
private _ldClient: LDClientMin,
1114
private _configKey: string,
@@ -21,6 +24,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
2124
}
2225

2326
trackDuration(duration: number): void {
27+
this._trackedMetrics.durationMs = duration;
2428
this._ldClient.track('$ld:ai:duration:total', this._context, this._getTrackData(), duration);
2529
}
2630

@@ -34,6 +38,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
3438
}
3539

3640
trackFeedback(feedback: { kind: LDFeedbackKind }): void {
41+
this._trackedMetrics.feedback = feedback;
3742
if (feedback.kind === LDFeedbackKind.Positive) {
3843
this._ldClient.track('$ld:ai:feedback:user:positive', this._context, this._getTrackData(), 1);
3944
} else if (feedback.kind === LDFeedbackKind.Negative) {
@@ -42,6 +47,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
4247
}
4348

4449
trackSuccess(): void {
50+
this._trackedMetrics.success = true;
4551
this._ldClient.track('$ld:ai:generation', this._context, this._getTrackData(), 1);
4652
}
4753

@@ -88,6 +94,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
8894
}
8995

9096
trackTokens(tokens: LDTokenUsage): void {
97+
this._trackedMetrics.tokens = tokens;
9198
const trackData = this._getTrackData();
9299
if (tokens.total > 0) {
93100
this._ldClient.track('$ld:ai:tokens:total', this._context, trackData, tokens.total);
@@ -99,4 +106,11 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
99106
this._ldClient.track('$ld:ai:tokens:output', this._context, trackData, tokens.output);
100107
}
101108
}
109+
110+
/**
111+
* Get a summary of the tracked metrics.
112+
*/
113+
getSummary(): LDAIMetricSummary {
114+
return { ...this._trackedMetrics };
115+
}
102116
}

packages/sdk/server-ai/src/api/config/LDAIConfigTracker.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
import { LDFeedbackKind, LDTokenUsage } from '../metrics';
22

3+
/**
4+
* Metrics which have been tracked.
5+
*/
6+
export interface LDAIMetricSummary {
7+
/**
8+
* The duration of generation.
9+
*/
10+
durationMs?: number;
11+
12+
/**
13+
* Information about token usage.
14+
*/
15+
tokens?: LDTokenUsage;
16+
17+
/**
18+
* Was generation successful.
19+
*/
20+
success?: boolean;
21+
22+
/**
23+
* Any sentiment about the generation.
24+
*/
25+
feedback?: { kind: LDFeedbackKind };
26+
}
27+
328
/**
429
* The LDAIConfigTracker is used to track various details about AI operations.
530
*/
@@ -76,4 +101,9 @@ export interface LDAIConfigTracker {
76101
>(
77102
res: TRes,
78103
): TRes;
104+
105+
/**
106+
* Get a summary of the tracked metrics.
107+
*/
108+
getSummary(): LDAIMetricSummary;
79109
}

0 commit comments

Comments
 (0)