@launchdarkly/server-sdk-ai 0.14.0 → 0.15.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.
- package/CHANGELOG.md +18 -0
- package/dist/index.cjs +1117 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1022 -0
- package/dist/index.d.ts +1022 -0
- package/dist/index.js +1071 -0
- package/dist/index.js.map +1 -0
- package/package.json +22 -5
- package/__tests__/Judge.test.ts +0 -496
- package/__tests__/LDAIClientImpl.test.ts +0 -589
- package/__tests__/LDAIConfigTrackerImpl.test.ts +0 -815
- package/__tests__/TokenUsage.test.ts +0 -119
- package/__tests__/TrackedChat.test.ts +0 -230
- package/dist/package.json +0 -53
- package/dist/src/LDAIClientImpl.d.ts +0 -39
- package/dist/src/LDAIClientImpl.d.ts.map +0 -1
- package/dist/src/LDAIClientImpl.js +0 -164
- package/dist/src/LDAIClientImpl.js.map +0 -1
- package/dist/src/LDAIConfigTrackerImpl.d.ts +0 -73
- package/dist/src/LDAIConfigTrackerImpl.d.ts.map +0 -1
- package/dist/src/LDAIConfigTrackerImpl.js +0 -203
- package/dist/src/LDAIConfigTrackerImpl.js.map +0 -1
- package/dist/src/LDClientMin.d.ts +0 -11
- package/dist/src/LDClientMin.d.ts.map +0 -1
- package/dist/src/LDClientMin.js +0 -3
- package/dist/src/LDClientMin.js.map +0 -1
- package/dist/src/api/LDAIClient.d.ts +0 -258
- package/dist/src/api/LDAIClient.d.ts.map +0 -1
- package/dist/src/api/LDAIClient.js +0 -3
- package/dist/src/api/LDAIClient.js.map +0 -1
- package/dist/src/api/chat/TrackedChat.d.ts +0 -72
- package/dist/src/api/chat/TrackedChat.d.ts.map +0 -1
- package/dist/src/api/chat/TrackedChat.js +0 -125
- package/dist/src/api/chat/TrackedChat.js.map +0 -1
- package/dist/src/api/chat/index.d.ts +0 -3
- package/dist/src/api/chat/index.d.ts.map +0 -1
- package/dist/src/api/chat/index.js +0 -19
- package/dist/src/api/chat/index.js.map +0 -1
- package/dist/src/api/chat/types.d.ts +0 -22
- package/dist/src/api/chat/types.d.ts.map +0 -1
- package/dist/src/api/chat/types.js +0 -3
- package/dist/src/api/chat/types.js.map +0 -1
- package/dist/src/api/config/LDAIConfigTracker.d.ts +0 -203
- package/dist/src/api/config/LDAIConfigTracker.d.ts.map +0 -1
- package/dist/src/api/config/LDAIConfigTracker.js +0 -3
- package/dist/src/api/config/LDAIConfigTracker.js.map +0 -1
- package/dist/src/api/config/LDAIConfigUtils.d.ts +0 -2
- package/dist/src/api/config/LDAIConfigUtils.d.ts.map +0 -1
- package/dist/src/api/config/LDAIConfigUtils.js +0 -141
- package/dist/src/api/config/LDAIConfigUtils.js.map +0 -1
- package/dist/src/api/config/index.d.ts +0 -3
- package/dist/src/api/config/index.d.ts.map +0 -1
- package/dist/src/api/config/index.js +0 -18
- package/dist/src/api/config/index.js.map +0 -1
- package/dist/src/api/config/types.d.ts +0 -202
- package/dist/src/api/config/types.d.ts.map +0 -1
- package/dist/src/api/config/types.js +0 -3
- package/dist/src/api/config/types.js.map +0 -1
- package/dist/src/api/index.d.ts +0 -7
- package/dist/src/api/index.d.ts.map +0 -1
- package/dist/src/api/index.js +0 -23
- package/dist/src/api/index.js.map +0 -1
- package/dist/src/api/judge/EvaluationSchemaBuilder.d.ts +0 -11
- package/dist/src/api/judge/EvaluationSchemaBuilder.d.ts.map +0 -1
- package/dist/src/api/judge/EvaluationSchemaBuilder.js +0 -52
- package/dist/src/api/judge/EvaluationSchemaBuilder.js.map +0 -1
- package/dist/src/api/judge/Judge.d.ts +0 -63
- package/dist/src/api/judge/Judge.d.ts.map +0 -1
- package/dist/src/api/judge/Judge.js +0 -149
- package/dist/src/api/judge/Judge.js.map +0 -1
- package/dist/src/api/judge/index.d.ts +0 -3
- package/dist/src/api/judge/index.d.ts.map +0 -1
- package/dist/src/api/judge/index.js +0 -6
- package/dist/src/api/judge/index.js.map +0 -1
- package/dist/src/api/judge/types.d.ts +0 -35
- package/dist/src/api/judge/types.d.ts.map +0 -1
- package/dist/src/api/judge/types.js +0 -3
- package/dist/src/api/judge/types.js.map +0 -1
- package/dist/src/api/metrics/BedrockTokenUsage.d.ts +0 -7
- package/dist/src/api/metrics/BedrockTokenUsage.d.ts.map +0 -1
- package/dist/src/api/metrics/BedrockTokenUsage.js +0 -12
- package/dist/src/api/metrics/BedrockTokenUsage.js.map +0 -1
- package/dist/src/api/metrics/LDAIMetrics.d.ts +0 -17
- package/dist/src/api/metrics/LDAIMetrics.d.ts.map +0 -1
- package/dist/src/api/metrics/LDAIMetrics.js +0 -3
- package/dist/src/api/metrics/LDAIMetrics.js.map +0 -1
- package/dist/src/api/metrics/LDFeedbackKind.d.ts +0 -14
- package/dist/src/api/metrics/LDFeedbackKind.d.ts.map +0 -1
- package/dist/src/api/metrics/LDFeedbackKind.js +0 -18
- package/dist/src/api/metrics/LDFeedbackKind.js.map +0 -1
- package/dist/src/api/metrics/LDTokenUsage.d.ts +0 -18
- package/dist/src/api/metrics/LDTokenUsage.d.ts.map +0 -1
- package/dist/src/api/metrics/LDTokenUsage.js +0 -3
- package/dist/src/api/metrics/LDTokenUsage.js.map +0 -1
- package/dist/src/api/metrics/OpenAiUsage.d.ts +0 -7
- package/dist/src/api/metrics/OpenAiUsage.d.ts.map +0 -1
- package/dist/src/api/metrics/OpenAiUsage.js +0 -13
- package/dist/src/api/metrics/OpenAiUsage.js.map +0 -1
- package/dist/src/api/metrics/VercelAISDKTokenUsage.d.ts +0 -9
- package/dist/src/api/metrics/VercelAISDKTokenUsage.d.ts.map +0 -1
- package/dist/src/api/metrics/VercelAISDKTokenUsage.js +0 -13
- package/dist/src/api/metrics/VercelAISDKTokenUsage.js.map +0 -1
- package/dist/src/api/metrics/index.d.ts +0 -7
- package/dist/src/api/metrics/index.d.ts.map +0 -1
- package/dist/src/api/metrics/index.js +0 -23
- package/dist/src/api/metrics/index.js.map +0 -1
- package/dist/src/api/providers/AIProvider.d.ts +0 -52
- package/dist/src/api/providers/AIProvider.d.ts.map +0 -1
- package/dist/src/api/providers/AIProvider.js +0 -88
- package/dist/src/api/providers/AIProvider.js.map +0 -1
- package/dist/src/api/providers/AIProviderFactory.d.ts +0 -39
- package/dist/src/api/providers/AIProviderFactory.d.ts.map +0 -1
- package/dist/src/api/providers/AIProviderFactory.js +0 -102
- package/dist/src/api/providers/AIProviderFactory.js.map +0 -1
- package/dist/src/api/providers/index.d.ts +0 -3
- package/dist/src/api/providers/index.d.ts.map +0 -1
- package/dist/src/api/providers/index.js +0 -19
- package/dist/src/api/providers/index.js.map +0 -1
- package/dist/src/index.d.ts +0 -19
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -29
- package/dist/src/index.js.map +0 -1
- package/docs/.nojekyll +0 -1
- package/docs/assets/highlight.css +0 -92
- package/docs/assets/main.js +0 -58
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1379
- package/docs/classes/AIProvider.html +0 -210
- package/docs/classes/AIProviderFactory.html +0 -208
- package/docs/classes/Judge.html +0 -322
- package/docs/classes/TrackedChat.html +0 -322
- package/docs/enums/LDFeedbackKind.html +0 -115
- package/docs/functions/createBedrockTokenUsage.html +0 -94
- package/docs/functions/createOpenAiUsage.html +0 -94
- package/docs/functions/createVercelAISDKTokenUsage.html +0 -98
- package/docs/functions/initAi.html +0 -93
- package/docs/index.html +0 -136
- package/docs/interfaces/ChatResponse.html +0 -130
- package/docs/interfaces/EvalScore.html +0 -119
- package/docs/interfaces/JudgeResponse.html +0 -129
- package/docs/interfaces/LDAIAgentConfig.html +0 -167
- package/docs/interfaces/LDAIAgentConfigDefault.html +0 -155
- package/docs/interfaces/LDAIAgentRequestConfig.html +0 -129
- package/docs/interfaces/LDAIClient.html +0 -449
- package/docs/interfaces/LDAICompletionConfig.html +0 -167
- package/docs/interfaces/LDAICompletionConfigDefault.html +0 -155
- package/docs/interfaces/LDAIConfig.html +0 -148
- package/docs/interfaces/LDAIConfigDefault.html +0 -133
- package/docs/interfaces/LDAIConfigTracker.html +0 -510
- package/docs/interfaces/LDAIJudgeConfig.html +0 -167
- package/docs/interfaces/LDAIJudgeConfigDefault.html +0 -155
- package/docs/interfaces/LDAIMetrics.html +0 -121
- package/docs/interfaces/LDJudge.html +0 -119
- package/docs/interfaces/LDJudgeConfiguration.html +0 -109
- package/docs/interfaces/LDLogger.html +0 -189
- package/docs/interfaces/LDMessage.html +0 -119
- package/docs/interfaces/LDModelConfig.html +0 -139
- package/docs/interfaces/LDProviderConfig.html +0 -105
- package/docs/interfaces/LDTokenUsage.html +0 -129
- package/docs/interfaces/StructuredResponse.html +0 -129
- package/docs/types/LDAIConfigDefaultKind.html +0 -81
- package/docs/types/LDAIConfigKind.html +0 -81
- package/docs/types/LDAIConfigMode.html +0 -81
- package/docs/types/SupportedAIProvider.html +0 -81
- package/docs/variables/SUPPORTED_AI_PROVIDERS.html +0 -81
- package/jest.config.js +0 -7
- package/src/LDAIClientImpl.ts +0 -327
- package/src/LDAIConfigTrackerImpl.ts +0 -278
- package/src/LDClientMin.ts +0 -18
- package/src/api/LDAIClient.ts +0 -325
- package/src/api/chat/TrackedChat.ts +0 -159
- package/src/api/chat/index.ts +0 -2
- package/src/api/chat/types.ts +0 -24
- package/src/api/config/LDAIConfigTracker.ts +0 -231
- package/src/api/config/LDAIConfigUtils.ts +0 -201
- package/src/api/config/index.ts +0 -3
- package/src/api/config/types.ts +0 -256
- package/src/api/index.ts +0 -6
- package/src/api/judge/EvaluationSchemaBuilder.ts +0 -54
- package/src/api/judge/Judge.ts +0 -216
- package/src/api/judge/index.ts +0 -2
- package/src/api/judge/types.ts +0 -39
- package/src/api/metrics/BedrockTokenUsage.ts +0 -13
- package/src/api/metrics/LDAIMetrics.ts +0 -18
- package/src/api/metrics/LDFeedbackKind.ts +0 -13
- package/src/api/metrics/LDTokenUsage.ts +0 -19
- package/src/api/metrics/OpenAiUsage.ts +0 -13
- package/src/api/metrics/VercelAISDKTokenUsage.ts +0 -15
- package/src/api/metrics/index.ts +0 -6
- package/src/api/providers/AIProvider.ts +0 -94
- package/src/api/providers/AIProviderFactory.ts +0 -152
- package/src/api/providers/index.ts +0 -2
- package/src/index.ts +0 -24
- package/tsconfig.eslint.json +0 -5
- package/tsconfig.json +0 -21
- package/tsconfig.ref.json +0 -7
- package/typedoc.json +0 -5
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,1022 @@
|
|
|
1
|
+
import * as common from '@launchdarkly/js-server-sdk-common';
|
|
2
|
+
import { LDLogger as LDLogger$1, LDContext, LDFlagValue } from '@launchdarkly/js-server-sdk-common';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Information about token usage.
|
|
6
|
+
*/
|
|
7
|
+
interface LDTokenUsage {
|
|
8
|
+
/**
|
|
9
|
+
* Combined token usage.
|
|
10
|
+
*/
|
|
11
|
+
total: number;
|
|
12
|
+
/**
|
|
13
|
+
* Number of tokens in the input.
|
|
14
|
+
*/
|
|
15
|
+
input: number;
|
|
16
|
+
/**
|
|
17
|
+
* Number of tokens in the output.
|
|
18
|
+
*/
|
|
19
|
+
output: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Metrics information for AI operations that includes success status and token usage.
|
|
24
|
+
* This class combines success/failure tracking with token usage metrics.
|
|
25
|
+
*/
|
|
26
|
+
interface LDAIMetrics {
|
|
27
|
+
/**
|
|
28
|
+
* Whether the AI operation was successful.
|
|
29
|
+
*/
|
|
30
|
+
success: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Token usage information for the operation.
|
|
33
|
+
* This will be undefined if no token usage data is available.
|
|
34
|
+
*/
|
|
35
|
+
usage?: LDTokenUsage;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Structured response from AI models.
|
|
40
|
+
*/
|
|
41
|
+
interface StructuredResponse {
|
|
42
|
+
/** The structured data returned by the model */
|
|
43
|
+
data: Record<string, unknown>;
|
|
44
|
+
/** The raw response from the model */
|
|
45
|
+
rawResponse: string;
|
|
46
|
+
/**
|
|
47
|
+
* Metrics information including success status and token usage.
|
|
48
|
+
*/
|
|
49
|
+
metrics: LDAIMetrics;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Score and reasoning for a single evaluation metric.
|
|
53
|
+
*/
|
|
54
|
+
interface EvalScore {
|
|
55
|
+
/** Score between 0.0 and 1.0 indicating the evaluation result for this metric */
|
|
56
|
+
score: number;
|
|
57
|
+
/** Reasoning behind the provided score for this metric */
|
|
58
|
+
reasoning: string;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Response from a judge evaluation containing scores and reasoning for multiple metrics.
|
|
62
|
+
*/
|
|
63
|
+
interface JudgeResponse {
|
|
64
|
+
/** The key of the judge configuration that was used to generate this response */
|
|
65
|
+
judgeConfigKey?: string;
|
|
66
|
+
/** Dictionary where keys are metric names and values contain score and reasoning */
|
|
67
|
+
evals: Record<string, EvalScore>;
|
|
68
|
+
/** Whether the evaluation completed successfully */
|
|
69
|
+
success: boolean;
|
|
70
|
+
/** Error message if evaluation failed */
|
|
71
|
+
error?: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
declare function createBedrockTokenUsage(data: {
|
|
75
|
+
totalTokens?: number;
|
|
76
|
+
inputTokens?: number;
|
|
77
|
+
outputTokens?: number;
|
|
78
|
+
}): LDTokenUsage;
|
|
79
|
+
|
|
80
|
+
declare function createOpenAiUsage(data: {
|
|
81
|
+
total_tokens?: number;
|
|
82
|
+
prompt_tokens?: number;
|
|
83
|
+
completion_tokens?: number;
|
|
84
|
+
}): LDTokenUsage;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Feedback about the generated content.
|
|
88
|
+
*/
|
|
89
|
+
declare enum LDFeedbackKind {
|
|
90
|
+
/**
|
|
91
|
+
* The sentiment was positive.
|
|
92
|
+
*/
|
|
93
|
+
Positive = "positive",
|
|
94
|
+
/**
|
|
95
|
+
* The sentiment is negative.
|
|
96
|
+
*/
|
|
97
|
+
Negative = "negative"
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
declare function createVercelAISDKTokenUsage(data: {
|
|
101
|
+
totalTokens?: number;
|
|
102
|
+
inputTokens?: number;
|
|
103
|
+
promptTokens?: number;
|
|
104
|
+
outputTokens?: number;
|
|
105
|
+
completionTokens?: number;
|
|
106
|
+
}): LDTokenUsage;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Metrics which have been tracked.
|
|
110
|
+
*/
|
|
111
|
+
interface LDAIMetricSummary {
|
|
112
|
+
/**
|
|
113
|
+
* The duration of generation.
|
|
114
|
+
*/
|
|
115
|
+
durationMs?: number;
|
|
116
|
+
/**
|
|
117
|
+
* Information about token usage.
|
|
118
|
+
*/
|
|
119
|
+
tokens?: LDTokenUsage;
|
|
120
|
+
/**
|
|
121
|
+
* Was generation successful.
|
|
122
|
+
*/
|
|
123
|
+
success?: boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Any sentiment about the generation.
|
|
126
|
+
*/
|
|
127
|
+
feedback?: {
|
|
128
|
+
kind: LDFeedbackKind;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Time to first token for this generation.
|
|
132
|
+
*/
|
|
133
|
+
timeToFirstTokenMs?: number;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The LDAIConfigTracker is used to track various details about AI operations.
|
|
137
|
+
*/
|
|
138
|
+
interface LDAIConfigTracker {
|
|
139
|
+
/**
|
|
140
|
+
* Get the data for tracking.
|
|
141
|
+
*/
|
|
142
|
+
getTrackData(): {
|
|
143
|
+
variationKey: string;
|
|
144
|
+
configKey: string;
|
|
145
|
+
version: number;
|
|
146
|
+
modelName: string;
|
|
147
|
+
providerName: string;
|
|
148
|
+
aiSdkName: string;
|
|
149
|
+
aiSdkVersion: string;
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* Track the duration of generation.
|
|
153
|
+
*
|
|
154
|
+
* Ideally this would not include overhead time such as network communication.
|
|
155
|
+
*
|
|
156
|
+
* @param durationMs The duration in milliseconds.
|
|
157
|
+
*/
|
|
158
|
+
trackDuration(durationMs: number): void;
|
|
159
|
+
/**
|
|
160
|
+
* Track information about token usage.
|
|
161
|
+
*
|
|
162
|
+
* @param tokens Token usage information.
|
|
163
|
+
*/
|
|
164
|
+
trackTokens(tokens: LDTokenUsage): void;
|
|
165
|
+
/**
|
|
166
|
+
* Generation was successful.
|
|
167
|
+
*/
|
|
168
|
+
trackSuccess(): void;
|
|
169
|
+
/**
|
|
170
|
+
* An error was encountered during generation.
|
|
171
|
+
*/
|
|
172
|
+
trackError(): void;
|
|
173
|
+
/**
|
|
174
|
+
* Track sentiment about the generation.
|
|
175
|
+
*
|
|
176
|
+
* @param feedback Feedback about the generation.
|
|
177
|
+
*/
|
|
178
|
+
trackFeedback(feedback: {
|
|
179
|
+
kind: LDFeedbackKind;
|
|
180
|
+
}): void;
|
|
181
|
+
/**
|
|
182
|
+
* Track the time to first token for this generation.
|
|
183
|
+
*
|
|
184
|
+
* @param timeToFirstTokenMs The duration in milliseconds.
|
|
185
|
+
*/
|
|
186
|
+
trackTimeToFirstToken(timeToFirstTokenMs: number): void;
|
|
187
|
+
/**
|
|
188
|
+
* Track evaluation scores for multiple metrics.
|
|
189
|
+
*
|
|
190
|
+
* @param scores Record mapping metric keys to their evaluation scores
|
|
191
|
+
*/
|
|
192
|
+
trackEvalScores(scores: Record<string, EvalScore>): void;
|
|
193
|
+
/**
|
|
194
|
+
* Track a judge response containing evaluation scores and judge configuration key.
|
|
195
|
+
*
|
|
196
|
+
* @param response Judge response containing evaluation scores and judge configuration key
|
|
197
|
+
*/
|
|
198
|
+
trackJudgeResponse(response: JudgeResponse): void;
|
|
199
|
+
/**
|
|
200
|
+
* Track the duration of execution of the provided function.
|
|
201
|
+
*
|
|
202
|
+
* If the provided function throws, then this method will also throw.
|
|
203
|
+
* In the case the provided function throws, this function will still record the duration.
|
|
204
|
+
*
|
|
205
|
+
* This function does not automatically record an error when the function throws.
|
|
206
|
+
*
|
|
207
|
+
* @param func The function to track the duration of.
|
|
208
|
+
* @returns The result of the function.
|
|
209
|
+
*/
|
|
210
|
+
trackDurationOf(func: () => Promise<any>): Promise<any>;
|
|
211
|
+
/**
|
|
212
|
+
* Track metrics for a generic AI operation.
|
|
213
|
+
*
|
|
214
|
+
* This function will track the duration of the operation, extract metrics using the provided
|
|
215
|
+
* metrics extractor function, and track success or error status accordingly.
|
|
216
|
+
*
|
|
217
|
+
* If the provided function throws, then this method will also throw.
|
|
218
|
+
* In the case the provided function throws, this function will record the duration and an error.
|
|
219
|
+
* A failed operation will not have any token usage data.
|
|
220
|
+
*
|
|
221
|
+
* @param metricsExtractor Function that extracts LDAIMetrics from the operation result
|
|
222
|
+
* @param func Function which executes the operation
|
|
223
|
+
* @returns The result of the operation
|
|
224
|
+
*/
|
|
225
|
+
trackMetricsOf<TRes>(metricsExtractor: (result: TRes) => LDAIMetrics, func: () => Promise<TRes>): Promise<TRes>;
|
|
226
|
+
/**
|
|
227
|
+
* Track metrics for a streaming AI operation.
|
|
228
|
+
*
|
|
229
|
+
* This function will track the duration of the operation, extract metrics using the provided
|
|
230
|
+
* metrics extractor function, and track success or error status accordingly.
|
|
231
|
+
*
|
|
232
|
+
* Unlike trackMetricsOf, this method is designed for streaming operations where:
|
|
233
|
+
* - The stream is created and returned immediately (synchronously)
|
|
234
|
+
* - Metrics are extracted asynchronously in the background once the stream completes
|
|
235
|
+
* - Duration is tracked from stream creation to metrics extraction completion
|
|
236
|
+
*
|
|
237
|
+
* The stream is returned immediately so the caller can begin consuming it without waiting.
|
|
238
|
+
* Metrics extraction happens in the background and does not block stream consumption.
|
|
239
|
+
*
|
|
240
|
+
* If the stream creator throws, then this method will also throw and record an error.
|
|
241
|
+
* If metrics extraction fails, the error is logged but does not affect stream consumption.
|
|
242
|
+
*
|
|
243
|
+
* @param streamCreator Function that creates and returns the stream (synchronous)
|
|
244
|
+
* @param metricsExtractor Function that asynchronously extracts metrics from the stream
|
|
245
|
+
* @returns The stream result (returned immediately, not a Promise)
|
|
246
|
+
*/
|
|
247
|
+
trackStreamMetricsOf<TStream>(streamCreator: () => TStream, metricsExtractor: (stream: TStream) => Promise<LDAIMetrics>): TStream;
|
|
248
|
+
/**
|
|
249
|
+
* Track an OpenAI operation.
|
|
250
|
+
*
|
|
251
|
+
* This function will track the duration of the operation, the token usage, and the success or error status.
|
|
252
|
+
*
|
|
253
|
+
* If the provided function throws, then this method will also throw.
|
|
254
|
+
* In the case the provided function throws, this function will record the duration and an error.
|
|
255
|
+
* A failed operation will not have any token usage data.
|
|
256
|
+
*
|
|
257
|
+
* @param func Function which executes the operation.
|
|
258
|
+
* @returns The result of the operation.
|
|
259
|
+
*/
|
|
260
|
+
trackOpenAIMetrics<TRes extends {
|
|
261
|
+
usage?: {
|
|
262
|
+
total_tokens?: number;
|
|
263
|
+
prompt_tokens?: number;
|
|
264
|
+
completion_tokens?: number;
|
|
265
|
+
};
|
|
266
|
+
}>(func: () => Promise<TRes>): Promise<TRes>;
|
|
267
|
+
/**
|
|
268
|
+
* Track an operation which uses Bedrock.
|
|
269
|
+
*
|
|
270
|
+
* This function will track the duration of the operation, the token usage, and the success or error status.
|
|
271
|
+
*
|
|
272
|
+
* @param res The result of the Bedrock operation.
|
|
273
|
+
* @returns The input operation.
|
|
274
|
+
*/
|
|
275
|
+
trackBedrockConverseMetrics<TRes extends {
|
|
276
|
+
$metadata: {
|
|
277
|
+
httpStatusCode?: number;
|
|
278
|
+
};
|
|
279
|
+
metrics?: {
|
|
280
|
+
latencyMs?: number;
|
|
281
|
+
};
|
|
282
|
+
usage?: {
|
|
283
|
+
inputTokens?: number;
|
|
284
|
+
outputTokens?: number;
|
|
285
|
+
totalTokens?: number;
|
|
286
|
+
};
|
|
287
|
+
}>(res: TRes): TRes;
|
|
288
|
+
/**
|
|
289
|
+
* Track a Vercel AI SDK generateText operation.
|
|
290
|
+
*
|
|
291
|
+
* This function will track the duration of the operation, the token usage, and the success or error status.
|
|
292
|
+
*
|
|
293
|
+
* If the provided function throws, then this method will also throw.
|
|
294
|
+
* In the case the provided function throws, this function will record the duration and an error.
|
|
295
|
+
* A failed operation will not have any token usage data.
|
|
296
|
+
*
|
|
297
|
+
* @param func Function which executes the operation.
|
|
298
|
+
* @returns The result of the operation.
|
|
299
|
+
*/
|
|
300
|
+
trackVercelAISDKGenerateTextMetrics<TRes extends {
|
|
301
|
+
usage?: {
|
|
302
|
+
totalTokens?: number;
|
|
303
|
+
inputTokens?: number;
|
|
304
|
+
promptTokens?: number;
|
|
305
|
+
outputTokens?: number;
|
|
306
|
+
completionTokens?: number;
|
|
307
|
+
};
|
|
308
|
+
}>(func: () => Promise<TRes>): Promise<TRes>;
|
|
309
|
+
/**
|
|
310
|
+
* Get a summary of the tracked metrics.
|
|
311
|
+
*/
|
|
312
|
+
getSummary(): LDAIMetricSummary;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Information about prompts.
|
|
317
|
+
*/
|
|
318
|
+
interface LDMessage {
|
|
319
|
+
/**
|
|
320
|
+
* The role of the prompt.
|
|
321
|
+
*/
|
|
322
|
+
role: 'user' | 'assistant' | 'system';
|
|
323
|
+
/**
|
|
324
|
+
* Content for the prompt.
|
|
325
|
+
*/
|
|
326
|
+
content: string;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Configuration related to the model.
|
|
330
|
+
*/
|
|
331
|
+
interface LDModelConfig {
|
|
332
|
+
/**
|
|
333
|
+
* The ID of the model.
|
|
334
|
+
*/
|
|
335
|
+
name: string;
|
|
336
|
+
/**
|
|
337
|
+
* Model specific parameters.
|
|
338
|
+
*/
|
|
339
|
+
parameters?: {
|
|
340
|
+
[index: string]: unknown;
|
|
341
|
+
};
|
|
342
|
+
/**
|
|
343
|
+
* Additional user-specified parameters.
|
|
344
|
+
*/
|
|
345
|
+
custom?: {
|
|
346
|
+
[index: string]: unknown;
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
interface LDProviderConfig {
|
|
350
|
+
/**
|
|
351
|
+
* The name of the provider.
|
|
352
|
+
*/
|
|
353
|
+
name: string;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Configuration for a single judge attachment.
|
|
357
|
+
*/
|
|
358
|
+
interface LDJudge {
|
|
359
|
+
/** The key of the judge AI Config */
|
|
360
|
+
key: string;
|
|
361
|
+
/** Sampling rate for evaluation (0.0 to 1.0) */
|
|
362
|
+
samplingRate: number;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Configuration for judge attachment to AI Configs.
|
|
366
|
+
*/
|
|
367
|
+
interface LDJudgeConfiguration {
|
|
368
|
+
/** Array of judge configurations */
|
|
369
|
+
judges: LDJudge[];
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Base AI Config interface for default implementations with optional enabled property.
|
|
373
|
+
*/
|
|
374
|
+
interface LDAIConfigDefault {
|
|
375
|
+
/**
|
|
376
|
+
* Optional model configuration.
|
|
377
|
+
*/
|
|
378
|
+
model?: LDModelConfig;
|
|
379
|
+
/**
|
|
380
|
+
* Optional configuration for the provider.
|
|
381
|
+
*/
|
|
382
|
+
provider?: LDProviderConfig;
|
|
383
|
+
/**
|
|
384
|
+
* Whether the configuration is enabled. Defaults to false when not provided.
|
|
385
|
+
*/
|
|
386
|
+
enabled?: boolean;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Base AI Config interface without mode-specific fields.
|
|
390
|
+
*/
|
|
391
|
+
interface LDAIConfig extends Omit<LDAIConfigDefault, 'enabled'> {
|
|
392
|
+
/**
|
|
393
|
+
* The key of the AI Config.
|
|
394
|
+
*/
|
|
395
|
+
key: string;
|
|
396
|
+
/**
|
|
397
|
+
* Whether the configuration is enabled.
|
|
398
|
+
*/
|
|
399
|
+
enabled: boolean;
|
|
400
|
+
/**
|
|
401
|
+
* A tracker which can be used to generate analytics.
|
|
402
|
+
* Undefined for disabled configs.
|
|
403
|
+
*/
|
|
404
|
+
tracker?: LDAIConfigTracker;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Default Agent-specific AI Config with instructions.
|
|
408
|
+
*/
|
|
409
|
+
interface LDAIAgentConfigDefault extends LDAIConfigDefault {
|
|
410
|
+
/**
|
|
411
|
+
* Instructions for the agent.
|
|
412
|
+
*/
|
|
413
|
+
instructions?: string;
|
|
414
|
+
/**
|
|
415
|
+
* Judge configuration for AI Configs being evaluated.
|
|
416
|
+
* References judge AI Configs that should evaluate this AI Config.
|
|
417
|
+
*/
|
|
418
|
+
judgeConfiguration?: LDJudgeConfiguration;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Default Completion AI Config (default mode).
|
|
422
|
+
*/
|
|
423
|
+
interface LDAICompletionConfigDefault extends LDAIConfigDefault {
|
|
424
|
+
/**
|
|
425
|
+
* Optional prompt data for completion configurations.
|
|
426
|
+
*/
|
|
427
|
+
messages?: LDMessage[];
|
|
428
|
+
/**
|
|
429
|
+
* Judge configuration for AI Configs being evaluated.
|
|
430
|
+
* References judge AI Configs that should evaluate this AI Config.
|
|
431
|
+
*/
|
|
432
|
+
judgeConfiguration?: LDJudgeConfiguration;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Default Judge-specific AI Config with required evaluation metric key.
|
|
436
|
+
*/
|
|
437
|
+
interface LDAIJudgeConfigDefault extends LDAIConfigDefault {
|
|
438
|
+
/**
|
|
439
|
+
* Optional prompt data for judge configurations.
|
|
440
|
+
*/
|
|
441
|
+
messages?: LDMessage[];
|
|
442
|
+
/**
|
|
443
|
+
* Evaluation metric keys for judge configurations.
|
|
444
|
+
* The keys of the metrics that this judge can evaluate.
|
|
445
|
+
*/
|
|
446
|
+
evaluationMetricKeys?: string[];
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Union type for all default AI Config variants.
|
|
450
|
+
*/
|
|
451
|
+
type LDAIConfigDefaultKind = LDAIAgentConfigDefault | LDAICompletionConfigDefault | LDAIJudgeConfigDefault;
|
|
452
|
+
/**
|
|
453
|
+
* Agent-specific AI Config with instructions.
|
|
454
|
+
*/
|
|
455
|
+
interface LDAIAgentConfig extends LDAIConfig {
|
|
456
|
+
/**
|
|
457
|
+
* Instructions for the agent.
|
|
458
|
+
*/
|
|
459
|
+
instructions?: string;
|
|
460
|
+
/**
|
|
461
|
+
* Judge configuration for AI Configs being evaluated.
|
|
462
|
+
* References judge AI Configs that should evaluate this AI Config.
|
|
463
|
+
*/
|
|
464
|
+
judgeConfiguration?: LDJudgeConfiguration;
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Completion AI Config (default mode).
|
|
468
|
+
*/
|
|
469
|
+
interface LDAICompletionConfig extends LDAIConfig {
|
|
470
|
+
/**
|
|
471
|
+
* Optional prompt data for completion configurations.
|
|
472
|
+
*/
|
|
473
|
+
messages?: LDMessage[];
|
|
474
|
+
/**
|
|
475
|
+
* Judge configuration for AI Configs being evaluated.
|
|
476
|
+
* References judge AI Configs that should evaluate this AI Config.
|
|
477
|
+
*/
|
|
478
|
+
judgeConfiguration?: LDJudgeConfiguration;
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Judge-specific AI Config with required evaluation metric key.
|
|
482
|
+
*/
|
|
483
|
+
interface LDAIJudgeConfig extends LDAIConfig {
|
|
484
|
+
/**
|
|
485
|
+
* Optional prompt data for judge configurations.
|
|
486
|
+
*/
|
|
487
|
+
messages?: LDMessage[];
|
|
488
|
+
/**
|
|
489
|
+
* Evaluation metric keys for judge configurations.
|
|
490
|
+
* The keys of the metrics that this judge can evaluate.
|
|
491
|
+
*/
|
|
492
|
+
evaluationMetricKeys: string[];
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Union type for all AI Config variants.
|
|
496
|
+
*/
|
|
497
|
+
type LDAIConfigKind = LDAIAgentConfig | LDAICompletionConfig | LDAIJudgeConfig;
|
|
498
|
+
/**
|
|
499
|
+
* Configuration for a single agent request.
|
|
500
|
+
*/
|
|
501
|
+
interface LDAIAgentRequestConfig {
|
|
502
|
+
/**
|
|
503
|
+
* The agent key to retrieve.
|
|
504
|
+
*/
|
|
505
|
+
key: string;
|
|
506
|
+
/**
|
|
507
|
+
* Default configuration for the agent.
|
|
508
|
+
*/
|
|
509
|
+
defaultValue: LDAIAgentConfigDefault;
|
|
510
|
+
/**
|
|
511
|
+
* Variables for instructions interpolation.
|
|
512
|
+
*/
|
|
513
|
+
variables?: Record<string, unknown>;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Mode type for AI configurations.
|
|
517
|
+
*/
|
|
518
|
+
type LDAIConfigMode = 'completion' | 'agent' | 'judge';
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Chat response structure.
|
|
522
|
+
*/
|
|
523
|
+
interface ChatResponse {
|
|
524
|
+
/**
|
|
525
|
+
* The response message from the AI.
|
|
526
|
+
*/
|
|
527
|
+
message: LDMessage;
|
|
528
|
+
/**
|
|
529
|
+
* Metrics information including success status and token usage.
|
|
530
|
+
*/
|
|
531
|
+
metrics: LDAIMetrics;
|
|
532
|
+
/**
|
|
533
|
+
* Promise that resolves to judge evaluation results.
|
|
534
|
+
* Only present when judges are configured for evaluation.
|
|
535
|
+
*/
|
|
536
|
+
evaluations?: Promise<Array<JudgeResponse | undefined>>;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Abstract base class for AI providers that implement chat model functionality.
|
|
541
|
+
* This class provides the contract that all provider implementations must follow
|
|
542
|
+
* to integrate with LaunchDarkly's tracking and configuration capabilities.
|
|
543
|
+
*
|
|
544
|
+
* Following the AICHAT spec recommendation to use base classes with non-abstract methods
|
|
545
|
+
* for better extensibility and backwards compatibility.
|
|
546
|
+
*/
|
|
547
|
+
declare abstract class AIProvider {
|
|
548
|
+
protected readonly logger?: LDLogger$1;
|
|
549
|
+
constructor(logger?: LDLogger$1);
|
|
550
|
+
/**
|
|
551
|
+
* Invoke the chat model with an array of messages.
|
|
552
|
+
* This method should convert messages to provider format, invoke the model,
|
|
553
|
+
* and return a ChatResponse with the result and metrics.
|
|
554
|
+
*
|
|
555
|
+
* Default implementation takes no action and returns a placeholder response.
|
|
556
|
+
* Provider implementations should override this method.
|
|
557
|
+
*
|
|
558
|
+
* @param messages Array of LDMessage objects representing the conversation
|
|
559
|
+
* @returns Promise that resolves to a ChatResponse containing the model's response
|
|
560
|
+
*/
|
|
561
|
+
invokeModel(_messages: LDMessage[]): Promise<ChatResponse>;
|
|
562
|
+
/**
|
|
563
|
+
* Invoke the chat model with structured output support.
|
|
564
|
+
* This method should convert messages to provider format, invoke the model with
|
|
565
|
+
* structured output configuration, and return a structured response.
|
|
566
|
+
*
|
|
567
|
+
* Default implementation takes no action and returns a placeholder response.
|
|
568
|
+
* Provider implementations should override this method.
|
|
569
|
+
*
|
|
570
|
+
* @param messages Array of LDMessage objects representing the conversation
|
|
571
|
+
* @param responseStructure Dictionary of output configurations keyed by output name
|
|
572
|
+
* @returns Promise that resolves to a structured response
|
|
573
|
+
*/
|
|
574
|
+
invokeStructuredModel(_messages: LDMessage[], _responseStructure: Record<string, unknown>): Promise<StructuredResponse>;
|
|
575
|
+
/**
|
|
576
|
+
* Static method that constructs an instance of the provider.
|
|
577
|
+
* Each provider implementation must provide their own static create method
|
|
578
|
+
* that accepts an AIConfig and returns a configured instance.
|
|
579
|
+
*
|
|
580
|
+
* @param aiConfig The LaunchDarkly AI configuration
|
|
581
|
+
* @param logger Optional logger for the provider
|
|
582
|
+
* @returns Promise that resolves to a configured provider instance
|
|
583
|
+
*/
|
|
584
|
+
static create(aiConfig: LDAIConfigKind, logger?: LDLogger$1): Promise<AIProvider>;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Judge implementation that handles evaluation functionality and conversation management.
|
|
589
|
+
*
|
|
590
|
+
* According to the AIEval spec, judges are AI Configs with mode: "judge" that evaluate
|
|
591
|
+
* other AI Configs using structured output.
|
|
592
|
+
*/
|
|
593
|
+
declare class Judge {
|
|
594
|
+
private readonly _aiConfig;
|
|
595
|
+
private readonly _aiConfigTracker;
|
|
596
|
+
private readonly _aiProvider;
|
|
597
|
+
private readonly _logger?;
|
|
598
|
+
private readonly _evaluationResponseStructure;
|
|
599
|
+
constructor(_aiConfig: LDAIJudgeConfig, _aiConfigTracker: LDAIConfigTracker, _aiProvider: AIProvider, logger?: LDLogger$1);
|
|
600
|
+
/**
|
|
601
|
+
* Evaluates an AI response using the judge's configuration.
|
|
602
|
+
*
|
|
603
|
+
* @param input The input prompt or question that was provided to the AI
|
|
604
|
+
* @param output The AI-generated response to be evaluated
|
|
605
|
+
* @param samplingRate Sampling rate (0-1) to determine if evaluation should be processed (defaults to 1)
|
|
606
|
+
* @returns Promise that resolves to evaluation results or undefined if not sampled
|
|
607
|
+
*/
|
|
608
|
+
evaluate(input: string, output: string, samplingRate?: number): Promise<JudgeResponse | undefined>;
|
|
609
|
+
/**
|
|
610
|
+
* Evaluates an AI response from chat messages and response.
|
|
611
|
+
*
|
|
612
|
+
* @param messages Array of messages representing the conversation history
|
|
613
|
+
* @param response The AI response to be evaluated
|
|
614
|
+
* @param samplingRatio Sampling ratio (0-1) to determine if evaluation should be processed (defaults to 1)
|
|
615
|
+
* @returns Promise that resolves to evaluation results or undefined if not sampled
|
|
616
|
+
*/
|
|
617
|
+
evaluateMessages(messages: LDMessage[], response: ChatResponse, samplingRatio?: number): Promise<JudgeResponse | undefined>;
|
|
618
|
+
/**
|
|
619
|
+
* Returns the AI Config used by this judge.
|
|
620
|
+
*/
|
|
621
|
+
getAIConfig(): LDAIJudgeConfig;
|
|
622
|
+
/**
|
|
623
|
+
* Returns the tracker associated with this judge.
|
|
624
|
+
*/
|
|
625
|
+
getTracker(): LDAIConfigTracker;
|
|
626
|
+
/**
|
|
627
|
+
* Returns the AI provider used by this judge.
|
|
628
|
+
*/
|
|
629
|
+
getProvider(): AIProvider;
|
|
630
|
+
/**
|
|
631
|
+
* Constructs evaluation messages by combining judge's config messages with input/output.
|
|
632
|
+
*/
|
|
633
|
+
private _constructEvaluationMessages;
|
|
634
|
+
/**
|
|
635
|
+
* Interpolates message content with variables using Mustache templating.
|
|
636
|
+
*/
|
|
637
|
+
private _interpolateMessage;
|
|
638
|
+
/**
|
|
639
|
+
* Parses the structured evaluation response from the AI provider.
|
|
640
|
+
*/
|
|
641
|
+
private _parseEvaluationResponse;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Concrete implementation of TrackedChat that provides chat functionality
|
|
646
|
+
* by delegating to an AIProvider implementation.
|
|
647
|
+
* This class handles conversation management and tracking, while delegating
|
|
648
|
+
* the actual model invocation to the provider.
|
|
649
|
+
*/
|
|
650
|
+
declare class TrackedChat {
|
|
651
|
+
protected readonly aiConfig: LDAICompletionConfig;
|
|
652
|
+
protected readonly tracker: LDAIConfigTracker;
|
|
653
|
+
protected readonly provider: AIProvider;
|
|
654
|
+
protected readonly judges: Record<string, Judge>;
|
|
655
|
+
private readonly _logger?;
|
|
656
|
+
protected messages: LDMessage[];
|
|
657
|
+
constructor(aiConfig: LDAICompletionConfig, tracker: LDAIConfigTracker, provider: AIProvider, judges?: Record<string, Judge>, _logger?: LDLogger$1 | undefined);
|
|
658
|
+
/**
|
|
659
|
+
* Invoke the chat model with a prompt string.
|
|
660
|
+
* This method handles conversation management and tracking, delegating to the provider's invokeModel method.
|
|
661
|
+
*/
|
|
662
|
+
invoke(prompt: string): Promise<ChatResponse>;
|
|
663
|
+
/**
|
|
664
|
+
* Evaluates the response with all configured judges.
|
|
665
|
+
* Returns a promise that resolves to an array of evaluation results.
|
|
666
|
+
*
|
|
667
|
+
* @param messages Array of messages representing the conversation history
|
|
668
|
+
* @param response The AI response to be evaluated
|
|
669
|
+
* @returns Promise resolving to array of judge evaluation results
|
|
670
|
+
*/
|
|
671
|
+
private _evaluateWithJudges;
|
|
672
|
+
/**
|
|
673
|
+
* Get the underlying AI configuration used to initialize this TrackedChat.
|
|
674
|
+
*/
|
|
675
|
+
getConfig(): LDAICompletionConfig;
|
|
676
|
+
/**
|
|
677
|
+
* Get the underlying AI configuration tracker used to initialize this TrackedChat.
|
|
678
|
+
*/
|
|
679
|
+
getTracker(): LDAIConfigTracker;
|
|
680
|
+
/**
|
|
681
|
+
* Get the underlying AI provider instance.
|
|
682
|
+
* This provides direct access to the provider for advanced use cases.
|
|
683
|
+
*/
|
|
684
|
+
getProvider(): AIProvider;
|
|
685
|
+
/**
|
|
686
|
+
* Get the judges associated with this TrackedChat.
|
|
687
|
+
* Returns a record of judge instances keyed by their configuration keys.
|
|
688
|
+
*/
|
|
689
|
+
getJudges(): Record<string, Judge>;
|
|
690
|
+
/**
|
|
691
|
+
* Append messages to the conversation history.
|
|
692
|
+
* Adds messages to the conversation history without invoking the model,
|
|
693
|
+
* which is useful for managing multi-turn conversations or injecting context.
|
|
694
|
+
*
|
|
695
|
+
* @param messages Array of messages to append to the conversation history
|
|
696
|
+
*/
|
|
697
|
+
appendMessages(messages: LDMessage[]): void;
|
|
698
|
+
/**
|
|
699
|
+
* Get all messages in the conversation history.
|
|
700
|
+
*
|
|
701
|
+
* @param includeConfigMessages Whether to include the config messages from the AIConfig.
|
|
702
|
+
* Defaults to false.
|
|
703
|
+
* @returns Array of messages. When includeConfigMessages is true, returns both config
|
|
704
|
+
* messages and conversation history with config messages prepended. When false,
|
|
705
|
+
* returns only the conversation history messages.
|
|
706
|
+
*/
|
|
707
|
+
getMessages(includeConfigMessages?: boolean): LDMessage[];
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* List of supported AI providers.
|
|
712
|
+
*/
|
|
713
|
+
declare const SUPPORTED_AI_PROVIDERS: readonly ["openai", "langchain", "vercel"];
|
|
714
|
+
/**
|
|
715
|
+
* Type representing the supported AI providers.
|
|
716
|
+
*/
|
|
717
|
+
type SupportedAIProvider = (typeof SUPPORTED_AI_PROVIDERS)[number];
|
|
718
|
+
/**
|
|
719
|
+
* Factory for creating AIProvider instances based on the provider configuration.
|
|
720
|
+
*/
|
|
721
|
+
declare class AIProviderFactory {
|
|
722
|
+
/**
|
|
723
|
+
* Create an AIProvider instance based on the AI configuration.
|
|
724
|
+
* This method attempts to load provider-specific implementations dynamically.
|
|
725
|
+
* Returns undefined if the provider is not supported.
|
|
726
|
+
*
|
|
727
|
+
* @param aiConfig The AI configuration
|
|
728
|
+
* @param logger Optional logger for logging provider initialization
|
|
729
|
+
* @param defaultAiProvider Optional default AI provider to use
|
|
730
|
+
*/
|
|
731
|
+
static create(aiConfig: LDAIConfigKind, logger?: LDLogger$1, defaultAiProvider?: SupportedAIProvider): Promise<AIProvider | undefined>;
|
|
732
|
+
/**
|
|
733
|
+
* Determine which providers to try based on defaultAiProvider and providerName.
|
|
734
|
+
*/
|
|
735
|
+
private static _getProvidersToTry;
|
|
736
|
+
/**
|
|
737
|
+
* Try to create a provider of the specified type.
|
|
738
|
+
*/
|
|
739
|
+
private static _tryCreateProvider;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/**
|
|
743
|
+
* Interface for performing AI operations using LaunchDarkly.
|
|
744
|
+
*/
|
|
745
|
+
interface LDAIClient {
|
|
746
|
+
/**
|
|
747
|
+
* Retrieves and processes a completion AI Config based on the provided key, LaunchDarkly context,
|
|
748
|
+
* and variables. This includes the model configuration and the customized messages.
|
|
749
|
+
*
|
|
750
|
+
* @param key The key of the AI Config.
|
|
751
|
+
* @param context The LaunchDarkly context object that contains relevant information about the
|
|
752
|
+
* current environment, user, or session. This context may influence how the configuration is
|
|
753
|
+
* processed or personalized.
|
|
754
|
+
* @param defaultValue A fallback value containing model configuration and messages. This will
|
|
755
|
+
* be used if the configuration is not available from LaunchDarkly.
|
|
756
|
+
* @param variables A map of key-value pairs representing dynamic variables to be injected into
|
|
757
|
+
* the message content. The keys correspond to placeholders within the template, and the values
|
|
758
|
+
* are the corresponding replacements.
|
|
759
|
+
*
|
|
760
|
+
* @returns The AI `config`, customized `messages`, and a `tracker`. If the configuration cannot be accessed from
|
|
761
|
+
* LaunchDarkly, then the return value will include information from the `defaultValue`. The returned `tracker` can
|
|
762
|
+
* be used to track AI operation metrics (latency, token usage, etc.).
|
|
763
|
+
*
|
|
764
|
+
* @example
|
|
765
|
+
* ```
|
|
766
|
+
* const key = "welcome_prompt";
|
|
767
|
+
* const context = {...};
|
|
768
|
+
* const variables = {username: 'john'};
|
|
769
|
+
* const defaultValue = {
|
|
770
|
+
* enabled: false,
|
|
771
|
+
* model: { name: 'gpt-4' },
|
|
772
|
+
* provider: { name: 'openai' },
|
|
773
|
+
* };
|
|
774
|
+
*
|
|
775
|
+
* const result = completionConfig(key, context, defaultValue, variables);
|
|
776
|
+
* // Output:
|
|
777
|
+
* {
|
|
778
|
+
* enabled: true,
|
|
779
|
+
* config: {
|
|
780
|
+
* modelId: "gpt-4o",
|
|
781
|
+
* temperature: 0.2,
|
|
782
|
+
* maxTokens: 4096,
|
|
783
|
+
* userDefinedKey: "myValue",
|
|
784
|
+
* },
|
|
785
|
+
* messages: [
|
|
786
|
+
* {
|
|
787
|
+
* role: "system",
|
|
788
|
+
* content: "You are an amazing GPT."
|
|
789
|
+
* },
|
|
790
|
+
* {
|
|
791
|
+
* role: "user",
|
|
792
|
+
* content: "Explain how you're an amazing GPT."
|
|
793
|
+
* }
|
|
794
|
+
* ],
|
|
795
|
+
* tracker: ...
|
|
796
|
+
* }
|
|
797
|
+
* ```
|
|
798
|
+
*/
|
|
799
|
+
completionConfig(key: string, context: LDContext, defaultValue: LDAICompletionConfigDefault, variables?: Record<string, unknown>): Promise<LDAICompletionConfig>;
|
|
800
|
+
/**
|
|
801
|
+
* @deprecated Use `completionConfig` instead. This method will be removed in a future version.
|
|
802
|
+
*/
|
|
803
|
+
config(key: string, context: LDContext, defaultValue: LDAICompletionConfigDefault, variables?: Record<string, unknown>): Promise<LDAICompletionConfig>;
|
|
804
|
+
/**
|
|
805
|
+
* Retrieves and processes a single AI Config agent based on the provided key, LaunchDarkly context,
|
|
806
|
+
* and variables. This includes the model configuration and the customized instructions.
|
|
807
|
+
*
|
|
808
|
+
* @param key The key of the AI Config agent.
|
|
809
|
+
* @param context The LaunchDarkly context object that contains relevant information about the
|
|
810
|
+
* current environment, user, or session. This context may influence how the configuration is
|
|
811
|
+
* processed or personalized.
|
|
812
|
+
* @param defaultValue A fallback value containing model configuration and instructions.
|
|
813
|
+
* @param variables A map of key-value pairs representing dynamic variables to be injected into
|
|
814
|
+
* the instructions. The keys correspond to placeholders within the template, and the values
|
|
815
|
+
* are the corresponding replacements.
|
|
816
|
+
*
|
|
817
|
+
* @returns An AI agent with customized `instructions` and a `tracker`. If the configuration
|
|
818
|
+
* cannot be accessed from LaunchDarkly, then the return value will include information from the
|
|
819
|
+
* `defaultValue`. The returned `tracker` can be used to track AI operation metrics (latency, token usage, etc.).
|
|
820
|
+
*
|
|
821
|
+
* @example
|
|
822
|
+
* ```
|
|
823
|
+
* const key = "research_agent";
|
|
824
|
+
* const context = {...};
|
|
825
|
+
* const variables = { topic: 'climate change' };
|
|
826
|
+
* const agentConfig = await client.agentConfig(key, context, {
|
|
827
|
+
* enabled: true,
|
|
828
|
+
* model: { name: 'gpt-4' },
|
|
829
|
+
* provider: { name: 'openai' },
|
|
830
|
+
* instructions: 'You are a research assistant.',
|
|
831
|
+
* }, variables);
|
|
832
|
+
*
|
|
833
|
+
* const researchResult = agentConfig.instructions; // Interpolated instructions
|
|
834
|
+
* agentConfig.tracker.trackSuccess();
|
|
835
|
+
* ```
|
|
836
|
+
*/
|
|
837
|
+
agentConfig(key: string, context: LDContext, defaultValue: LDAIAgentConfigDefault, variables?: Record<string, unknown>): Promise<LDAIAgentConfig>;
|
|
838
|
+
/**
|
|
839
|
+
* @deprecated Use `agentConfig` instead. This method will be removed in a future version.
|
|
840
|
+
*/
|
|
841
|
+
agent(key: string, context: LDContext, defaultValue: LDAIAgentConfigDefault, variables?: Record<string, unknown>): Promise<LDAIAgentConfig>;
|
|
842
|
+
/**
|
|
843
|
+
* Retrieves and processes a Judge AI Config based on the provided key, LaunchDarkly context,
|
|
844
|
+
* and variables. This includes the model configuration and the customized messages for evaluation.
|
|
845
|
+
*
|
|
846
|
+
* @param key The key of the Judge AI Config.
|
|
847
|
+
* @param context The LaunchDarkly context object that contains relevant information about the
|
|
848
|
+
* current environment, user, or session. This context may influence how the configuration is
|
|
849
|
+
* processed or personalized.
|
|
850
|
+
* @param defaultValue A fallback value containing model configuration and messages. This will
|
|
851
|
+
* be used if the configuration is not available from LaunchDarkly.
|
|
852
|
+
* @param variables Optional variables for template interpolation in messages and instructions.
|
|
853
|
+
* @returns A promise that resolves to a tracked judge configuration.
|
|
854
|
+
*
|
|
855
|
+
* @example
|
|
856
|
+
* ```typescript
|
|
857
|
+
* const judgeConf = await client.judgeConfig(key, context, {
|
|
858
|
+
* enabled: true,
|
|
859
|
+
* model: { name: 'gpt-4' },
|
|
860
|
+
* provider: { name: 'openai' },
|
|
861
|
+
* evaluationMetricKeys: ['$ld:ai:judge:relevance'],
|
|
862
|
+
* messages: [{ role: 'system', content: 'You are a relevance judge.' }]
|
|
863
|
+
* }, variables);
|
|
864
|
+
*
|
|
865
|
+
* const config = judgeConf.config; // Interpolated configuration
|
|
866
|
+
* judgeConf.tracker.trackSuccess();
|
|
867
|
+
* ```
|
|
868
|
+
*/
|
|
869
|
+
judgeConfig(key: string, context: LDContext, defaultValue: LDAIJudgeConfigDefault, variables?: Record<string, unknown>): Promise<LDAIJudgeConfig>;
|
|
870
|
+
/**
|
|
871
|
+
* Retrieves and processes multiple AI Config agents based on the provided agent configurations
|
|
872
|
+
* and LaunchDarkly context. This includes the model configuration and the customized instructions.
|
|
873
|
+
*
|
|
874
|
+
* @param agentConfigs An array of agent configurations, each containing the agent key, default configuration,
|
|
875
|
+
* and variables for instructions interpolation.
|
|
876
|
+
* @param context The LaunchDarkly context object that contains relevant information about the
|
|
877
|
+
* current environment, user, or session. This context may influence how the configuration is
|
|
878
|
+
* processed or personalized.
|
|
879
|
+
*
|
|
880
|
+
* @returns A map of agent keys to their respective AI agents with customized `instructions` and `tracker`.
|
|
881
|
+
* If a configuration cannot be accessed from LaunchDarkly, then the return value will include information
|
|
882
|
+
* from the respective `defaultValue`. The returned `tracker` can be used to track AI operation metrics
|
|
883
|
+
* (latency, token usage, etc.).
|
|
884
|
+
*
|
|
885
|
+
* @example
|
|
886
|
+
* ```
|
|
887
|
+
* const agentConfigsList = [
|
|
888
|
+
* {
|
|
889
|
+
* key: 'research_agent',
|
|
890
|
+
* defaultValue: {
|
|
891
|
+
* enabled: true,
|
|
892
|
+
* model: { name: 'gpt-4' },
|
|
893
|
+
* provider: { name: 'openai' },
|
|
894
|
+
* instructions: 'You are a research assistant.'
|
|
895
|
+
* },
|
|
896
|
+
* variables: { topic: 'climate change' }
|
|
897
|
+
* },
|
|
898
|
+
* {
|
|
899
|
+
* key: 'writing_agent',
|
|
900
|
+
* defaultValue: {
|
|
901
|
+
* enabled: true,
|
|
902
|
+
* model: { name: 'gpt-4' },
|
|
903
|
+
* provider: { name: 'openai' },
|
|
904
|
+
* instructions: 'You are a writing assistant.'
|
|
905
|
+
* },
|
|
906
|
+
* variables: { style: 'academic' }
|
|
907
|
+
* }
|
|
908
|
+
* ] as const;
|
|
909
|
+
* const context = {...};
|
|
910
|
+
*
|
|
911
|
+
* const configs = await client.agentConfigs(agentConfigsList, context);
|
|
912
|
+
* const researchResult = configs["research_agent"].instructions; // Interpolated instructions
|
|
913
|
+
* configs["research_agent"].tracker.trackSuccess();
|
|
914
|
+
* ```
|
|
915
|
+
*/
|
|
916
|
+
agentConfigs<const T extends readonly LDAIAgentRequestConfig[]>(agentConfigs: T, context: LDContext): Promise<Record<T[number]['key'], LDAIAgentConfig>>;
|
|
917
|
+
/**
|
|
918
|
+
* @deprecated Use `agentConfigs` instead. This method will be removed in a future version.
|
|
919
|
+
*/
|
|
920
|
+
agents<const T extends readonly LDAIAgentRequestConfig[]>(agentConfigs: T, context: LDContext): Promise<Record<T[number]['key'], LDAIAgentConfig>>;
|
|
921
|
+
/**
|
|
922
|
+
* Returns a TrackedChat instance for chat interactions.
|
|
923
|
+
* This method serves as the primary entry point for creating TrackedChat instances from configuration.
|
|
924
|
+
*
|
|
925
|
+
* @param key The key identifying the AI chat configuration to use.
|
|
926
|
+
* @param context The standard LDContext used when evaluating flags.
|
|
927
|
+
* @param defaultValue A default value representing a standard AI chat config result.
|
|
928
|
+
* @param variables Dictionary of values for instruction interpolation.
|
|
929
|
+
* The variables will also be used for judge evaluation. For the judge only, the variables
|
|
930
|
+
* `message_history` and `response_to_evaluate` are reserved and will be ignored.
|
|
931
|
+
* @param defaultAiProvider Optional default AI provider to use.
|
|
932
|
+
* @returns A promise that resolves to the TrackedChat instance, or null if the configuration is disabled.
|
|
933
|
+
*
|
|
934
|
+
* @example
|
|
935
|
+
* ```
|
|
936
|
+
* const key = "customer_support_chat";
|
|
937
|
+
* const context = {...};
|
|
938
|
+
* const defaultValue = {
|
|
939
|
+
* enabled: false,
|
|
940
|
+
* model: { name: "gpt-4" },
|
|
941
|
+
* provider: { name: "openai" },
|
|
942
|
+
* messages: [
|
|
943
|
+
* { role: "system", content: "You are a helpful customer support agent." }
|
|
944
|
+
* ]
|
|
945
|
+
* };
|
|
946
|
+
* const variables = { customerName: 'John' };
|
|
947
|
+
*
|
|
948
|
+
* const chat = await client.createChat(key, context, defaultValue, variables);
|
|
949
|
+
* if (chat) {
|
|
950
|
+
* const response = await chat.invoke("I need help with my order");
|
|
951
|
+
* console.log(response.message.content);
|
|
952
|
+
* }
|
|
953
|
+
* ```
|
|
954
|
+
*/
|
|
955
|
+
createChat(key: string, context: LDContext, defaultValue: LDAICompletionConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<TrackedChat | undefined>;
|
|
956
|
+
/**
|
|
957
|
+
* @deprecated Use `createChat` instead. This method will be removed in a future version.
|
|
958
|
+
*/
|
|
959
|
+
initChat(key: string, context: LDContext, defaultValue: LDAICompletionConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<TrackedChat | undefined>;
|
|
960
|
+
/**
|
|
961
|
+
* Creates and returns a new Judge instance for AI evaluation.
|
|
962
|
+
*
|
|
963
|
+
* @param key The key identifying the AI judge configuration to use
|
|
964
|
+
* @param context Standard LDContext used when evaluating flags
|
|
965
|
+
* @param defaultValue A default value representing a standard AI config result
|
|
966
|
+
* @param variables Dictionary of values for instruction interpolation.
|
|
967
|
+
* The variables `message_history` and `response_to_evaluate` are reserved for the judge and will be ignored.
|
|
968
|
+
* @param defaultAiProvider Optional default AI provider to use.
|
|
969
|
+
* @returns Promise that resolves to a Judge instance or undefined if disabled/unsupported
|
|
970
|
+
*
|
|
971
|
+
* @example
|
|
972
|
+
* ```
|
|
973
|
+
* const judge = await client.createJudge(
|
|
974
|
+
* "relevance-judge",
|
|
975
|
+
* context,
|
|
976
|
+
* {
|
|
977
|
+
* enabled: true,
|
|
978
|
+
* model: { name: "gpt-4" },
|
|
979
|
+
* provider: { name: "openai" },
|
|
980
|
+
* evaluationMetricKeys: ['$ld:ai:judge:relevance'],
|
|
981
|
+
* messages: [{ role: 'system', content: 'You are a relevance judge.' }]
|
|
982
|
+
* },
|
|
983
|
+
* { metric: "relevance" }
|
|
984
|
+
* );
|
|
985
|
+
*
|
|
986
|
+
* if (judge) {
|
|
987
|
+
* const result = await judge.evaluate("User question", "AI response");
|
|
988
|
+
* console.log('Relevance score:', result.evals.relevance?.score);
|
|
989
|
+
* }
|
|
990
|
+
* ```
|
|
991
|
+
*/
|
|
992
|
+
createJudge(key: string, context: LDContext, defaultValue: LDAIJudgeConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<Judge | undefined>;
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
/**
|
|
996
|
+
* Interface which represents the required interface components for a sever SDK
|
|
997
|
+
* to work with the AI SDK.
|
|
998
|
+
*/
|
|
999
|
+
interface LDClientMin {
|
|
1000
|
+
variation(key: string, context: LDContext, defaultValue: LDFlagValue, callback?: (err: any, res: LDFlagValue) => void): Promise<LDFlagValue>;
|
|
1001
|
+
track(key: string, context: LDContext, data?: any, metricValue?: number): void;
|
|
1002
|
+
readonly logger?: LDLogger$1;
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
/**
|
|
1006
|
+
* This is the API reference for the LaunchDarkly AI SDK for Server-Side JavaScript.
|
|
1007
|
+
*
|
|
1008
|
+
* In typical usage, you will call {@link initAi} once at startup time to obtain an instance of
|
|
1009
|
+
* {@link LDAIClient}, which provides access to all of the SDK's functionality.
|
|
1010
|
+
*
|
|
1011
|
+
* @packageDocumentation
|
|
1012
|
+
*/
|
|
1013
|
+
|
|
1014
|
+
/**
|
|
1015
|
+
* Initialize a new AI client. This client will be used to perform any AI operations.
|
|
1016
|
+
* @param ldClient The base LaunchDarkly client.
|
|
1017
|
+
* @returns A new AI client.
|
|
1018
|
+
*/
|
|
1019
|
+
declare function initAi(ldClient: LDClientMin): LDAIClient;
|
|
1020
|
+
type LDLogger = common.LDLogger;
|
|
1021
|
+
|
|
1022
|
+
export { AIProvider, AIProviderFactory, type ChatResponse, type EvalScore, Judge, type JudgeResponse, type LDAIAgentConfig, type LDAIAgentConfigDefault, type LDAIAgentRequestConfig, type LDAIClient, type LDAICompletionConfig, type LDAICompletionConfigDefault, type LDAIConfig, type LDAIConfigDefault, type LDAIConfigDefaultKind, type LDAIConfigKind, type LDAIConfigMode, type LDAIConfigTracker, type LDAIJudgeConfig, type LDAIJudgeConfigDefault, type LDAIMetrics, LDFeedbackKind, type LDJudge, type LDJudgeConfiguration, type LDLogger, type LDMessage, type LDModelConfig, type LDProviderConfig, type LDTokenUsage, SUPPORTED_AI_PROVIDERS, type StructuredResponse, type SupportedAIProvider, TrackedChat, createBedrockTokenUsage, createOpenAiUsage, createVercelAISDKTokenUsage, initAi };
|