Skip to content

Commit 8eb4ad3

Browse files
committed
Convert openai example to typescript.
1 parent 96185fb commit 8eb4ad3

File tree

7 files changed

+125
-68
lines changed

7 files changed

+125
-68
lines changed

packages/sdk/ai/examples/bedrock/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"type": "commonjs",
88
"scripts": {
99
"build": "tsc",
10-
"test": "jest",
1110
"lint": "eslint . --ext .ts"
1211
},
1312
"keywords": [

packages/sdk/ai/examples/bedrock/src/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ import { initAi, LDAIConfig } from '@launchdarkly/ai';
55
import { init } from '@launchdarkly/node-server-sdk';
66

77
const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY;
8-
const aiConfigKey = process.env.LAUNCHDARKLY_AI_CONFIG_KEY || 'sample-ai-config';
8+
const aiConfigKey = process.env.LAUNCHDARKLY_AI_CONFIG_KEY;
99
const awsClient = new BedrockRuntimeClient({ region: 'us-east-1' });
1010

1111
if (!sdkKey) {
1212
console.error('*** Please set the LAUNCHDARKLY_SDK_KEY env first');
1313
process.exit(1);
1414
}
1515

16+
if (!aiConfigKey) {
17+
console.error('*** Please set the LAUNCHDARKLY_AI_CONFIG_KEY env first');
18+
process.exit(1);
19+
}
20+
1621
const ldClient = init(sdkKey);
1722

1823
// Set up the context properties
@@ -22,8 +27,6 @@ const context = {
2227
name: 'Sandy',
2328
};
2429

25-
console.log('*** SDK successfully initialized');
26-
2730
function mapPromptToConversation(
2831
prompt: { role: 'user' | 'assistant' | 'system'; content: string }[],
2932
): Message[] {
@@ -40,10 +43,11 @@ async function main() {
4043

4144
try {
4245
await ldClient.waitForInitialization({ timeout: 10 });
46+
console.log('*** SDK successfully initialized');
4347
const aiClient = initAi(ldClient);
4448

4549
configValue = await aiClient.modelConfig(
46-
aiConfigKey,
50+
aiConfigKey!,
4751
context,
4852
{
4953
model: {
@@ -64,8 +68,8 @@ async function main() {
6468
const completion = tracker.trackBedrockConverse(
6569
await awsClient.send(
6670
new ConverseCommand({
67-
modelId: configValue.config?.model?.modelId ?? 'no-model',
68-
messages: mapPromptToConversation(configValue.config?.prompt ?? []),
71+
modelId: configValue.config.model?.modelId ?? 'no-model',
72+
messages: mapPromptToConversation(configValue.config.prompt ?? []),
6973
}),
7074
),
7175
);

packages/sdk/ai/examples/openai/index.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

packages/sdk/ai/examples/openai/package.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"types": "dist/index.d.ts",
77
"scripts": {
88
"build": "tsc",
9-
"test": "jest",
109
"lint": "eslint . --ext .ts"
1110
},
1211
"keywords": [
@@ -18,10 +17,26 @@
1817
"license": "Apache-2.0",
1918
"dependencies": {
2019
"@launchdarkly/ai": "0.1.0",
20+
"@launchdarkly/node-server-sdk": "9.7.0",
2121
"openai": "^4.58.1"
2222
},
2323
"devDependencies": {
24-
"eslint": "^8.45.0"
24+
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
25+
"@tsconfig/node20": "20.1.4",
26+
"@typescript-eslint/eslint-plugin": "^6.20.0",
27+
"@typescript-eslint/parser": "^6.20.0",
28+
"eslint": "^8.45.0",
29+
"eslint-config-airbnb-base": "^15.0.0",
30+
"eslint-config-airbnb-typescript": "^17.1.0",
31+
"eslint-config-prettier": "^8.8.0",
32+
"eslint-plugin-import": "^2.27.5",
33+
"eslint-plugin-jest": "^27.6.3",
34+
"eslint-plugin-prettier": "^5.0.0",
35+
"jest": "^29.7.0",
36+
"prettier": "^3.0.0",
37+
"rimraf": "^5.0.5",
38+
"typedoc": "0.25.0",
39+
"typescript": "^5.5.3"
2540
},
2641
"directories": {
2742
"example": "example"
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* eslint-disable no-console */
2+
import { OpenAI } from 'openai';
3+
4+
import { initAi } from '@launchdarkly/ai';
5+
import { init, LDContext } from '@launchdarkly/node-server-sdk';
6+
7+
// Environment variables
8+
const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY;
9+
const aiConfigKey = process.env.LAUNCHDARKLY_AI_CONFIG_KEY || 'sample-ai-config';
10+
11+
// Initialize OpenAI client
12+
const client = new OpenAI({
13+
apiKey: process.env.OPENAI_API_KEY, // This is the default and can be omitted
14+
});
15+
16+
// Validate required environment variables
17+
if (!sdkKey) {
18+
console.error('*** Please set the LAUNCHDARKLY_SDK_KEY env first');
19+
process.exit(1);
20+
}
21+
22+
if (!aiConfigKey) {
23+
console.error('*** Please set the LAUNCHDARKLY_AI_CONFIG_KEY env first');
24+
process.exit(1);
25+
}
26+
27+
// Initialize LaunchDarkly client
28+
const ldClient = init(sdkKey);
29+
30+
// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard
31+
// soon after you run the demo.
32+
const context: LDContext = {
33+
kind: 'user',
34+
key: 'example-user-key',
35+
name: 'Sandy',
36+
};
37+
38+
async function main(): Promise<void> {
39+
try {
40+
await ldClient.waitForInitialization({ timeout: 10 });
41+
console.log('*** SDK successfully initialized');
42+
const aiClient = initAi(ldClient);
43+
44+
const configValue = await aiClient.modelConfig(
45+
aiConfigKey,
46+
context,
47+
{
48+
model: {
49+
modelId: 'gpt-4',
50+
},
51+
},
52+
{ myVariable: 'My User Defined Variable' },
53+
);
54+
55+
const { tracker } = configValue;
56+
const completion = await tracker.trackOpenAI(async () =>
57+
client.chat.completions.create({
58+
messages: configValue.config.prompt || [],
59+
model: configValue.config.model?.modelId || 'gpt-4',
60+
}),
61+
);
62+
63+
console.log('AI Response:', completion.choices[0]?.message.content);
64+
console.log('Success.');
65+
} catch (error) {
66+
console.log(`*** SDK failed to initialize: ${error}`);
67+
process.exit(1);
68+
}
69+
}
70+
71+
main();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"include": ["/**/*.ts", "/**/*.tsx"],
4+
"exclude": ["node_modules"]
5+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"extends": "@tsconfig/node20/tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": true,
5+
"outDir": "dist",
6+
"baseUrl": ".",
7+
"allowUnusedLabels": false,
8+
"allowUnreachableCode": false,
9+
"noFallthroughCasesInSwitch": true,
10+
"noUncheckedIndexedAccess": true,
11+
"noUnusedLocals": true,
12+
"noUnusedParameters": true,
13+
"forceConsistentCasingInFileNames": true,
14+
"declaration": true,
15+
"sourceMap": true,
16+
"resolveJsonModule": true,
17+
"module": "CommonJS",
18+
"moduleResolution": "Node"
19+
},
20+
"include": ["src"],
21+
"exclude": ["dist", "node_modules"]
22+
}

0 commit comments

Comments
 (0)