Skip to content

Commit

Permalink
Convert openai example to typescript.
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion committed Nov 4, 2024
1 parent 96185fb commit 8eb4ad3
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 68 deletions.
1 change: 0 additions & 1 deletion packages/sdk/ai/examples/bedrock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"type": "commonjs",
"scripts": {
"build": "tsc",
"test": "jest",
"lint": "eslint . --ext .ts"
},
"keywords": [
Expand Down
16 changes: 10 additions & 6 deletions packages/sdk/ai/examples/bedrock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import { initAi, LDAIConfig } from '@launchdarkly/ai';
import { init } from '@launchdarkly/node-server-sdk';

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

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

if (!aiConfigKey) {
console.error('*** Please set the LAUNCHDARKLY_AI_CONFIG_KEY env first');
process.exit(1);
}

const ldClient = init(sdkKey);

// Set up the context properties
Expand All @@ -22,8 +27,6 @@ const context = {
name: 'Sandy',
};

console.log('*** SDK successfully initialized');

function mapPromptToConversation(
prompt: { role: 'user' | 'assistant' | 'system'; content: string }[],
): Message[] {
Expand All @@ -40,10 +43,11 @@ async function main() {

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

configValue = await aiClient.modelConfig(
aiConfigKey,
aiConfigKey!,
context,
{
model: {
Expand All @@ -64,8 +68,8 @@ async function main() {
const completion = tracker.trackBedrockConverse(
await awsClient.send(
new ConverseCommand({
modelId: configValue.config?.model?.modelId ?? 'no-model',
messages: mapPromptToConversation(configValue.config?.prompt ?? []),
modelId: configValue.config.model?.modelId ?? 'no-model',
messages: mapPromptToConversation(configValue.config.prompt ?? []),
}),
),
);
Expand Down
59 changes: 0 additions & 59 deletions packages/sdk/ai/examples/openai/index.js

This file was deleted.

19 changes: 17 additions & 2 deletions packages/sdk/ai/examples/openai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"test": "jest",
"lint": "eslint . --ext .ts"
},
"keywords": [
Expand All @@ -18,10 +17,26 @@
"license": "Apache-2.0",
"dependencies": {
"@launchdarkly/ai": "0.1.0",
"@launchdarkly/node-server-sdk": "9.7.0",
"openai": "^4.58.1"
},
"devDependencies": {
"eslint": "^8.45.0"
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
"@tsconfig/node20": "20.1.4",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"eslint": "^8.45.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^27.6.3",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.7.0",
"prettier": "^3.0.0",
"rimraf": "^5.0.5",
"typedoc": "0.25.0",
"typescript": "^5.5.3"
},
"directories": {
"example": "example"
Expand Down
71 changes: 71 additions & 0 deletions packages/sdk/ai/examples/openai/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* eslint-disable no-console */
import { OpenAI } from 'openai';

import { initAi } from '@launchdarkly/ai';
import { init, LDContext } from '@launchdarkly/node-server-sdk';

// Environment variables
const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY;
const aiConfigKey = process.env.LAUNCHDARKLY_AI_CONFIG_KEY || 'sample-ai-config';

// Initialize OpenAI client
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // This is the default and can be omitted
});

// Validate required environment variables
if (!sdkKey) {
console.error('*** Please set the LAUNCHDARKLY_SDK_KEY env first');
process.exit(1);
}

if (!aiConfigKey) {
console.error('*** Please set the LAUNCHDARKLY_AI_CONFIG_KEY env first');
process.exit(1);
}

// Initialize LaunchDarkly client
const ldClient = init(sdkKey);

// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard
// soon after you run the demo.
const context: LDContext = {
kind: 'user',
key: 'example-user-key',
name: 'Sandy',
};

async function main(): Promise<void> {
try {
await ldClient.waitForInitialization({ timeout: 10 });
console.log('*** SDK successfully initialized');
const aiClient = initAi(ldClient);

const configValue = await aiClient.modelConfig(
aiConfigKey,
context,
{
model: {
modelId: 'gpt-4',
},
},
{ myVariable: 'My User Defined Variable' },
);

const { tracker } = configValue;
const completion = await tracker.trackOpenAI(async () =>
client.chat.completions.create({
messages: configValue.config.prompt || [],
model: configValue.config.model?.modelId || 'gpt-4',
}),
);

console.log('AI Response:', completion.choices[0]?.message.content);
console.log('Success.');
} catch (error) {
console.log(`*** SDK failed to initialize: ${error}`);
process.exit(1);
}
}

main();
5 changes: 5 additions & 0 deletions packages/sdk/ai/examples/openai/tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["/**/*.ts", "/**/*.tsx"],
"exclude": ["node_modules"]
}
22 changes: 22 additions & 0 deletions packages/sdk/ai/examples/openai/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "@tsconfig/node20/tsconfig.json",
"compilerOptions": {
"noEmit": true,
"outDir": "dist",
"baseUrl": ".",
"allowUnusedLabels": false,
"allowUnreachableCode": false,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"forceConsistentCasingInFileNames": true,
"declaration": true,
"sourceMap": true,
"resolveJsonModule": true,
"module": "CommonJS",
"moduleResolution": "Node"
},
"include": ["src"],
"exclude": ["dist", "node_modules"]
}

0 comments on commit 8eb4ad3

Please sign in to comment.