Skip to content

Commit

Permalink
feat: Add getSummary method to the tracker. (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion authored Dec 4, 2024
1 parent 7a93a45 commit 4df902d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/sdk/server-ai/__tests__/LDAIConfigTrackerImpl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,39 @@ it('only tracks non-zero token counts', () => {
expect.anything(),
);
});

it('returns empty summary when no metrics tracked', () => {
const tracker = new LDAIConfigTrackerImpl(mockLdClient, configKey, versionKey, testContext);

const summary = tracker.getSummary();

expect(summary).toEqual({});
});

it('summarizes tracked metrics', () => {
const tracker = new LDAIConfigTrackerImpl(mockLdClient, configKey, versionKey, testContext);

tracker.trackDuration(1000);
tracker.trackTokens({
total: 100,
input: 40,
output: 60,
});
tracker.trackFeedback({ kind: LDFeedbackKind.Positive });
tracker.trackSuccess();

const summary = tracker.getSummary();

expect(summary).toEqual({
durationMs: 1000,
tokens: {
total: 100,
input: 40,
output: 60,
},
feedback: {
kind: 'positive',
},
success: true,
});
});
14 changes: 14 additions & 0 deletions packages/sdk/server-ai/src/LDAIConfigTrackerImpl.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { LDContext } from '@launchdarkly/js-server-sdk-common';

import { LDAIConfigTracker } from './api/config';
import { LDAIMetricSummary } from './api/config/LDAIConfigTracker';
import { createBedrockTokenUsage, LDFeedbackKind, LDTokenUsage } from './api/metrics';
import { createOpenAiUsage } from './api/metrics/OpenAiUsage';
import { LDClientMin } from './LDClientMin';

export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
private _trackedMetrics: LDAIMetricSummary = {};

constructor(
private _ldClient: LDClientMin,
private _configKey: string,
Expand All @@ -21,6 +24,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
}

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

Expand All @@ -34,6 +38,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
}

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

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

Expand Down Expand Up @@ -88,6 +94,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
}

trackTokens(tokens: LDTokenUsage): void {
this._trackedMetrics.tokens = tokens;
const trackData = this._getTrackData();
if (tokens.total > 0) {
this._ldClient.track('$ld:ai:tokens:total', this._context, trackData, tokens.total);
Expand All @@ -99,4 +106,11 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
this._ldClient.track('$ld:ai:tokens:output', this._context, trackData, tokens.output);
}
}

/**
* Get a summary of the tracked metrics.
*/
getSummary(): LDAIMetricSummary {
return { ...this._trackedMetrics };
}
}
30 changes: 30 additions & 0 deletions packages/sdk/server-ai/src/api/config/LDAIConfigTracker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { LDFeedbackKind, LDTokenUsage } from '../metrics';

/**
* Metrics which have been tracked.
*/
export interface LDAIMetricSummary {
/**
* The duration of generation.
*/
durationMs?: number;

/**
* Information about token usage.
*/
tokens?: LDTokenUsage;

/**
* Was generation successful.
*/
success?: boolean;

/**
* Any sentiment about the generation.
*/
feedback?: { kind: LDFeedbackKind };
}

/**
* The LDAIConfigTracker is used to track various details about AI operations.
*/
Expand Down Expand Up @@ -76,4 +101,9 @@ export interface LDAIConfigTracker {
>(
res: TRes,
): TRes;

/**
* Get a summary of the tracked metrics.
*/
getSummary(): LDAIMetricSummary;
}

0 comments on commit 4df902d

Please sign in to comment.