@launchdarkly/server-sdk-ai 0.13.0 → 0.14.1
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 +39 -0
- package/README.md +1 -1
- package/__tests__/Judge.test.ts +521 -0
- package/__tests__/LDAIClientImpl.test.ts +535 -323
- package/__tests__/LDAIConfigTrackerImpl.test.ts +50 -290
- package/__tests__/TrackedChat.test.ts +5 -5
- package/dist/package.json +53 -0
- package/dist/src/LDAIClientImpl.d.ts +39 -0
- package/dist/src/LDAIClientImpl.d.ts.map +1 -0
- package/dist/src/LDAIClientImpl.js +164 -0
- package/dist/src/LDAIClientImpl.js.map +1 -0
- package/dist/{LDAIConfigTrackerImpl.d.ts → src/LDAIConfigTrackerImpl.d.ts} +12 -11
- package/dist/src/LDAIConfigTrackerImpl.d.ts.map +1 -0
- package/dist/{LDAIConfigTrackerImpl.js → src/LDAIConfigTrackerImpl.js} +21 -44
- package/dist/src/LDAIConfigTrackerImpl.js.map +1 -0
- package/dist/src/LDClientMin.d.ts.map +1 -0
- package/dist/{LDClientMin.js.map → src/LDClientMin.js.map} +1 -1
- package/dist/src/api/LDAIClient.d.ts +258 -0
- package/dist/src/api/LDAIClient.d.ts.map +1 -0
- package/dist/{api → src/api}/LDAIClient.js.map +1 -1
- package/dist/{api → src/api}/chat/TrackedChat.d.ts +22 -4
- package/dist/src/api/chat/TrackedChat.d.ts.map +1 -0
- package/dist/{api → src/api}/chat/TrackedChat.js +43 -2
- package/dist/src/api/chat/TrackedChat.js.map +1 -0
- package/dist/src/api/chat/index.d.ts.map +1 -0
- package/dist/src/api/chat/index.js.map +1 -0
- package/dist/src/api/chat/types.d.ts +22 -0
- package/dist/src/api/chat/types.d.ts.map +1 -0
- package/dist/{api → src/api}/chat/types.js.map +1 -1
- package/dist/{api → src/api}/config/LDAIConfigTracker.d.ts +25 -25
- package/dist/src/api/config/LDAIConfigTracker.d.ts.map +1 -0
- package/dist/src/api/config/LDAIConfigTracker.js.map +1 -0
- package/dist/src/api/config/LDAIConfigUtils.d.ts +2 -0
- package/dist/src/api/config/LDAIConfigUtils.d.ts.map +1 -0
- package/dist/src/api/config/LDAIConfigUtils.js +145 -0
- package/dist/src/api/config/LDAIConfigUtils.js.map +1 -0
- package/dist/src/api/config/index.d.ts +3 -0
- package/dist/src/api/config/index.d.ts.map +1 -0
- package/dist/{api/agents → src/api/config}/index.js +1 -1
- package/dist/src/api/config/index.js.map +1 -0
- package/dist/src/api/config/types.d.ts +206 -0
- package/dist/src/api/config/types.d.ts.map +1 -0
- package/dist/{api/agents/LDAIAgent.js → src/api/config/types.js} +1 -1
- package/dist/src/api/config/types.js.map +1 -0
- package/dist/{api → src/api}/index.d.ts +1 -1
- package/dist/src/api/index.d.ts.map +1 -0
- package/dist/{api → src/api}/index.js +1 -1
- package/dist/src/api/index.js.map +1 -0
- package/dist/src/api/judge/EvaluationSchemaBuilder.d.ts +11 -0
- package/dist/src/api/judge/EvaluationSchemaBuilder.d.ts.map +1 -0
- package/dist/src/api/judge/EvaluationSchemaBuilder.js +52 -0
- package/dist/src/api/judge/EvaluationSchemaBuilder.js.map +1 -0
- package/dist/src/api/judge/Judge.d.ts +63 -0
- package/dist/src/api/judge/Judge.d.ts.map +1 -0
- package/dist/src/api/judge/Judge.js +151 -0
- package/dist/src/api/judge/Judge.js.map +1 -0
- package/dist/src/api/judge/index.d.ts +3 -0
- package/dist/src/api/judge/index.d.ts.map +1 -0
- package/dist/src/api/judge/index.js +6 -0
- package/dist/src/api/judge/index.js.map +1 -0
- package/dist/src/api/judge/types.d.ts +37 -0
- package/dist/src/api/judge/types.d.ts.map +1 -0
- package/dist/{api/config/LDAIConfig.js → src/api/judge/types.js} +1 -1
- package/dist/src/api/judge/types.js.map +1 -0
- package/dist/src/api/metrics/BedrockTokenUsage.d.ts.map +1 -0
- package/dist/src/api/metrics/BedrockTokenUsage.js.map +1 -0
- package/dist/src/api/metrics/LDAIMetrics.d.ts.map +1 -0
- package/dist/src/api/metrics/LDAIMetrics.js.map +1 -0
- package/dist/src/api/metrics/LDFeedbackKind.d.ts.map +1 -0
- package/dist/src/api/metrics/LDFeedbackKind.js.map +1 -0
- package/dist/src/api/metrics/LDTokenUsage.d.ts.map +1 -0
- package/dist/src/api/metrics/LDTokenUsage.js.map +1 -0
- package/dist/src/api/metrics/OpenAiUsage.d.ts.map +1 -0
- package/dist/src/api/metrics/OpenAiUsage.js.map +1 -0
- package/dist/src/api/metrics/VercelAISDKTokenUsage.d.ts.map +1 -0
- package/dist/src/api/metrics/VercelAISDKTokenUsage.js.map +1 -0
- package/dist/src/api/metrics/index.d.ts.map +1 -0
- package/dist/src/api/metrics/index.js.map +1 -0
- package/dist/{api → src/api}/providers/AIProvider.d.ts +20 -3
- package/dist/src/api/providers/AIProvider.d.ts.map +1 -0
- package/dist/src/api/providers/AIProvider.js +88 -0
- package/dist/src/api/providers/AIProvider.js.map +1 -0
- package/dist/{api → src/api}/providers/AIProviderFactory.d.ts +2 -2
- package/dist/src/api/providers/AIProviderFactory.d.ts.map +1 -0
- package/dist/src/api/providers/AIProviderFactory.js.map +1 -0
- package/dist/src/api/providers/index.d.ts.map +1 -0
- package/dist/src/api/providers/index.js.map +1 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js.map +1 -0
- package/docs/assets/search.js +1 -1
- package/docs/classes/AIProvider.html +55 -20
- package/docs/classes/AIProviderFactory.html +27 -17
- package/docs/classes/Judge.html +322 -0
- package/docs/classes/TrackedChat.html +97 -29
- package/docs/enums/LDFeedbackKind.html +22 -12
- package/docs/functions/createBedrockTokenUsage.html +20 -10
- package/docs/functions/createOpenAiUsage.html +20 -10
- package/docs/functions/createVercelAISDKTokenUsage.html +20 -10
- package/docs/functions/initAi.html +20 -10
- package/docs/index.html +36 -16
- package/docs/interfaces/ChatResponse.html +35 -14
- package/docs/interfaces/EvalScore.html +119 -0
- package/docs/interfaces/JudgeResponse.html +139 -0
- package/docs/interfaces/LDAIAgentConfig.html +90 -31
- package/docs/interfaces/{LDAIAgent.html → LDAIAgentConfigDefault.html} +51 -41
- package/docs/interfaces/LDAIAgentRequestConfig.html +129 -0
- package/docs/interfaces/LDAIClient.html +234 -40
- package/docs/interfaces/{VercelAISDKConfig.html → LDAICompletionConfig.html} +96 -90
- package/docs/interfaces/LDAICompletionConfigDefault.html +155 -0
- package/docs/interfaces/LDAIConfig.html +52 -75
- package/docs/interfaces/LDAIConfigDefault.html +133 -0
- package/docs/interfaces/LDAIConfigTracker.html +102 -63
- package/docs/interfaces/LDAIJudgeConfig.html +178 -0
- package/docs/interfaces/LDAIJudgeConfigDefault.html +155 -0
- package/docs/interfaces/LDAIMetrics.html +22 -12
- package/docs/interfaces/LDJudge.html +119 -0
- package/docs/interfaces/{VercelAISDKMapOptions.html → LDJudgeConfiguration.html} +35 -23
- package/docs/interfaces/LDLogger.html +19 -9
- package/docs/interfaces/LDMessage.html +22 -12
- package/docs/interfaces/LDModelConfig.html +23 -13
- package/docs/interfaces/LDProviderConfig.html +21 -11
- package/docs/interfaces/LDTokenUsage.html +23 -13
- package/docs/interfaces/StructuredResponse.html +129 -0
- package/docs/types/{VercelAISDKProvider.html → LDAIConfigDefaultKind.html} +26 -35
- package/docs/types/{LDAIAgentDefaults.html → LDAIConfigKind.html} +24 -14
- package/docs/types/{LDAIDefaults.html → LDAIConfigMode.html} +24 -24
- package/docs/types/SupportedAIProvider.html +20 -10
- package/docs/variables/SUPPORTED_AI_PROVIDERS.html +20 -10
- package/package.json +3 -3
- package/src/LDAIClientImpl.ts +222 -176
- package/src/LDAIConfigTrackerImpl.ts +31 -54
- package/src/api/LDAIClient.ts +166 -33
- package/src/api/chat/TrackedChat.ts +68 -5
- package/src/api/chat/types.ts +8 -1
- package/src/api/config/LDAIConfigTracker.ts +27 -30
- package/src/api/config/LDAIConfigUtils.ts +212 -0
- package/src/api/config/index.ts +2 -2
- package/src/api/config/types.ts +260 -0
- package/src/api/index.ts +1 -1
- package/src/api/judge/EvaluationSchemaBuilder.ts +54 -0
- package/src/api/judge/Judge.ts +218 -0
- package/src/api/judge/index.ts +2 -0
- package/src/api/judge/types.ts +41 -0
- package/src/api/providers/AIProvider.ts +54 -3
- package/src/api/providers/AIProviderFactory.ts +4 -4
- package/tsconfig.json +3 -3
- package/tsconfig.ref.json +1 -1
- package/__tests__/LDAIConfigMapper.test.ts +0 -159
- package/dist/LDAIClientImpl.d.ts +0 -23
- package/dist/LDAIClientImpl.d.ts.map +0 -1
- package/dist/LDAIClientImpl.js +0 -128
- package/dist/LDAIClientImpl.js.map +0 -1
- package/dist/LDAIConfigMapper.d.ts +0 -14
- package/dist/LDAIConfigMapper.d.ts.map +0 -1
- package/dist/LDAIConfigMapper.js +0 -59
- package/dist/LDAIConfigMapper.js.map +0 -1
- package/dist/LDAIConfigTrackerImpl.d.ts.map +0 -1
- package/dist/LDAIConfigTrackerImpl.js.map +0 -1
- package/dist/LDClientMin.d.ts.map +0 -1
- package/dist/api/LDAIClient.d.ts +0 -169
- package/dist/api/LDAIClient.d.ts.map +0 -1
- package/dist/api/agents/LDAIAgent.d.ts +0 -32
- package/dist/api/agents/LDAIAgent.d.ts.map +0 -1
- package/dist/api/agents/LDAIAgent.js.map +0 -1
- package/dist/api/agents/index.d.ts +0 -2
- package/dist/api/agents/index.d.ts.map +0 -1
- package/dist/api/agents/index.js.map +0 -1
- package/dist/api/chat/TrackedChat.d.ts.map +0 -1
- package/dist/api/chat/TrackedChat.js.map +0 -1
- package/dist/api/chat/index.d.ts.map +0 -1
- package/dist/api/chat/index.js.map +0 -1
- package/dist/api/chat/types.d.ts +0 -16
- package/dist/api/chat/types.d.ts.map +0 -1
- package/dist/api/config/LDAIConfig.d.ts +0 -95
- package/dist/api/config/LDAIConfig.d.ts.map +0 -1
- package/dist/api/config/LDAIConfig.js.map +0 -1
- package/dist/api/config/LDAIConfigTracker.d.ts.map +0 -1
- package/dist/api/config/LDAIConfigTracker.js.map +0 -1
- package/dist/api/config/VercelAISDK.d.ts +0 -31
- package/dist/api/config/VercelAISDK.d.ts.map +0 -1
- package/dist/api/config/VercelAISDK.js +0 -3
- package/dist/api/config/VercelAISDK.js.map +0 -1
- package/dist/api/config/index.d.ts +0 -4
- package/dist/api/config/index.d.ts.map +0 -1
- package/dist/api/config/index.js +0 -19
- package/dist/api/config/index.js.map +0 -1
- package/dist/api/index.d.ts.map +0 -1
- package/dist/api/index.js.map +0 -1
- package/dist/api/metrics/BedrockTokenUsage.d.ts.map +0 -1
- package/dist/api/metrics/BedrockTokenUsage.js.map +0 -1
- package/dist/api/metrics/LDAIMetrics.d.ts.map +0 -1
- package/dist/api/metrics/LDAIMetrics.js.map +0 -1
- package/dist/api/metrics/LDFeedbackKind.d.ts.map +0 -1
- package/dist/api/metrics/LDFeedbackKind.js.map +0 -1
- package/dist/api/metrics/LDTokenUsage.d.ts.map +0 -1
- package/dist/api/metrics/LDTokenUsage.js.map +0 -1
- package/dist/api/metrics/OpenAiUsage.d.ts.map +0 -1
- package/dist/api/metrics/OpenAiUsage.js.map +0 -1
- package/dist/api/metrics/VercelAISDKTokenUsage.d.ts.map +0 -1
- package/dist/api/metrics/VercelAISDKTokenUsage.js.map +0 -1
- package/dist/api/metrics/index.d.ts.map +0 -1
- package/dist/api/metrics/index.js.map +0 -1
- package/dist/api/providers/AIProvider.d.ts.map +0 -1
- package/dist/api/providers/AIProvider.js +0 -31
- package/dist/api/providers/AIProvider.js.map +0 -1
- package/dist/api/providers/AIProviderFactory.d.ts.map +0 -1
- package/dist/api/providers/AIProviderFactory.js.map +0 -1
- package/dist/api/providers/index.d.ts.map +0 -1
- package/dist/api/providers/index.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/src/LDAIConfigMapper.ts +0 -69
- package/src/api/agents/LDAIAgent.ts +0 -36
- package/src/api/agents/index.ts +0 -1
- package/src/api/config/LDAIConfig.ts +0 -104
- package/src/api/config/VercelAISDK.ts +0 -33
- /package/dist/{LDClientMin.d.ts → src/LDClientMin.d.ts} +0 -0
- /package/dist/{LDClientMin.js → src/LDClientMin.js} +0 -0
- /package/dist/{api → src/api}/LDAIClient.js +0 -0
- /package/dist/{api → src/api}/chat/index.d.ts +0 -0
- /package/dist/{api → src/api}/chat/index.js +0 -0
- /package/dist/{api → src/api}/chat/types.js +0 -0
- /package/dist/{api → src/api}/config/LDAIConfigTracker.js +0 -0
- /package/dist/{api → src/api}/metrics/BedrockTokenUsage.d.ts +0 -0
- /package/dist/{api → src/api}/metrics/BedrockTokenUsage.js +0 -0
- /package/dist/{api → src/api}/metrics/LDAIMetrics.d.ts +0 -0
- /package/dist/{api → src/api}/metrics/LDAIMetrics.js +0 -0
- /package/dist/{api → src/api}/metrics/LDFeedbackKind.d.ts +0 -0
- /package/dist/{api → src/api}/metrics/LDFeedbackKind.js +0 -0
- /package/dist/{api → src/api}/metrics/LDTokenUsage.d.ts +0 -0
- /package/dist/{api → src/api}/metrics/LDTokenUsage.js +0 -0
- /package/dist/{api → src/api}/metrics/OpenAiUsage.d.ts +0 -0
- /package/dist/{api → src/api}/metrics/OpenAiUsage.js +0 -0
- /package/dist/{api → src/api}/metrics/VercelAISDKTokenUsage.d.ts +0 -0
- /package/dist/{api → src/api}/metrics/VercelAISDKTokenUsage.js +0 -0
- /package/dist/{api → src/api}/metrics/index.d.ts +0 -0
- /package/dist/{api → src/api}/metrics/index.js +0 -0
- /package/dist/{api → src/api}/providers/AIProviderFactory.js +0 -0
- /package/dist/{api → src/api}/providers/index.d.ts +0 -0
- /package/dist/{api → src/api}/providers/index.js +0 -0
- /package/dist/{index.d.ts → src/index.d.ts} +0 -0
- /package/dist/{index.js → src/index.js} +0 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { LDContext } from '@launchdarkly/js-server-sdk-common';
|
|
2
|
+
import { TrackedChat } from './chat';
|
|
3
|
+
import { LDAIAgentConfig, LDAIAgentConfigDefault, LDAIAgentRequestConfig, LDAICompletionConfig, LDAICompletionConfigDefault, LDAIJudgeConfig, LDAIJudgeConfigDefault } from './config';
|
|
4
|
+
import { Judge } from './judge/Judge';
|
|
5
|
+
import { SupportedAIProvider } from './providers';
|
|
6
|
+
/**
|
|
7
|
+
* Interface for performing AI operations using LaunchDarkly.
|
|
8
|
+
*/
|
|
9
|
+
export interface LDAIClient {
|
|
10
|
+
/**
|
|
11
|
+
* Retrieves and processes a completion AI Config based on the provided key, LaunchDarkly context,
|
|
12
|
+
* and variables. This includes the model configuration and the customized messages.
|
|
13
|
+
*
|
|
14
|
+
* @param key The key of the AI Config.
|
|
15
|
+
* @param context The LaunchDarkly context object that contains relevant information about the
|
|
16
|
+
* current environment, user, or session. This context may influence how the configuration is
|
|
17
|
+
* processed or personalized.
|
|
18
|
+
* @param defaultValue A fallback value containing model configuration and messages. This will
|
|
19
|
+
* be used if the configuration is not available from LaunchDarkly.
|
|
20
|
+
* @param variables A map of key-value pairs representing dynamic variables to be injected into
|
|
21
|
+
* the message content. The keys correspond to placeholders within the template, and the values
|
|
22
|
+
* are the corresponding replacements.
|
|
23
|
+
*
|
|
24
|
+
* @returns The AI `config`, customized `messages`, and a `tracker`. If the configuration cannot be accessed from
|
|
25
|
+
* LaunchDarkly, then the return value will include information from the `defaultValue`. The returned `tracker` can
|
|
26
|
+
* be used to track AI operation metrics (latency, token usage, etc.).
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```
|
|
30
|
+
* const key = "welcome_prompt";
|
|
31
|
+
* const context = {...};
|
|
32
|
+
* const variables = {username: 'john'};
|
|
33
|
+
* const defaultValue = {
|
|
34
|
+
* enabled: false,
|
|
35
|
+
* model: { name: 'gpt-4' },
|
|
36
|
+
* provider: { name: 'openai' },
|
|
37
|
+
* };
|
|
38
|
+
*
|
|
39
|
+
* const result = completionConfig(key, context, defaultValue, variables);
|
|
40
|
+
* // Output:
|
|
41
|
+
* {
|
|
42
|
+
* enabled: true,
|
|
43
|
+
* config: {
|
|
44
|
+
* modelId: "gpt-4o",
|
|
45
|
+
* temperature: 0.2,
|
|
46
|
+
* maxTokens: 4096,
|
|
47
|
+
* userDefinedKey: "myValue",
|
|
48
|
+
* },
|
|
49
|
+
* messages: [
|
|
50
|
+
* {
|
|
51
|
+
* role: "system",
|
|
52
|
+
* content: "You are an amazing GPT."
|
|
53
|
+
* },
|
|
54
|
+
* {
|
|
55
|
+
* role: "user",
|
|
56
|
+
* content: "Explain how you're an amazing GPT."
|
|
57
|
+
* }
|
|
58
|
+
* ],
|
|
59
|
+
* tracker: ...
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
completionConfig(key: string, context: LDContext, defaultValue: LDAICompletionConfigDefault, variables?: Record<string, unknown>): Promise<LDAICompletionConfig>;
|
|
64
|
+
/**
|
|
65
|
+
* @deprecated Use `completionConfig` instead. This method will be removed in a future version.
|
|
66
|
+
*/
|
|
67
|
+
config(key: string, context: LDContext, defaultValue: LDAICompletionConfigDefault, variables?: Record<string, unknown>): Promise<LDAICompletionConfig>;
|
|
68
|
+
/**
|
|
69
|
+
* Retrieves and processes a single AI Config agent based on the provided key, LaunchDarkly context,
|
|
70
|
+
* and variables. This includes the model configuration and the customized instructions.
|
|
71
|
+
*
|
|
72
|
+
* @param key The key of the AI Config agent.
|
|
73
|
+
* @param context The LaunchDarkly context object that contains relevant information about the
|
|
74
|
+
* current environment, user, or session. This context may influence how the configuration is
|
|
75
|
+
* processed or personalized.
|
|
76
|
+
* @param defaultValue A fallback value containing model configuration and instructions.
|
|
77
|
+
* @param variables A map of key-value pairs representing dynamic variables to be injected into
|
|
78
|
+
* the instructions. The keys correspond to placeholders within the template, and the values
|
|
79
|
+
* are the corresponding replacements.
|
|
80
|
+
*
|
|
81
|
+
* @returns An AI agent with customized `instructions` and a `tracker`. If the configuration
|
|
82
|
+
* cannot be accessed from LaunchDarkly, then the return value will include information from the
|
|
83
|
+
* `defaultValue`. The returned `tracker` can be used to track AI operation metrics (latency, token usage, etc.).
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```
|
|
87
|
+
* const key = "research_agent";
|
|
88
|
+
* const context = {...};
|
|
89
|
+
* const variables = { topic: 'climate change' };
|
|
90
|
+
* const agentConfig = await client.agentConfig(key, context, {
|
|
91
|
+
* enabled: true,
|
|
92
|
+
* model: { name: 'gpt-4' },
|
|
93
|
+
* provider: { name: 'openai' },
|
|
94
|
+
* instructions: 'You are a research assistant.',
|
|
95
|
+
* }, variables);
|
|
96
|
+
*
|
|
97
|
+
* const researchResult = agentConfig.instructions; // Interpolated instructions
|
|
98
|
+
* agentConfig.tracker.trackSuccess();
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
agentConfig(key: string, context: LDContext, defaultValue: LDAIAgentConfigDefault, variables?: Record<string, unknown>): Promise<LDAIAgentConfig>;
|
|
102
|
+
/**
|
|
103
|
+
* @deprecated Use `agentConfig` instead. This method will be removed in a future version.
|
|
104
|
+
*/
|
|
105
|
+
agent(key: string, context: LDContext, defaultValue: LDAIAgentConfigDefault, variables?: Record<string, unknown>): Promise<LDAIAgentConfig>;
|
|
106
|
+
/**
|
|
107
|
+
* Retrieves and processes a Judge AI Config based on the provided key, LaunchDarkly context,
|
|
108
|
+
* and variables. This includes the model configuration and the customized messages for evaluation.
|
|
109
|
+
*
|
|
110
|
+
* @param key The key of the Judge AI Config.
|
|
111
|
+
* @param context The LaunchDarkly context object that contains relevant information about the
|
|
112
|
+
* current environment, user, or session. This context may influence how the configuration is
|
|
113
|
+
* processed or personalized.
|
|
114
|
+
* @param defaultValue A fallback value containing model configuration and messages. This will
|
|
115
|
+
* be used if the configuration is not available from LaunchDarkly.
|
|
116
|
+
* @param variables Optional variables for template interpolation in messages and instructions.
|
|
117
|
+
* @returns A promise that resolves to a tracked judge configuration.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const judgeConf = await client.judgeConfig(key, context, {
|
|
122
|
+
* enabled: true,
|
|
123
|
+
* model: { name: 'gpt-4' },
|
|
124
|
+
* provider: { name: 'openai' },
|
|
125
|
+
* evaluationMetricKeys: ['$ld:ai:judge:relevance'],
|
|
126
|
+
* messages: [{ role: 'system', content: 'You are a relevance judge.' }]
|
|
127
|
+
* }, variables);
|
|
128
|
+
*
|
|
129
|
+
* const config = judgeConf.config; // Interpolated configuration
|
|
130
|
+
* judgeConf.tracker.trackSuccess();
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
judgeConfig(key: string, context: LDContext, defaultValue: LDAIJudgeConfigDefault, variables?: Record<string, unknown>): Promise<LDAIJudgeConfig>;
|
|
134
|
+
/**
|
|
135
|
+
* Retrieves and processes multiple AI Config agents based on the provided agent configurations
|
|
136
|
+
* and LaunchDarkly context. This includes the model configuration and the customized instructions.
|
|
137
|
+
*
|
|
138
|
+
* @param agentConfigs An array of agent configurations, each containing the agent key, default configuration,
|
|
139
|
+
* and variables for instructions interpolation.
|
|
140
|
+
* @param context The LaunchDarkly context object that contains relevant information about the
|
|
141
|
+
* current environment, user, or session. This context may influence how the configuration is
|
|
142
|
+
* processed or personalized.
|
|
143
|
+
*
|
|
144
|
+
* @returns A map of agent keys to their respective AI agents with customized `instructions` and `tracker`.
|
|
145
|
+
* If a configuration cannot be accessed from LaunchDarkly, then the return value will include information
|
|
146
|
+
* from the respective `defaultValue`. The returned `tracker` can be used to track AI operation metrics
|
|
147
|
+
* (latency, token usage, etc.).
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```
|
|
151
|
+
* const agentConfigsList = [
|
|
152
|
+
* {
|
|
153
|
+
* key: 'research_agent',
|
|
154
|
+
* defaultValue: {
|
|
155
|
+
* enabled: true,
|
|
156
|
+
* model: { name: 'gpt-4' },
|
|
157
|
+
* provider: { name: 'openai' },
|
|
158
|
+
* instructions: 'You are a research assistant.'
|
|
159
|
+
* },
|
|
160
|
+
* variables: { topic: 'climate change' }
|
|
161
|
+
* },
|
|
162
|
+
* {
|
|
163
|
+
* key: 'writing_agent',
|
|
164
|
+
* defaultValue: {
|
|
165
|
+
* enabled: true,
|
|
166
|
+
* model: { name: 'gpt-4' },
|
|
167
|
+
* provider: { name: 'openai' },
|
|
168
|
+
* instructions: 'You are a writing assistant.'
|
|
169
|
+
* },
|
|
170
|
+
* variables: { style: 'academic' }
|
|
171
|
+
* }
|
|
172
|
+
* ] as const;
|
|
173
|
+
* const context = {...};
|
|
174
|
+
*
|
|
175
|
+
* const configs = await client.agentConfigs(agentConfigsList, context);
|
|
176
|
+
* const researchResult = configs["research_agent"].instructions; // Interpolated instructions
|
|
177
|
+
* configs["research_agent"].tracker.trackSuccess();
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
agentConfigs<const T extends readonly LDAIAgentRequestConfig[]>(agentConfigs: T, context: LDContext): Promise<Record<T[number]['key'], LDAIAgentConfig>>;
|
|
181
|
+
/**
|
|
182
|
+
* @deprecated Use `agentConfigs` instead. This method will be removed in a future version.
|
|
183
|
+
*/
|
|
184
|
+
agents<const T extends readonly LDAIAgentRequestConfig[]>(agentConfigs: T, context: LDContext): Promise<Record<T[number]['key'], LDAIAgentConfig>>;
|
|
185
|
+
/**
|
|
186
|
+
* Returns a TrackedChat instance for chat interactions.
|
|
187
|
+
* This method serves as the primary entry point for creating TrackedChat instances from configuration.
|
|
188
|
+
*
|
|
189
|
+
* @param key The key identifying the AI chat configuration to use.
|
|
190
|
+
* @param context The standard LDContext used when evaluating flags.
|
|
191
|
+
* @param defaultValue A default value representing a standard AI chat config result.
|
|
192
|
+
* @param variables Dictionary of values for instruction interpolation.
|
|
193
|
+
* The variables will also be used for judge evaluation. For the judge only, the variables
|
|
194
|
+
* `message_history` and `response_to_evaluate` are reserved and will be ignored.
|
|
195
|
+
* @param defaultAiProvider Optional default AI provider to use.
|
|
196
|
+
* @returns A promise that resolves to the TrackedChat instance, or null if the configuration is disabled.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```
|
|
200
|
+
* const key = "customer_support_chat";
|
|
201
|
+
* const context = {...};
|
|
202
|
+
* const defaultValue = {
|
|
203
|
+
* enabled: false,
|
|
204
|
+
* model: { name: "gpt-4" },
|
|
205
|
+
* provider: { name: "openai" },
|
|
206
|
+
* messages: [
|
|
207
|
+
* { role: "system", content: "You are a helpful customer support agent." }
|
|
208
|
+
* ]
|
|
209
|
+
* };
|
|
210
|
+
* const variables = { customerName: 'John' };
|
|
211
|
+
*
|
|
212
|
+
* const chat = await client.createChat(key, context, defaultValue, variables);
|
|
213
|
+
* if (chat) {
|
|
214
|
+
* const response = await chat.invoke("I need help with my order");
|
|
215
|
+
* console.log(response.message.content);
|
|
216
|
+
* }
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
createChat(key: string, context: LDContext, defaultValue: LDAICompletionConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<TrackedChat | undefined>;
|
|
220
|
+
/**
|
|
221
|
+
* @deprecated Use `createChat` instead. This method will be removed in a future version.
|
|
222
|
+
*/
|
|
223
|
+
initChat(key: string, context: LDContext, defaultValue: LDAICompletionConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<TrackedChat | undefined>;
|
|
224
|
+
/**
|
|
225
|
+
* Creates and returns a new Judge instance for AI evaluation.
|
|
226
|
+
*
|
|
227
|
+
* @param key The key identifying the AI judge configuration to use
|
|
228
|
+
* @param context Standard LDContext used when evaluating flags
|
|
229
|
+
* @param defaultValue A default value representing a standard AI config result
|
|
230
|
+
* @param variables Dictionary of values for instruction interpolation.
|
|
231
|
+
* The variables `message_history` and `response_to_evaluate` are reserved for the judge and will be ignored.
|
|
232
|
+
* @param defaultAiProvider Optional default AI provider to use.
|
|
233
|
+
* @returns Promise that resolves to a Judge instance or undefined if disabled/unsupported
|
|
234
|
+
*
|
|
235
|
+
* @example
|
|
236
|
+
* ```
|
|
237
|
+
* const judge = await client.createJudge(
|
|
238
|
+
* "relevance-judge",
|
|
239
|
+
* context,
|
|
240
|
+
* {
|
|
241
|
+
* enabled: true,
|
|
242
|
+
* model: { name: "gpt-4" },
|
|
243
|
+
* provider: { name: "openai" },
|
|
244
|
+
* evaluationMetricKeys: ['$ld:ai:judge:relevance'],
|
|
245
|
+
* messages: [{ role: 'system', content: 'You are a relevance judge.' }]
|
|
246
|
+
* },
|
|
247
|
+
* { metric: "relevance" }
|
|
248
|
+
* );
|
|
249
|
+
*
|
|
250
|
+
* if (judge) {
|
|
251
|
+
* const result = await judge.evaluate("User question", "AI response");
|
|
252
|
+
* console.log('Relevance score:', result.evals.relevance?.score);
|
|
253
|
+
* }
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
createJudge(key: string, context: LDContext, defaultValue: LDAIJudgeConfigDefault, variables?: Record<string, unknown>, defaultAiProvider?: SupportedAIProvider): Promise<Judge | undefined>;
|
|
257
|
+
}
|
|
258
|
+
//# sourceMappingURL=LDAIClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LDAIClient.d.ts","sourceRoot":"","sources":["../../../src/api/LDAIClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,oCAAoC,CAAC;AAE/D,OAAO,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,EACL,eAAe,EACf,sBAAsB,EACtB,sBAAsB,EACtB,oBAAoB,EACpB,2BAA2B,EAC3B,eAAe,EACf,sBAAsB,EACvB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAElD;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAoDG;IACH,gBAAgB,CACd,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,SAAS,EAClB,YAAY,EAAE,2BAA2B,EACzC,SAA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,MAAM,CAAC,KAAK,CAAC,CAAC,SAAS,SAAS,sBAAsB,EAAE,EACtD,YAAY,EAAE,CAAC,EACf,OAAO,EAAE,SAAS,GACjB,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,EAAE,eAAe,CAAC,CAAC,CAAC;IAEtD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACH,UAAU,CACR,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,SAAS,EAClB,YAAY,EAAE,2BAA2B,EACzC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,iBAAiB,CAAC,EAAE,mBAAmB,GACtC,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CAAC;IAEpC;;OAEG;IACH,QAAQ,CACN,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,SAAS,EAClB,YAAY,EAAE,2BAA2B,EACzC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,iBAAiB,CAAC,EAAE,mBAAmB,GACtC,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CAAC;IAEpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA+BG;IACH,WAAW,CACT,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,SAAS,EAClB,YAAY,EAAE,sBAAsB,EACpC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,iBAAiB,CAAC,EAAE,mBAAmB,GACtC,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC,CAAC;CAC/B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LDAIClient.js","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"LDAIClient.js","sourceRoot":"","sources":["../../../src/api/LDAIClient.ts"],"names":[],"mappings":""}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LDLogger } from '@launchdarkly/js-server-sdk-common';
|
|
2
2
|
import { LDAIConfigTracker } from '../config/LDAIConfigTracker';
|
|
3
|
+
import { LDAICompletionConfig, LDMessage } from '../config/types';
|
|
4
|
+
import { Judge } from '../judge/Judge';
|
|
3
5
|
import { AIProvider } from '../providers/AIProvider';
|
|
4
6
|
import { ChatResponse } from './types';
|
|
5
7
|
/**
|
|
@@ -9,20 +11,31 @@ import { ChatResponse } from './types';
|
|
|
9
11
|
* the actual model invocation to the provider.
|
|
10
12
|
*/
|
|
11
13
|
export declare class TrackedChat {
|
|
12
|
-
protected readonly aiConfig:
|
|
14
|
+
protected readonly aiConfig: LDAICompletionConfig;
|
|
13
15
|
protected readonly tracker: LDAIConfigTracker;
|
|
14
16
|
protected readonly provider: AIProvider;
|
|
17
|
+
protected readonly judges: Record<string, Judge>;
|
|
18
|
+
private readonly _logger?;
|
|
15
19
|
protected messages: LDMessage[];
|
|
16
|
-
constructor(aiConfig:
|
|
20
|
+
constructor(aiConfig: LDAICompletionConfig, tracker: LDAIConfigTracker, provider: AIProvider, judges?: Record<string, Judge>, _logger?: LDLogger | undefined);
|
|
17
21
|
/**
|
|
18
22
|
* Invoke the chat model with a prompt string.
|
|
19
23
|
* This method handles conversation management and tracking, delegating to the provider's invokeModel method.
|
|
20
24
|
*/
|
|
21
25
|
invoke(prompt: string): Promise<ChatResponse>;
|
|
26
|
+
/**
|
|
27
|
+
* Evaluates the response with all configured judges.
|
|
28
|
+
* Returns a promise that resolves to an array of evaluation results.
|
|
29
|
+
*
|
|
30
|
+
* @param messages Array of messages representing the conversation history
|
|
31
|
+
* @param response The AI response to be evaluated
|
|
32
|
+
* @returns Promise resolving to array of judge evaluation results
|
|
33
|
+
*/
|
|
34
|
+
private _evaluateWithJudges;
|
|
22
35
|
/**
|
|
23
36
|
* Get the underlying AI configuration used to initialize this TrackedChat.
|
|
24
37
|
*/
|
|
25
|
-
getConfig():
|
|
38
|
+
getConfig(): LDAICompletionConfig;
|
|
26
39
|
/**
|
|
27
40
|
* Get the underlying AI configuration tracker used to initialize this TrackedChat.
|
|
28
41
|
*/
|
|
@@ -32,6 +45,11 @@ export declare class TrackedChat {
|
|
|
32
45
|
* This provides direct access to the provider for advanced use cases.
|
|
33
46
|
*/
|
|
34
47
|
getProvider(): AIProvider;
|
|
48
|
+
/**
|
|
49
|
+
* Get the judges associated with this TrackedChat.
|
|
50
|
+
* Returns a record of judge instances keyed by their configuration keys.
|
|
51
|
+
*/
|
|
52
|
+
getJudges(): Record<string, Judge>;
|
|
35
53
|
/**
|
|
36
54
|
* Append messages to the conversation history.
|
|
37
55
|
* Adds messages to the conversation history without invoking the model,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TrackedChat.d.ts","sourceRoot":"","sources":["../../../../src/api/chat/TrackedChat.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oCAAoC,CAAC;AAE9D,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAAE,oBAAoB,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEvC,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEvC;;;;;GAKG;AACH,qBAAa,WAAW;IAIpB,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,oBAAoB;IACjD,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,iBAAiB;IAC7C,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,UAAU;IACvC,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC;IAChD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC;IAP3B,SAAS,CAAC,QAAQ,EAAE,SAAS,EAAE,CAAC;gBAGX,QAAQ,EAAE,oBAAoB,EAC9B,OAAO,EAAE,iBAAiB,EAC1B,QAAQ,EAAE,UAAU,EACpB,MAAM,GAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAM,EACpC,OAAO,CAAC,sBAAU;IAKrC;;;OAGG;IACG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IA6BnD;;;;;;;OAOG;YACW,mBAAmB;IAoCjC;;OAEG;IACH,SAAS,IAAI,oBAAoB;IAIjC;;OAEG;IACH,UAAU,IAAI,iBAAiB;IAI/B;;;OAGG;IACH,WAAW,IAAI,UAAU;IAIzB;;;OAGG;IACH,SAAS,IAAI,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC;IAIlC;;;;;;OAMG;IACH,cAAc,CAAC,QAAQ,EAAE,SAAS,EAAE,GAAG,IAAI;IAI3C;;;;;;;;OAQG;IACH,WAAW,CAAC,qBAAqB,GAAE,OAAe,GAAG,SAAS,EAAE;CAOjE"}
|
|
@@ -8,10 +8,12 @@ exports.TrackedChat = void 0;
|
|
|
8
8
|
* the actual model invocation to the provider.
|
|
9
9
|
*/
|
|
10
10
|
class TrackedChat {
|
|
11
|
-
constructor(aiConfig, tracker, provider) {
|
|
11
|
+
constructor(aiConfig, tracker, provider, judges = {}, _logger) {
|
|
12
12
|
this.aiConfig = aiConfig;
|
|
13
13
|
this.tracker = tracker;
|
|
14
14
|
this.provider = provider;
|
|
15
|
+
this.judges = judges;
|
|
16
|
+
this._logger = _logger;
|
|
15
17
|
this.messages = [];
|
|
16
18
|
}
|
|
17
19
|
/**
|
|
@@ -19,6 +21,7 @@ class TrackedChat {
|
|
|
19
21
|
* This method handles conversation management and tracking, delegating to the provider's invokeModel method.
|
|
20
22
|
*/
|
|
21
23
|
async invoke(prompt) {
|
|
24
|
+
var _a;
|
|
22
25
|
// Convert prompt string to LDMessage with role 'user' and add to conversation history
|
|
23
26
|
const userMessage = {
|
|
24
27
|
role: 'user',
|
|
@@ -30,10 +33,41 @@ class TrackedChat {
|
|
|
30
33
|
const allMessages = [...configMessages, ...this.messages];
|
|
31
34
|
// Delegate to provider-specific implementation with tracking
|
|
32
35
|
const response = await this.tracker.trackMetricsOf((result) => result.metrics, () => this.provider.invokeModel(allMessages));
|
|
33
|
-
|
|
36
|
+
if (((_a = this.aiConfig.judgeConfiguration) === null || _a === void 0 ? void 0 : _a.judges) &&
|
|
37
|
+
this.aiConfig.judgeConfiguration.judges.length > 0) {
|
|
38
|
+
response.evaluations = this._evaluateWithJudges(this.messages, response);
|
|
39
|
+
}
|
|
34
40
|
this.messages.push(response.message);
|
|
35
41
|
return response;
|
|
36
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Evaluates the response with all configured judges.
|
|
45
|
+
* Returns a promise that resolves to an array of evaluation results.
|
|
46
|
+
*
|
|
47
|
+
* @param messages Array of messages representing the conversation history
|
|
48
|
+
* @param response The AI response to be evaluated
|
|
49
|
+
* @returns Promise resolving to array of judge evaluation results
|
|
50
|
+
*/
|
|
51
|
+
async _evaluateWithJudges(messages, response) {
|
|
52
|
+
const judgeConfigs = this.aiConfig.judgeConfiguration.judges;
|
|
53
|
+
// Start all judge evaluations in parallel
|
|
54
|
+
const evaluationPromises = judgeConfigs.map(async (judgeConfig) => {
|
|
55
|
+
var _a;
|
|
56
|
+
const judge = this.judges[judgeConfig.key];
|
|
57
|
+
if (!judge) {
|
|
58
|
+
(_a = this._logger) === null || _a === void 0 ? void 0 : _a.warn(`Judge configuration is not enabled: ${judgeConfig.key}`, this.tracker.getTrackData());
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
const judgeResponse = await judge.evaluateMessages(messages, response, judgeConfig.samplingRate);
|
|
62
|
+
if (judgeResponse && judgeResponse.success) {
|
|
63
|
+
this.tracker.trackJudgeResponse(judgeResponse);
|
|
64
|
+
}
|
|
65
|
+
return judgeResponse;
|
|
66
|
+
});
|
|
67
|
+
// ensure all evaluations complete even if some fail
|
|
68
|
+
const results = await Promise.allSettled(evaluationPromises);
|
|
69
|
+
return results.map((result) => (result.status === 'fulfilled' ? result.value : undefined));
|
|
70
|
+
}
|
|
37
71
|
/**
|
|
38
72
|
* Get the underlying AI configuration used to initialize this TrackedChat.
|
|
39
73
|
*/
|
|
@@ -53,6 +87,13 @@ class TrackedChat {
|
|
|
53
87
|
getProvider() {
|
|
54
88
|
return this.provider;
|
|
55
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Get the judges associated with this TrackedChat.
|
|
92
|
+
* Returns a record of judge instances keyed by their configuration keys.
|
|
93
|
+
*/
|
|
94
|
+
getJudges() {
|
|
95
|
+
return this.judges;
|
|
96
|
+
}
|
|
56
97
|
/**
|
|
57
98
|
* Append messages to the conversation history.
|
|
58
99
|
* Adds messages to the conversation history without invoking the model,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TrackedChat.js","sourceRoot":"","sources":["../../../../src/api/chat/TrackedChat.ts"],"names":[],"mappings":";;;AASA;;;;;GAKG;AACH,MAAa,WAAW;IAGtB,YACqB,QAA8B,EAC9B,OAA0B,EAC1B,QAAoB,EACpB,SAAgC,EAAE,EACpC,OAAkB;QAJhB,aAAQ,GAAR,QAAQ,CAAsB;QAC9B,YAAO,GAAP,OAAO,CAAmB;QAC1B,aAAQ,GAAR,QAAQ,CAAY;QACpB,WAAM,GAAN,MAAM,CAA4B;QACpC,YAAO,GAAP,OAAO,CAAW;QAEnC,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;IACrB,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,MAAM,CAAC,MAAc;;QACzB,sFAAsF;QACtF,MAAM,WAAW,GAAc;YAC7B,IAAI,EAAE,MAAM;YACZ,OAAO,EAAE,MAAM;SAChB,CAAC;QACF,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;QAEhC,uEAAuE;QACvE,MAAM,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,IAAI,EAAE,CAAC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;CACF;AAnJD,kCAmJC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/api/chat/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/api/chat/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,gDAA8B"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { LDMessage } from '../config/types';
|
|
2
|
+
import { JudgeResponse } from '../judge/types';
|
|
3
|
+
import { LDAIMetrics } from '../metrics/LDAIMetrics';
|
|
4
|
+
/**
|
|
5
|
+
* Chat response structure.
|
|
6
|
+
*/
|
|
7
|
+
export interface ChatResponse {
|
|
8
|
+
/**
|
|
9
|
+
* The response message from the AI.
|
|
10
|
+
*/
|
|
11
|
+
message: LDMessage;
|
|
12
|
+
/**
|
|
13
|
+
* Metrics information including success status and token usage.
|
|
14
|
+
*/
|
|
15
|
+
metrics: LDAIMetrics;
|
|
16
|
+
/**
|
|
17
|
+
* Promise that resolves to judge evaluation results.
|
|
18
|
+
* Only present when judges are configured for evaluation.
|
|
19
|
+
*/
|
|
20
|
+
evaluations?: Promise<Array<JudgeResponse | undefined>>;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/api/chat/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B;;OAEG;IACH,OAAO,EAAE,SAAS,CAAC;IAEnB;;OAEG;IACH,OAAO,EAAE,WAAW,CAAC;IAErB;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,aAAa,GAAG,SAAS,CAAC,CAAC,CAAC;CACzD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../src/api/chat/types.ts"],"names":[],"mappings":""}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { EvalScore, JudgeResponse } from '../judge/types';
|
|
1
2
|
import { LDAIMetrics, LDFeedbackKind, LDTokenUsage } from '../metrics';
|
|
2
3
|
/**
|
|
3
4
|
* Metrics which have been tracked.
|
|
@@ -30,6 +31,18 @@ export interface LDAIMetricSummary {
|
|
|
30
31
|
* The LDAIConfigTracker is used to track various details about AI operations.
|
|
31
32
|
*/
|
|
32
33
|
export interface LDAIConfigTracker {
|
|
34
|
+
/**
|
|
35
|
+
* Get the data for tracking.
|
|
36
|
+
*/
|
|
37
|
+
getTrackData(): {
|
|
38
|
+
variationKey: string;
|
|
39
|
+
configKey: string;
|
|
40
|
+
version: number;
|
|
41
|
+
modelName: string;
|
|
42
|
+
providerName: string;
|
|
43
|
+
aiSdkName: string;
|
|
44
|
+
aiSdkVersion: string;
|
|
45
|
+
};
|
|
33
46
|
/**
|
|
34
47
|
* Track the duration of generation.
|
|
35
48
|
*
|
|
@@ -66,6 +79,18 @@ export interface LDAIConfigTracker {
|
|
|
66
79
|
* @param timeToFirstTokenMs The duration in milliseconds.
|
|
67
80
|
*/
|
|
68
81
|
trackTimeToFirstToken(timeToFirstTokenMs: number): void;
|
|
82
|
+
/**
|
|
83
|
+
* Track evaluation scores for multiple metrics.
|
|
84
|
+
*
|
|
85
|
+
* @param scores Record mapping metric keys to their evaluation scores
|
|
86
|
+
*/
|
|
87
|
+
trackEvalScores(scores: Record<string, EvalScore>): void;
|
|
88
|
+
/**
|
|
89
|
+
* Track a judge response containing evaluation scores and judge configuration key.
|
|
90
|
+
*
|
|
91
|
+
* @param response Judge response containing evaluation scores and judge configuration key
|
|
92
|
+
*/
|
|
93
|
+
trackJudgeResponse(response: JudgeResponse): void;
|
|
69
94
|
/**
|
|
70
95
|
* Track the duration of execution of the provided function.
|
|
71
96
|
*
|
|
@@ -176,31 +201,6 @@ export interface LDAIConfigTracker {
|
|
|
176
201
|
completionTokens?: number;
|
|
177
202
|
};
|
|
178
203
|
}>(func: () => Promise<TRes>): Promise<TRes>;
|
|
179
|
-
/**
|
|
180
|
-
* Track a Vercel AI SDK streamText operation.
|
|
181
|
-
*
|
|
182
|
-
* This function will track the duration of the operation, the token usage, and the success or error status.
|
|
183
|
-
*
|
|
184
|
-
* If the provided function throws, then this method will also throw.
|
|
185
|
-
* In the case the provided function throws, this function will record the duration and an error.
|
|
186
|
-
* A failed operation will not have any token usage data.
|
|
187
|
-
*
|
|
188
|
-
* @deprecated Use `trackStreamMetricsOf()` with `VercelProvider.createStreamMetricsExtractor()` from the
|
|
189
|
-
* `@launchdarkly/server-sdk-ai-vercel` package instead. This method will be removed in a future version.
|
|
190
|
-
*
|
|
191
|
-
* @param func Function which executes the operation.
|
|
192
|
-
* @returns The result of the operation.
|
|
193
|
-
*/
|
|
194
|
-
trackVercelAISDKStreamTextMetrics<TRes extends {
|
|
195
|
-
finishReason?: Promise<string>;
|
|
196
|
-
usage?: Promise<{
|
|
197
|
-
totalTokens?: number;
|
|
198
|
-
inputTokens?: number;
|
|
199
|
-
promptTokens?: number;
|
|
200
|
-
outputTokens?: number;
|
|
201
|
-
completionTokens?: number;
|
|
202
|
-
}>;
|
|
203
|
-
}>(func: () => TRes): TRes;
|
|
204
204
|
/**
|
|
205
205
|
* Get a summary of the tracked metrics.
|
|
206
206
|
*/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LDAIConfigTracker.d.ts","sourceRoot":"","sources":["../../../../src/api/config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CAAC;KACH,EAED,GAAG,EAAE,IAAI,GACR,IAAI,CAAC;IAER;;;;;;;;;;;OAWG;IACH,mCAAmC,CACjC,IAAI,SAAS;QACX,KAAK,CAAC,EAAE;YACN,WAAW,CAAC,EAAE,MAAM,CAAC;YACrB,WAAW,CAAC,EAAE,MAAM,CAAC;YACrB,YAAY,CAAC,EAAE,MAAM,CAAC;YACtB,YAAY,CAAC,EAAE,MAAM,CAAC;YACtB,gBAAgB,CAAC,EAAE,MAAM,CAAC;SAC3B,CAAC;KACH,EAED,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GACxB,OAAO,CAAC,IAAI,CAAC,CAAC;IAEjB;;OAEG;IACH,UAAU,IAAI,iBAAiB,CAAC;CACjC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LDAIConfigTracker.js","sourceRoot":"","sources":["../../../../src/api/config/LDAIConfigTracker.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LDAIConfigUtils.d.ts","sourceRoot":"","sources":["../../../../src/api/config/LDAIConfigUtils.ts"],"names":[],"mappings":""}
|