Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
InTheCloudDan committed Nov 4, 2024
1 parent 9c6fdd3 commit 5899e9d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
34 changes: 29 additions & 5 deletions packages/sdk/ai/src/LDAIConfigTracker.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { LDClient, LDContext } from '@launchdarkly/node-server-sdk';

import {
BedrockTokenUsage,
createBedrockTokenUsage,
FeedbackKind,
OpenAITokenUsage,
TokenMetrics,
TokenUsage,
UnderscoreTokenUsage,
} from './api/metrics';
Expand Down Expand Up @@ -33,7 +33,7 @@ export class LDAIConfigTracker {
this.ldClient.track('$ld:ai:duration:total', this.context, this.getTrackData(), duration);
}

async trackDurationOf(func: Function, ...args: any[]): Promise<any> {
async trackDurationOf(func: (...args: any[]) => Promise<any>, ...args: any[]): Promise<any> {
const startTime = Date.now();
const result = await func(...args);
const endTime = Date.now();
Expand All @@ -58,7 +58,7 @@ export class LDAIConfigTracker {
this.ldClient.track('$ld:ai:generation', this.context, this.getTrackData(), generation);
}

async trackOpenAI(func: Function, ...args: any[]): Promise<any> {
async trackOpenAI(func: (...args: any[]) => Promise<any>, ...args: any[]): Promise<any> {
const result = await this.trackDurationOf(func, ...args);
this.trackGeneration(1);
if (result.usage) {
Expand Down Expand Up @@ -90,8 +90,8 @@ export class LDAIConfigTracker {
return res;
}

trackTokens(tokens: TokenUsage | UnderscoreTokenUsage | BedrockTokenUsage): void {
const tokenMetrics = tokens.toMetrics();
trackTokens(tokens: TokenUsage | UnderscoreTokenUsage | { totalTokens: number; inputTokens: number; outputTokens: number }): void {
const tokenMetrics = toMetrics(tokens);
if (tokenMetrics.total > 0) {
this.ldClient.track(
'$ld:ai:tokens:total',
Expand All @@ -118,3 +118,27 @@ export class LDAIConfigTracker {
}
}
}

function toMetrics(
usage: TokenUsage | UnderscoreTokenUsage | { totalTokens: number; inputTokens: number; outputTokens: number },
): TokenMetrics {
if ('inputTokens' in usage && 'outputTokens' in usage) {
// Bedrock usage
return {
total: usage.totalTokens,
input: usage.inputTokens,
output: usage.outputTokens,
};
}

// OpenAI usage (both camelCase and snake_case)
return {
total: 'total_tokens' in usage ? usage.total_tokens! : (usage as TokenUsage).totalTokens ?? 0,
input:
'prompt_tokens' in usage ? usage.prompt_tokens! : (usage as TokenUsage).promptTokens ?? 0,
output:
'completion_tokens' in usage
? usage.completion_tokens!
: (usage as TokenUsage).completionTokens ?? 0,
};
}
11 changes: 8 additions & 3 deletions packages/sdk/ai/src/api/config/LDAIConfigTracker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { BedrockTokenUsage, FeedbackKind, TokenUsage, UnderscoreTokenUsage } from '../metrics';
import { FeedbackKind, TokenUsage, UnderscoreTokenUsage } from '../metrics';

export interface LDAIConfigTracker {
trackDuration: (duration: number) => void;
trackTokens: (tokens: TokenUsage | UnderscoreTokenUsage | BedrockTokenUsage) => void;
trackTokens: (
tokens:
| TokenUsage
| UnderscoreTokenUsage
| { totalTokens: number; inputTokens: number; outputTokens: number },
) => void;
trackError: (error: number) => void;
trackGeneration: (generation: number) => void;
trackFeedback: (feedback: { kind: FeedbackKind }) => void;
trackDurationOf: (func: Function, ...args: any[]) => any;
trackDurationOf: (func: (...args: any[]) => Promise<any>, ...args: any[]) => Promise<any>;
trackOpenAI: (func: Function, ...args: any[]) => any;
trackBedrockConverse: (res: any) => any;
}

0 comments on commit 5899e9d

Please sign in to comment.