Skip to content

Commit ba26071

Browse files
authored
chore: Add example for TrackChat from the AI SDK (#937)
1 parent 65f8bcc commit ba26071

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"packages/sdk/server-ai",
3737
"packages/sdk/server-ai/examples/bedrock",
3838
"packages/sdk/server-ai/examples/openai",
39+
"packages/sdk/server-ai/examples/tracked-chat",
3940
"packages/sdk/server-ai/examples/vercel-ai",
4041
"packages/telemetry/browser-telemetry",
4142
"contract-tests",
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Tracked Chat Example
2+
3+
This example demonstrates how to use the LaunchDarkly AI SDK chat functionality with multiple providers for tracked chat interactions.
4+
5+
## Prerequisites
6+
7+
1. A LaunchDarkly account and SDK key
8+
1. An OpenAI API key (for the AI provider)
9+
1. Node.js 16 or later
10+
11+
## Setup
12+
13+
1. Install dependencies:
14+
```bash
15+
yarn install
16+
```
17+
18+
1. Set up environment variables:
19+
```bash
20+
cp .env.example .env
21+
```
22+
23+
Edit `.env` and add your keys:
24+
```
25+
LAUNCHDARKLY_SDK_KEY=your-sdk-key-here
26+
OPENAI_API_KEY=your-openai-api-key-here
27+
LAUNCHDARKLY_AI_CONFIG_KEY=sample-ai-chat-config
28+
```
29+
30+
1. Create an AI Config in LaunchDarkly:
31+
- Navigate to the AI Configs section in your LaunchDarkly dashboard
32+
- Create a new AI Config with the key `sample-ai-config`
33+
- Add a variation with the following settings:
34+
- **Model Selection**: Select "OpenAI" as the provider and "gpt-3.5-turbo" as the model
35+
- **Messages**: Add a system message with the content: "You are a helpful assistant for {{companyName}}. You should be friendly and informative."
36+
- Save the variation
37+
- Update the default target rule to use the newly created variation
38+
39+
## Running the Example
40+
41+
```bash
42+
yarn start
43+
```
44+
45+
This will:
46+
1. Initialize the LaunchDarkly client
47+
1. Create a chat configuration using the AI Config
48+
1. Send a message to the AI and display the response
49+
1. Automatically track interaction metrics (duration, tokens, success/error)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "tracked-chat-example",
3+
"version": "1.0.0",
4+
"description": "Example demonstrating LaunchDarkly AI SDK chat functionality with multiple providers",
5+
"type": "module",
6+
"scripts": {
7+
"build": "tsc",
8+
"start": "yarn build && node ./dist/index.js"
9+
},
10+
"dependencies": {
11+
"@ai-sdk/google": "^2.0.20",
12+
"@langchain/core": "^0.3.78",
13+
"@langchain/google-genai": "^0.2.18",
14+
"@launchdarkly/node-server-sdk": "^9.0.0",
15+
"@launchdarkly/server-sdk-ai": "0.12.1",
16+
"@launchdarkly/server-sdk-ai-langchain": "^0.1.0",
17+
"@launchdarkly/server-sdk-ai-openai": "^0.1.0",
18+
"@launchdarkly/server-sdk-ai-vercel": "^0.1.0",
19+
"dotenv": "^16.0.0",
20+
"langchain": "^0.1.0"
21+
},
22+
"devDependencies": {
23+
"@types/node": "^20.0.0",
24+
"tsx": "^4.0.0",
25+
"typescript": "^5.0.0"
26+
}
27+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* eslint-disable no-console */
2+
import { init, type LDContext } from '@launchdarkly/node-server-sdk';
3+
import { initAi } from '@launchdarkly/server-sdk-ai';
4+
5+
// Environment variables
6+
const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY;
7+
const aiConfigKey = process.env.LAUNCHDARKLY_AI_CONFIG_KEY || 'sample-ai-config';
8+
9+
// Validate required environment variables
10+
if (!sdkKey) {
11+
console.error('*** Please set the LAUNCHDARKLY_SDK_KEY env first');
12+
process.exit(1);
13+
}
14+
15+
// Initialize LaunchDarkly client
16+
const ldClient = init(sdkKey);
17+
18+
// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard
19+
// soon after you run the demo.
20+
const context: LDContext = {
21+
kind: 'user',
22+
key: 'example-user-key',
23+
name: 'Sandy',
24+
};
25+
26+
async function main(): Promise<void> {
27+
try {
28+
await ldClient.waitForInitialization({ timeout: 10 });
29+
console.log('*** SDK successfully initialized');
30+
} catch (error) {
31+
console.log(`*** SDK failed to initialize: ${error}`);
32+
process.exit(1);
33+
}
34+
35+
const aiClient = initAi(ldClient);
36+
const defaultValue = {
37+
enabled: true,
38+
model: { name: 'gpt-3.5-turbo' },
39+
messages: [{ role: 'system' as const, content: 'You are a helpful assistant.' }],
40+
provider: { name: 'openai' },
41+
};
42+
43+
// You provide a disabled default value
44+
// const defaultValue = {
45+
// enabled: false,
46+
// };
47+
48+
// Get AI chat configuration from LaunchDarkly
49+
const chat = await aiClient.initChat(aiConfigKey, context, defaultValue, {
50+
companyName: 'LaunchDarkly',
51+
});
52+
53+
if (!chat) {
54+
console.log('*** AI chat configuration is not enabled');
55+
process.exit(0);
56+
}
57+
58+
// Example of using the chat functionality
59+
console.log('\n*** Starting chat conversation:');
60+
try {
61+
const userInput = 'Hello! Can you help me understand how your company can help me?';
62+
console.log('User Input:', userInput);
63+
64+
const response = await chat.invoke(userInput);
65+
66+
console.log('AI Response:', response.message.content);
67+
68+
console.log('Success.');
69+
} catch (err) {
70+
console.error('Error:', err);
71+
}
72+
}
73+
74+
main();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2022",
4+
"module": "ESNext",
5+
"moduleResolution": "node",
6+
"esModuleInterop": true,
7+
"allowSyntheticDefaultImports": true,
8+
"strict": true,
9+
"skipLibCheck": true,
10+
"forceConsistentCasingInFileNames": true,
11+
"outDir": "./dist",
12+
"rootDir": "./src",
13+
"declaration": true,
14+
"sourceMap": true
15+
},
16+
"include": ["src/**/*"],
17+
"exclude": ["node_modules", "dist"]
18+
}

release-please-config.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@
8686
"type": "json",
8787
"path": "examples/openai/package.json",
8888
"jsonpath": "$.dependencies['@launchdarkly/server-sdk-ai']"
89+
},
90+
{
91+
"type": "json",
92+
"path": "examples/tracked-chat/package.json",
93+
"jsonpath": "$.dependencies['@launchdarkly/server-sdk-ai']"
94+
},
95+
{
96+
"type": "json",
97+
"path": "examples/vercel-ai/package.json",
98+
"jsonpath": "$.dependencies['@launchdarkly/server-sdk-ai']"
8999
}
90100
]
91101
},

0 commit comments

Comments
 (0)