@launchdarkly/server-sdk-ai 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,26 +1,30 @@
1
1
  import { LDContext } from '@launchdarkly/js-server-sdk-common';
2
2
 
3
3
  import { LDAIConfigTracker } from './api/config';
4
+ import { LDAIMetricSummary } from './api/config/LDAIConfigTracker';
4
5
  import { createBedrockTokenUsage, LDFeedbackKind, LDTokenUsage } from './api/metrics';
5
6
  import { createOpenAiUsage } from './api/metrics/OpenAiUsage';
6
7
  import { LDClientMin } from './LDClientMin';
7
8
 
8
9
  export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
10
+ private _trackedMetrics: LDAIMetricSummary = {};
11
+
9
12
  constructor(
10
13
  private _ldClient: LDClientMin,
11
14
  private _configKey: string,
12
- private _versionKey: string,
15
+ private _variationKey: string,
13
16
  private _context: LDContext,
14
17
  ) {}
15
18
 
16
- private _getTrackData(): { versionKey: string; configKey: string } {
19
+ private _getTrackData(): { variationKey: string; configKey: string } {
17
20
  return {
18
- versionKey: this._versionKey,
21
+ variationKey: this._variationKey,
19
22
  configKey: this._configKey,
20
23
  };
21
24
  }
22
25
 
23
26
  trackDuration(duration: number): void {
27
+ this._trackedMetrics.durationMs = duration;
24
28
  this._ldClient.track('$ld:ai:duration:total', this._context, this._getTrackData(), duration);
25
29
  }
26
30
 
@@ -34,6 +38,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
34
38
  }
35
39
 
36
40
  trackFeedback(feedback: { kind: LDFeedbackKind }): void {
41
+ this._trackedMetrics.feedback = feedback;
37
42
  if (feedback.kind === LDFeedbackKind.Positive) {
38
43
  this._ldClient.track('$ld:ai:feedback:user:positive', this._context, this._getTrackData(), 1);
39
44
  } else if (feedback.kind === LDFeedbackKind.Negative) {
@@ -42,6 +47,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
42
47
  }
43
48
 
44
49
  trackSuccess(): void {
50
+ this._trackedMetrics.success = true;
45
51
  this._ldClient.track('$ld:ai:generation', this._context, this._getTrackData(), 1);
46
52
  }
47
53
 
@@ -88,6 +94,7 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
88
94
  }
89
95
 
90
96
  trackTokens(tokens: LDTokenUsage): void {
97
+ this._trackedMetrics.tokens = tokens;
91
98
  const trackData = this._getTrackData();
92
99
  if (tokens.total > 0) {
93
100
  this._ldClient.track('$ld:ai:tokens:total', this._context, trackData, tokens.total);
@@ -99,4 +106,11 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
99
106
  this._ldClient.track('$ld:ai:tokens:output', this._context, trackData, tokens.output);
100
107
  }
101
108
  }
109
+
110
+ /**
111
+ * Get a summary of the tracked metrics.
112
+ */
113
+ getSummary(): LDAIMetricSummary {
114
+ return { ...this._trackedMetrics };
115
+ }
102
116
  }
@@ -7,7 +7,7 @@ export interface LDModelConfig {
7
7
  /**
8
8
  * The ID of the model.
9
9
  */
10
- id: string;
10
+ name: string;
11
11
 
12
12
  /**
13
13
  * Model specific parameters.
@@ -24,7 +24,7 @@ export interface LDProviderConfig {
24
24
  /**
25
25
  * The ID of the provider.
26
26
  */
27
- id: string;
27
+ name: string;
28
28
  }
29
29
 
30
30
  /**
@@ -1,5 +1,30 @@
1
1
  import { LDFeedbackKind, LDTokenUsage } from '../metrics';
2
2
 
3
+ /**
4
+ * Metrics which have been tracked.
5
+ */
6
+ export interface LDAIMetricSummary {
7
+ /**
8
+ * The duration of generation.
9
+ */
10
+ durationMs?: number;
11
+
12
+ /**
13
+ * Information about token usage.
14
+ */
15
+ tokens?: LDTokenUsage;
16
+
17
+ /**
18
+ * Was generation successful.
19
+ */
20
+ success?: boolean;
21
+
22
+ /**
23
+ * Any sentiment about the generation.
24
+ */
25
+ feedback?: { kind: LDFeedbackKind };
26
+ }
27
+
3
28
  /**
4
29
  * The LDAIConfigTracker is used to track various details about AI operations.
5
30
  */
@@ -76,4 +101,9 @@ export interface LDAIConfigTracker {
76
101
  >(
77
102
  res: TRes,
78
103
  ): TRes;
104
+
105
+ /**
106
+ * Get a summary of the tracked metrics.
107
+ */
108
+ getSummary(): LDAIMetricSummary;
79
109
  }