|
1 | | -import { BedrockTokenUsage, FeedbackKind, TokenUsage, UnderscoreTokenUsage } from '../metrics'; |
2 | | - |
3 | | -export interface LDAIConfigTracker { |
4 | | - trackDuration: (duration: number) => void; |
5 | | - trackTokens: (tokens: TokenUsage | UnderscoreTokenUsage | BedrockTokenUsage) => void; |
6 | | - trackError: (error: number) => void; |
7 | | - trackGeneration: (generation: number) => void; |
8 | | - trackFeedback: (feedback: { kind: FeedbackKind }) => void; |
| 1 | +import { LDClient, LDContext } from '@launchdarkly/node-server-sdk'; |
| 2 | + |
| 3 | +import { |
| 4 | + BedrockTokenUsage, |
| 5 | + FeedbackKind, |
| 6 | + OpenAITokenUsage, |
| 7 | + TokenUsage, |
| 8 | + UnderscoreTokenUsage, |
| 9 | +} from '../metrics'; |
| 10 | + |
| 11 | +export class LDAIConfigTracker { |
| 12 | + private ldClient: LDClient; |
| 13 | + private variationId: string; |
| 14 | + private configKey: string; |
| 15 | + private context: LDContext; |
| 16 | + |
| 17 | + constructor(ldClient: LDClient, configKey: string, variationId: string,context: LDContext) { |
| 18 | + this.ldClient = ldClient; |
| 19 | + this.variationId = variationId; |
| 20 | + this.configKey = configKey; |
| 21 | + this.context = context; |
| 22 | + } |
| 23 | + |
| 24 | + private getTrackData() { |
| 25 | + return { |
| 26 | + variationId: this.variationId, |
| 27 | + configKey: this.configKey, |
| 28 | + }; |
| 29 | + } |
| 30 | + |
| 31 | + trackDuration(duration: number): void { |
| 32 | + this.ldClient.track('$ld:ai:duration:total', this.context, this.getTrackData(), duration); |
| 33 | + } |
| 34 | + |
| 35 | + async trackDurationOf(func: Function, ...args: any[]): Promise<any> { |
| 36 | + const startTime = Date.now(); |
| 37 | + const result = await func(...args); |
| 38 | + const endTime = Date.now(); |
| 39 | + const duration = endTime - startTime; // duration in milliseconds |
| 40 | + this.trackDuration(duration); |
| 41 | + return result; |
| 42 | + } |
| 43 | + |
| 44 | + trackError(error: number): void { |
| 45 | + this.ldClient.track('$ld:ai:error', this.context, this.getTrackData(), error); |
| 46 | + } |
| 47 | + |
| 48 | + trackFeedback(feedback: { kind: FeedbackKind }): void { |
| 49 | + if (feedback.kind === FeedbackKind.Positive) { |
| 50 | + this.ldClient.track('$ld:ai:feedback:user:positive', this.context, this.getTrackData(), 1); |
| 51 | + } else if (feedback.kind === FeedbackKind.Negative) { |
| 52 | + this.ldClient.track('$ld:ai:feedback:user:negative', this.context, this.getTrackData(), 1); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + trackGeneration(generation: number): void { |
| 57 | + this.ldClient.track('$ld:ai:generation', this.context, this.getTrackData(), generation); |
| 58 | + } |
| 59 | + |
| 60 | + async trackOpenAI(func: Function, ...args: any[]): Promise<any> { |
| 61 | + const result = await this.trackDurationOf(func, ...args); |
| 62 | + this.trackGeneration(1); |
| 63 | + if (result.usage) { |
| 64 | + this.trackTokens(new OpenAITokenUsage(result.usage)); |
| 65 | + } |
| 66 | + return result; |
| 67 | + } |
| 68 | + |
| 69 | + async trackBedrockConverse(res: any): Promise<any> { |
| 70 | + if (res.$metadata?.httpStatusCode === 200) { |
| 71 | + this.trackGeneration(1); |
| 72 | + } else if (res.$metadata?.httpStatusCode >= 400) { |
| 73 | + this.trackError(res.$metadata.httpStatusCode); |
| 74 | + } |
| 75 | + if (res.metrics) { |
| 76 | + this.trackDuration(res.metrics.latencyMs); |
| 77 | + } |
| 78 | + if (res.usage) { |
| 79 | + this.trackTokens(new BedrockTokenUsage(res.usage)); |
| 80 | + } |
| 81 | + return res; |
| 82 | + } |
| 83 | + |
| 84 | + trackTokens(tokens: TokenUsage | UnderscoreTokenUsage | BedrockTokenUsage): void { |
| 85 | + const tokenMetrics = tokens.toMetrics(); |
| 86 | + if (tokenMetrics.total > 0) { |
| 87 | + this.ldClient.track( |
| 88 | + '$ld:ai:tokens:total', |
| 89 | + this.context, |
| 90 | + this.getTrackData(), |
| 91 | + tokenMetrics.total, |
| 92 | + ); |
| 93 | + } |
| 94 | + if (tokenMetrics.input > 0) { |
| 95 | + this.ldClient.track( |
| 96 | + '$ld:ai:tokens:input', |
| 97 | + this.context, |
| 98 | + this.getTrackData(), |
| 99 | + tokenMetrics.input, |
| 100 | + ); |
| 101 | + } |
| 102 | + if (tokenMetrics.output > 0) { |
| 103 | + this.ldClient.track( |
| 104 | + '$ld:ai:tokens:output', |
| 105 | + this.context, |
| 106 | + this.getTrackData(), |
| 107 | + tokenMetrics.output, |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
9 | 111 | } |
0 commit comments