Skip to content

Commit

Permalink
feat: track timeToFirstToken in LDAIConfigTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
moribellamy committed Jan 22, 2025
1 parent 1ff96ce commit 1fc9e76
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/sdk/server-ai/__tests__/LDAIConfigTrackerImpl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ it('tracks duration of async function', async () => {
);
});

it('tracks time to first token', () => {
const tracker = new LDAIConfigTrackerImpl(mockLdClient, configKey, variationKey, testContext);
tracker.trackTimeToFirstToken(1000);

expect(mockTrack).toHaveBeenCalledWith(
'$ld:ai:tokens:ttf',
testContext,
{ configKey, variationKey },
1000,
);
});

it('tracks positive feedback', () => {
const tracker = new LDAIConfigTrackerImpl(mockLdClient, configKey, variationKey, testContext);
tracker.trackFeedback({ kind: LDFeedbackKind.Positive });
Expand Down
10 changes: 10 additions & 0 deletions packages/sdk/server-ai/src/LDAIConfigTrackerImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
}
}

trackTimeToFirstToken(timeToFirstTokenMs: number) {
this._trackedMetrics.timeToFirstTokenMs = timeToFirstTokenMs;
this._ldClient.track(
'$ld:ai:tokens:ttf',
this._context,
this._getTrackData(),
timeToFirstTokenMs,
);
}

trackFeedback(feedback: { kind: LDFeedbackKind }): void {
this._trackedMetrics.feedback = feedback;
if (feedback.kind === LDFeedbackKind.Positive) {
Expand Down
12 changes: 12 additions & 0 deletions packages/sdk/server-ai/src/api/config/LDAIConfigTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export interface LDAIMetricSummary {
* Any sentiment about the generation.
*/
feedback?: { kind: LDFeedbackKind };

/**
* Time to first token for this generation.
*/
timeToFirstTokenMs?: number;
}

/**
Expand Down Expand Up @@ -62,6 +67,13 @@ export interface LDAIConfigTracker {
*/
trackFeedback(feedback: { kind: LDFeedbackKind }): void;

/**
* Track the time to first token for this generation.
*
* @param timeToFirstTokenMs The duration in milliseconds.
*/
trackTimeToFirstToken(timeToFirstTokenMs: number): void;

/**
* Track the duration of execution of the provided function.
*
Expand Down

0 comments on commit 1fc9e76

Please sign in to comment.