Skip to content

Commit 9c4e69f

Browse files
author
Daniel OBrien
committed
openai and bedrock examples
1 parent 9ce1049 commit 9c4e69f

File tree

6 files changed

+272
-0
lines changed

6 files changed

+272
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# LaunchDarkly AI SDK for AWS Bedrock Example
2+
3+
This package demonstrates the integration of LaunchDarkly's AI SDK with AWS Bedrock, allowing you to leverage LaunchDarkly's AI Config capabilities in AI-powered applications using AWS Bedrock.
4+
5+
## Installation
6+
7+
To install the package and its dependencies, run:
8+
9+
```bash
10+
npm install
11+
```
12+
13+
## Configuration
14+
15+
Before running the example, make sure to set the following environment variables:
16+
17+
- `LAUNCHDARKLY_SDK_KEY`: Your LaunchDarkly SDK key
18+
- `LAUNCHDARKLY_AI_CONFIG_KEY`: Your LaunchDarkly AI configuration key (defaults to 'sample-ai-config' if not set)
19+
20+
Additionally, ensure you have proper AWS credentials configured to access Bedrock services.
21+
22+
## Usage
23+
24+
The main script (`index.js`) demonstrates how to:
25+
26+
1. Initialize the LaunchDarkly SDK
27+
2. Set up a user context
28+
3. Initialize the LaunchDarkly AI client
29+
4. Retrieve an AI model configuration
30+
5. Send a prompt to AWS Bedrock
31+
6. Track token usage
32+
33+
To run the example:
34+
35+
```bash
36+
node index.js
37+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const LaunchDarkly = require('@launchdarkly/node-server-sdk');
2+
const LaunchDarklyAI = require('@launchdarkly/ai');
3+
const { OpenAITokenUsage } = require('@launchdarkly/ai'); // Adjusted require
4+
const { BedrockRuntimeClient, ConverseCommand } = require("@aws-sdk/client-bedrock-runtime");
5+
6+
const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY;
7+
const aiConfigKey = process.env.LAUNCHDARKLY_AI_CONFIG_KEY || 'sample-ai-config';
8+
const ci = process.env.CI;
9+
const awsClient = new BedrockRuntimeClient({ region: "us-east-1" });
10+
11+
if (!sdkKey) {
12+
console.error("*** Please set the LAUNCHDARKLY_SDK_KEY env first");
13+
process.exit(1);
14+
}
15+
if (!aiConfigKey) {
16+
console.error("*** Please set the LAUNCHDARKLY_AI_CONFIG_KEY env first");
17+
process.exit(1);
18+
}
19+
20+
const ldClient = LaunchDarkly.init(sdkKey);
21+
22+
// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard
23+
// soon after you run the demo.
24+
const context = {
25+
kind: 'user',
26+
key: 'example-user-key',
27+
name: 'Sandy',
28+
};
29+
30+
console.log("*** SDK successfully initialized");
31+
32+
async function main() {
33+
let tracker;
34+
let configValue;
35+
try {
36+
await ldClient.waitForInitialization({timeout: 10});
37+
const aiClient = LaunchDarklyAI.init(ldClient);
38+
39+
configValue = await aiClient.modelConfig(aiConfigKey, context, false, { myVariable: "My User Defined Variable" });
40+
tracker = configValue.tracker;
41+
42+
43+
44+
45+
} catch (error) {
46+
console.log(`*** SDK failed to initialize: ${error}`);
47+
process.exit(1);
48+
}
49+
const conversation = [
50+
{
51+
role: "user",
52+
content: [{ text: "Hello, how are you?" }],
53+
},
54+
];
55+
56+
const completion = await tracker.trackBedrockConverse(await awsClient.send(
57+
new ConverseCommand({modelId: configValue.config.config.modelId, messages: mapPromptToConversation(configValue.config.prompt)})
58+
));
59+
console.log("AI Response:", completion.output.message.content[0].text);
60+
console.log("Success.");
61+
}
62+
63+
function mapPromptToConversation(prompt) {
64+
return prompt.map(item => ({
65+
role: item.role,
66+
content: [{ text: item.content }]
67+
}));
68+
}
69+
70+
main();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "@launchdarkly/hello-ai-bedrock",
3+
"version": "0.1.0",
4+
"description": "LaunchDarkly AI SDK for Node.js",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"scripts": {
8+
"build": "tsc",
9+
"test": "jest",
10+
"lint": "eslint . --ext .ts"
11+
},
12+
"keywords": [
13+
"launchdarkly",
14+
"ai",
15+
"llm"
16+
],
17+
"author": "LaunchDarkly",
18+
"license": "Apache-2.0",
19+
"dependencies": {
20+
"@aws-sdk/client-bedrock-runtime": "^3.679.0",
21+
"@launchdarkly/ai": "0.1.0"
22+
},
23+
"devDependencies": {
24+
"eslint": "^8.45.0"
25+
},
26+
"directories": {
27+
"example": "example"
28+
},
29+
"repository": {
30+
"type": "git",
31+
"url": "github.com/launchdarkly/js-core"
32+
}
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# LaunchDarkly AI SDK for OpenAI Example
2+
3+
This package demonstrates the integration of LaunchDarkly's AI SDK with OpenAI, allowing you to leverage LaunchDarkly's AI Config capabilities in AI-powered applications using OpenAI's services.
4+
5+
## Installation
6+
7+
To install the package and its dependencies, run:
8+
9+
```bash
10+
npm install
11+
```
12+
13+
## Configuration
14+
15+
Before running the example, make sure to set the following environment variables:
16+
17+
- `LAUNCHDARKLY_SDK_KEY`: Your LaunchDarkly SDK key
18+
- `LAUNCHDARKLY_AI_CONFIG_KEY`: Your LaunchDarkly AI configuration key (defaults to 'sample-ai-config' if not set)
19+
- `OPENAI_API_KEY`: Your OpenAI API key
20+
21+
## Usage
22+
23+
The main script (`index.js`) demonstrates how to:
24+
25+
1. Initialize the LaunchDarkly SDK
26+
2. Set up a user context
27+
3. Initialize the LaunchDarkly AI client
28+
4. Retrieve an AI model configuration
29+
5. Send a prompt to OpenAI
30+
6. Track token usage
31+
32+
To run the example:
33+
34+
```bash
35+
node index.js
36+
```
37+
38+
## Note
39+
40+
This example uses OpenAI's chat completions API. Make sure your LaunchDarkly AI configuration is set up correctly to work with OpenAI's models and API structure.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const LaunchDarkly = require('@launchdarkly/node-server-sdk');
2+
const LaunchDarklyAI = require('@launchdarkly/ai');
3+
const { OpenAITokenUsage } = require('@launchdarkly/ai'); // Adjusted require
4+
const OpenAI = require('openai');
5+
6+
const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY;
7+
const aiConfigKey = process.env.LAUNCHDARKLY_AI_CONFIG_KEY || 'sample-ai-config';
8+
const ci = process.env.CI;
9+
10+
const client = new OpenAI({
11+
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
12+
});
13+
14+
if (!sdkKey) {
15+
console.error("*** Please set the LAUNCHDARKLY_SDK_KEY env first");
16+
process.exit(1);
17+
}
18+
if (!aiConfigKey) {
19+
console.error("*** Please set the LAUNCHDARKLY_AI_CONFIG_KEY env first");
20+
process.exit(1);
21+
}
22+
23+
const ldClient = LaunchDarkly.init(sdkKey);
24+
25+
// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard
26+
// soon after you run the demo.
27+
const context = {
28+
kind: 'user',
29+
key: 'example-user-key',
30+
name: 'Sandy',
31+
};
32+
33+
console.log("*** SDK successfully initialized");
34+
35+
async function main() {
36+
try {
37+
await ldClient.waitForInitialization({timeout: 10});
38+
const aiClient = LaunchDarklyAI.init(ldClient);
39+
40+
const configValue = await aiClient.modelConfig(aiConfigKey, context, false, { myVariable: "My User Defined Variable" });
41+
const tracker = configValue.tracker;
42+
const completion = await tracker.trackOpenAI(async () => {
43+
return await client.chat.completions.create({
44+
messages: configValue.config.prompt,
45+
model: configValue.config.config.modelId,
46+
});
47+
});
48+
49+
console.log("AI Response:", completion.choices[0].message.content);
50+
console.log("Success.");
51+
52+
} catch (error) {
53+
console.log(`*** SDK failed to initialize: ${error}`);
54+
process.exit(1);
55+
}
56+
57+
}
58+
59+
main();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "@launchdarkly/hello-ai",
3+
"version": "0.1.0",
4+
"description": "LaunchDarkly AI SDK for Node.js",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
7+
"scripts": {
8+
"build": "tsc",
9+
"test": "jest",
10+
"lint": "eslint . --ext .ts"
11+
},
12+
"keywords": [
13+
"launchdarkly",
14+
"ai",
15+
"llm"
16+
],
17+
"author": "LaunchDarkly",
18+
"license": "Apache-2.0",
19+
"dependencies": {
20+
"@launchdarkly/ai": "0.1.0",
21+
"openai": "^4.58.1"
22+
},
23+
"devDependencies": {
24+
"eslint": "^8.45.0"
25+
},
26+
"directories": {
27+
"example": "example"
28+
},
29+
"repository": {
30+
"type": "git",
31+
"url": "github.com/launchdarkly/js-core"
32+
}
33+
}

0 commit comments

Comments
 (0)