Skip to content

Commit 17d595a

Browse files
authored
fix: Improve documentation for AI SDK and AIProvider (#958)
1 parent 686ddd1 commit 17d595a

File tree

5 files changed

+252
-8
lines changed

5 files changed

+252
-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: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
# or
31+
yarn add @launchdarkly/server-sdk-ai @launchdarkly/server-sdk-ai-langchain
32+
```
33+
34+
2. Create a chat session and use it:
35+
36+
```typescript
37+
import { init } from '@launchdarkly/node-server-sdk';
38+
import { initAi } from '@launchdarkly/server-sdk-ai';
39+
40+
// Initialize LaunchDarkly client
41+
const ldClient = init(sdkKey);
42+
const aiClient = initAi(ldClient);
43+
44+
// Create a chat session
45+
const defaultConfig = {
46+
enabled: true,
47+
model: { name: 'gpt-4' },
48+
provider: { name: 'openai' }
49+
};
50+
const chat = await aiClient.initChat('my-chat-config', context, defaultConfig);
51+
52+
if (chat) {
53+
const response = await chat.invoke('What is the capital of France?');
54+
console.log(response.message.content);
55+
}
56+
```
57+
58+
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).
59+
60+
## Advanced Usage
61+
62+
For more control, you can use the LangChain provider package directly with LaunchDarkly configurations:
63+
64+
```typescript
65+
import { LangChainProvider } from '@launchdarkly/server-sdk-ai-langchain';
66+
import { HumanMessage } from '@langchain/core/messages';
67+
68+
// Create a LangChain model from LaunchDarkly configuration
69+
const llm = await LangChainProvider.createLangChainModel(aiConfig);
70+
71+
// Convert LaunchDarkly messages to LangChain format and add user message
72+
const configMessages = aiConfig.messages || [];
73+
const userMessage = new HumanMessage('What is the capital of France?');
74+
const allMessages = [...LangChainProvider.convertMessagesToLangChain(configMessages), userMessage];
75+
76+
// Track the model call with LaunchDarkly tracking
77+
const response = await aiConfig.tracker.trackMetricsOf(
78+
(result) => LangChainProvider.createAIMetrics(result),
79+
() => llm.invoke(allMessages)
80+
);
81+
82+
console.log('AI Response:', response.content);
83+
```
84+
85+
86+
## Contributing
87+
88+
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.
89+
90+
## About LaunchDarkly
91+
92+
- 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:
93+
- 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.
94+
- 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?).
95+
- 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.
96+
- 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).
97+
- Disable parts of your application to facilitate maintenance, without taking everything offline.
98+
- 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.
99+
- Explore LaunchDarkly
100+
- [launchdarkly.com](https://www.launchdarkly.com/ 'LaunchDarkly Main Website') for more information
101+
- [docs.launchdarkly.com](https://docs.launchdarkly.com/ 'LaunchDarkly Documentation') for our documentation and SDK reference guides
102+
- [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ 'LaunchDarkly API Documentation') for our API documentation
103+
- [blog.launchdarkly.com](https://blog.launchdarkly.com/ 'LaunchDarkly Blog Documentation') for the latest product updates
104+
105+
[server-ai-langchain-ci-badge]: https://github.com/launchdarkly/js-core/actions/workflows/server-ai-langchain.yml/badge.svg
106+
[server-ai-langchain-ci]: https://github.com/launchdarkly/js-core/actions/workflows/server-ai-langchain.yml
107+
[server-ai-langchain-npm-badge]: https://img.shields.io/npm/v/@launchdarkly/server-sdk-ai-langchain.svg?style=flat-square
108+
[server-ai-langchain-npm-link]: https://www.npmjs.com/package/@launchdarkly/server-sdk-ai-langchain
109+
[server-ai-langchain-ghp-badge]: https://img.shields.io/static/v1?label=GitHub+Pages&message=API+reference&color=00add8
110+
[server-ai-langchain-ghp-link]: https://launchdarkly.github.io/js-core/packages/ai-providers/server-ai-langchain/docs/
111+
[server-ai-langchain-dm-badge]: https://img.shields.io/npm/dm/@launchdarkly/server-sdk-ai-langchain.svg?style=flat-square
112+
[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: 128 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ This assumes that you have already installed the LaunchDarkly Node.js (server-si
2727

2828
```shell
2929
npm install @launchdarkly/server-sdk-ai --save
30+
# or
31+
yarn add @launchdarkly/server-sdk-ai
3032
```
3133

3234
2. Create an AI SDK instance:
@@ -36,18 +38,138 @@ npm install @launchdarkly/server-sdk-ai --save
3638
const aiClient = initAi(ldClient);
3739
```
3840

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

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

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

52174
## Contributing
53175

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)