Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading