Skip to content

Commit 4b13442

Browse files
committed
fix: Improve documentation for AI SDK and AIProvider
1 parent 686ddd1 commit 4b13442

File tree

5 files changed

+248
-8
lines changed

5 files changed

+248
-8
lines changed

.github/workflows/manual-publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ on:
3535
- packages/tooling/jest
3636
- packages/sdk/browser
3737
- packages/sdk/server-ai
38+
- packages/ai-providers/server-ai-langchain
3839
- packages/telemetry/browser-telemetry
3940
- packages/sdk/combined-browser
4041
prerelease:

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ This includes shared libraries, used by SDKs and other tools, as well as SDKs.
3535
| [@launchdarkly/node-server-sdk-otel](packages/telemetry/node-server-sdk-otel/README.md) | [![NPM][node-otel-npm-badge]][node-otel-npm-link] | [Node OTel][node-otel-issues] | [![Actions Status][node-otel-ci-badge]][node-otel-ci] |
3636
| [@launchdarkly/browser-telemetry](packages/telemetry/browser-telemetry/README.md) | [![NPM][browser-telemetry-npm-badge]][browser-telemetry-npm-link] | [Browser Telemetry][browser-telemetry-issues] | [![Actions Status][browser-telemetry-ci-badge]][browser-telemetry-ci] |
3737

38+
| AI Providers | npm | issues | tests |
39+
| ------------------------------------------------------------------------------------------ | ------------------------------------------------------------- | ------------------------------------------- | ------------------------------------------------------------------- |
40+
| [@launchdarkly/server-sdk-ai-langchain](packages/ai-providers/server-ai-langchain/README.md) | [![NPM][server-ai-langchain-npm-badge]][server-ai-langchain-npm-link] | [server-ai-langchain][package-ai-providers-server-ai-langchain-issues] | [![Actions Status][server-ai-langchain-ci-badge]][server-ai-langchain-ci] |
41+
3842
## Organization
3943

4044
`packages` Top level directory containing package implementations.
@@ -219,4 +223,10 @@ We encourage pull requests and other contributions from the community. Check out
219223
[sdk-combined-browser-ghp-link]: https://launchdarkly.github.io/js-core/packages/sdk/combined-browser/docs/
220224
[sdk-combined-browser-dm-badge]: https://img.shields.io/npm/dm/@launchdarkly/browser.svg?style=flat-square
221225
[sdk-combined-browser-dt-badge]: https://img.shields.io/npm/dt/@launchdarkly/browser.svg?style=flat-square
222-
[package-sdk-browser-issues]: https://github.com/launchdarkly/js-core/issues?q=is%3Aissue+is%3Aopen+label%3A%22package%3A+sdk%2Fcombined-browser%22+
226+
[package-sdk-browser-issues]: https://github.com/launchdarkly/js-core/issues?q=is%3Aissue+is%3Aopen+label%3A%22package%3A+sdk%2Fcombined-browser%22+
227+
[//]: # 'ai-providers/server-ai-langchain'
228+
[server-ai-langchain-ci-badge]: https://github.com/launchdarkly/js-core/actions/workflows/server-ai-langchain.yml/badge.svg
229+
[server-ai-langchain-ci]: https://github.com/launchdarkly/js-core/actions/workflows/server-ai-langchain.yml
230+
[server-ai-langchain-npm-badge]: https://img.shields.io/npm/v/@launchdarkly/server-sdk-ai-langchain.svg?style=flat-square
231+
[server-ai-langchain-npm-link]: https://www.npmjs.com/package/@launchdarkly/server-sdk-ai-langchain
232+
[package-ai-providers-server-ai-langchain-issues]: https://github.com/launchdarkly/js-core/issues?q=is%3Aissue+is%3Aopen+label%3A%22package%3A+ai-providers%2Fserver-ai-langchain%22+
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# LaunchDarkly AI SDK LangChain Provider for Server-Side JavaScript
2+
3+
[![NPM][server-ai-langchain-npm-badge]][server-ai-langchain-npm-link]
4+
[![Actions Status][server-ai-langchain-ci-badge]][server-ai-langchain-ci]
5+
[![Documentation][server-ai-langchain-ghp-badge]][server-ai-langchain-ghp-link]
6+
[![NPM][server-ai-langchain-dm-badge]][server-ai-langchain-npm-link]
7+
[![NPM][server-ai-langchain-dt-badge]][server-ai-langchain-npm-link]
8+
9+
# ⛔️⛔️⛔️⛔️
10+
11+
> [!CAUTION]
12+
> This library is a alpha version and should not be considered ready for production use while this message is visible.
13+
14+
# ☝️☝️☝️☝️☝️☝️
15+
16+
## LaunchDarkly overview
17+
18+
[LaunchDarkly](https://www.launchdarkly.com) is a feature management platform that serves over 100 billion feature flags daily to help teams build better software, faster. [Get started](https://docs.launchdarkly.com/home/getting-started) using LaunchDarkly today!
19+
20+
[![Twitter Follow](https://img.shields.io/twitter/follow/launchdarkly.svg?style=social&label=Follow&maxAge=2592000)](https://twitter.com/intent/follow?screen_name=launchdarkly)
21+
22+
## Quick Setup
23+
24+
This package provides LangChain integration for the LaunchDarkly AI SDK. The simplest way to use it is with the LaunchDarkly AI SDK's `initChat` method:
25+
26+
1. Install the required packages:
27+
28+
```shell
29+
npm install @launchdarkly/server-sdk-ai @launchdarkly/server-sdk-ai-langchain --save
30+
```
31+
32+
2. Create a chat session and use it:
33+
34+
```typescript
35+
import { init } from '@launchdarkly/node-server-sdk';
36+
import { initAi } from '@launchdarkly/server-sdk-ai';
37+
38+
// Initialize LaunchDarkly client
39+
const ldClient = init(sdkKey);
40+
const aiClient = initAi(ldClient);
41+
42+
// Create a chat session
43+
const defaultConfig = {
44+
enabled: true,
45+
model: { name: 'gpt-4' },
46+
provider: { name: 'openai' }
47+
};
48+
const chat = await aiClient.initChat('my-chat-config', context, defaultConfig);
49+
50+
if (chat) {
51+
const response = await chat.invoke("What is the capital of France?");
52+
console.log(response.message.content);
53+
}
54+
```
55+
56+
For more information about using the LaunchDarkly AI SDK, see the [LaunchDarkly AI SDK documentation](https://github.com/launchdarkly/js-core/tree/main/packages/sdk/server-ai/README.md).
57+
58+
## Advanced Usage
59+
60+
For more control, you can use the LangChain provider package directly with LaunchDarkly configurations:
61+
62+
```typescript
63+
import { LangChainProvider } from '@launchdarkly/server-sdk-ai-langchain';
64+
import { HumanMessage } from '@langchain/core/messages';
65+
66+
// Create a LangChain model from LaunchDarkly configuration
67+
const llm = await LangChainProvider.createLangChainModel(aiConfig);
68+
69+
// Convert LaunchDarkly messages to LangChain format and add user message
70+
const configMessages = aiConfig.messages || [];
71+
const userMessage = new HumanMessage("What is the capital of France?");
72+
const allMessages = [...LangChainProvider.convertMessagesToLangChain(configMessages), userMessage];
73+
74+
// Track the model call with LaunchDarkly tracking
75+
const response = await aiConfig.tracker.trackMetricsOf(
76+
(result) => LangChainProvider.createAIMetrics(result),
77+
() => llm.invoke(allMessages)
78+
);
79+
80+
console.log('AI Response:', response.content);
81+
```
82+
83+
84+
## Contributing
85+
86+
We encourage pull requests and other contributions from the community. Check out our [contributing guidelines](CONTRIBUTING.md) for instructions on how to contribute to this SDK.
87+
88+
## About LaunchDarkly
89+
90+
- LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can:
91+
- Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases.
92+
- Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?).
93+
- Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file.
94+
- Grant access to certain features based on user attributes, like payment plan (eg: users on the 'gold' plan get access to more features than users in the 'silver' plan).
95+
- Disable parts of your application to facilitate maintenance, without taking everything offline.
96+
- LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Check out [our documentation](https://docs.launchdarkly.com/sdk) for a complete list.
97+
- Explore LaunchDarkly
98+
- [launchdarkly.com](https://www.launchdarkly.com/ 'LaunchDarkly Main Website') for more information
99+
- [docs.launchdarkly.com](https://docs.launchdarkly.com/ 'LaunchDarkly Documentation') for our documentation and SDK reference guides
100+
- [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ 'LaunchDarkly API Documentation') for our API documentation
101+
- [blog.launchdarkly.com](https://blog.launchdarkly.com/ 'LaunchDarkly Blog Documentation') for the latest product updates
102+
103+
[server-ai-langchain-ci-badge]: https://github.com/launchdarkly/js-core/actions/workflows/server-ai-langchain.yml/badge.svg
104+
[server-ai-langchain-ci]: https://github.com/launchdarkly/js-core/actions/workflows/server-ai-langchain.yml
105+
[server-ai-langchain-npm-badge]: https://img.shields.io/npm/v/@launchdarkly/server-sdk-ai-langchain.svg?style=flat-square
106+
[server-ai-langchain-npm-link]: https://www.npmjs.com/package/@launchdarkly/server-sdk-ai-langchain
107+
[server-ai-langchain-ghp-badge]: https://img.shields.io/static/v1?label=GitHub+Pages&message=API+reference&color=00add8
108+
[server-ai-langchain-ghp-link]: https://launchdarkly.github.io/js-core/packages/ai-providers/server-ai-langchain/docs/
109+
[server-ai-langchain-dm-badge]: https://img.shields.io/npm/dm/@launchdarkly/server-sdk-ai-langchain.svg?style=flat-square
110+
[server-ai-langchain-dt-badge]: https://img.shields.io/npm/dt/@launchdarkly/server-sdk-ai-langchain.svg?style=flat-square

packages/sdk/server-ai/README.md

Lines changed: 126 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,138 @@ npm install @launchdarkly/server-sdk-ai --save
3636
const aiClient = initAi(ldClient);
3737
```
3838

39-
3. Evaluate a model configuration:
39+
## Setting Default AI Configurations
40+
41+
When retrieving AI configurations, you need to provide default values that will be used if the configuration is not available from LaunchDarkly:
42+
43+
### Fully Configured Default
44+
45+
```typescript
46+
const defaultConfig = {
47+
enabled: true,
48+
model: {
49+
name: 'gpt-4',
50+
parameters: { temperature: 0.7, maxTokens: 1000 }
51+
},
52+
messages: [
53+
{ role: 'system', content: 'You are a helpful assistant.' }
54+
]
55+
};
56+
```
57+
58+
### Disabled Default
59+
60+
```typescript
61+
const defaultConfig = {
62+
enabled: false
63+
};
64+
```
65+
66+
## Retrieving AI Configurations
67+
68+
The `config` method retrieves AI configurations from LaunchDarkly with support for dynamic variables and fallback values:
69+
70+
```typescript
71+
const aiConfig = await aiClient.config(
72+
aiConfigKey,
73+
context,
74+
defaultConfig,
75+
{ myVariable: 'My User Defined Variable' } // Variables for template interpolation
76+
);
77+
78+
// Ensure configuration is enabled
79+
if (aiConfig.enabled) {
80+
const { messages, model, tracker } = aiConfig;
81+
// Use with your AI provider
82+
}
83+
```
84+
85+
## TrackedChat for Conversational AI
86+
87+
`TrackedChat` provides a high-level interface for conversational AI with automatic conversation management and metrics tracking:
88+
89+
- Automatically configures models based on AI configuration
90+
- Maintains conversation history across multiple interactions
91+
- Automatically tracks token usage, latency, and success rates
92+
- Works with any supported AI provider (see [AI Providers](https://github.com/launchdarkly/js-core#ai-providers) for available packages)
93+
94+
### Using TrackedChat
4095

4196
```typescript
42-
const config = await aiClient.config(
43-
aiConfigKey!,
97+
// Use the same defaultConfig from the retrieval section above
98+
const chat = await aiClient.initChat(
99+
'customer-support-chat',
44100
context,
45-
{ enabled: false },
46-
{ myVariable: 'My User Defined Variable' },
101+
defaultConfig,
102+
{ customerName: 'John' }
47103
);
104+
105+
if (chat) {
106+
// Simple conversation flow - metrics are automatically tracked by invoke()
107+
const response1 = await chat.invoke("I need help with my order");
108+
console.log(response1.message.content);
109+
110+
const response2 = await chat.invoke("What's the status?");
111+
console.log(response2.message.content);
112+
113+
// Access conversation history
114+
const messages = chat.getMessages();
115+
console.log(`Conversation has ${messages.length} messages`);
116+
}
48117
```
49118

50-
For an example of how to use the config please refer to the examples folder.
119+
## Advanced Usage with Providers
120+
121+
For more control, you can use the configuration directly with AI providers. We recommend using [LaunchDarkly AI Provider packages](https://github.com/launchdarkly/js-core#ai-providers) when available:
122+
123+
### Using AI Provider Packages
124+
125+
```typescript
126+
import { LangChainProvider } from '@launchdarkly/server-sdk-ai-langchain';
127+
128+
const aiConfig = await aiClient.config(aiConfigKey, context, defaultValue);
129+
130+
// Create LangChain model from configuration
131+
const llm = await LangChainProvider.createLangChainModel(aiConfig);
132+
133+
// Use with tracking
134+
const response = await aiConfig.tracker.trackMetricsOf(
135+
(result) => LangChainProvider.createAIMetrics(result),
136+
() => llm.invoke(messages)
137+
);
138+
139+
console.log('AI Response:', response.content);
140+
```
141+
142+
### Using Custom Providers
143+
144+
```typescript
145+
import { LDAIMetrics } from '@launchdarkly/server-sdk-ai';
146+
147+
const aiConfig = await aiClient.config(aiConfigKey, context, defaultValue);
148+
149+
// Define custom metrics mapping for your provider
150+
const mapCustomProviderMetrics = (response: any): LDAIMetrics => ({
151+
success: true,
152+
usage: {
153+
total: response.usage?.total_tokens || 0,
154+
input: response.usage?.prompt_tokens || 0,
155+
output: response.usage?.completion_tokens || 0,
156+
}
157+
});
158+
159+
// Use with custom provider and tracking
160+
const result = await aiConfig.tracker.trackMetricsOf(
161+
mapCustomProviderMetrics,
162+
() => customProvider.generate({
163+
messages: aiConfig.messages || [],
164+
model: aiConfig.model?.name || 'custom-model',
165+
temperature: aiConfig.model?.parameters?.temperature ?? 0.5,
166+
})
167+
);
168+
169+
console.log('AI Response:', result.content);
170+
```
51171

52172
## Contributing
53173

release-please-config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"packages": {
33
"packages/ai-providers/server-ai-langchain": {
44
"bump-minor-pre-major": true,
5-
"release-as": "0.1.0",
65
"prerelease": true
76
},
87
"packages/shared/common": {},

0 commit comments

Comments
 (0)