diff --git a/packages/sdk/ai/examples/bedrock/README.md b/packages/sdk/ai/examples/bedrock/README.md new file mode 100644 index 0000000000..fae00f9ffa --- /dev/null +++ b/packages/sdk/ai/examples/bedrock/README.md @@ -0,0 +1,37 @@ +# LaunchDarkly AI SDK for AWS Bedrock Example + +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. + +## Installation + +To install the package and its dependencies, run: + +```bash +npm install +``` + +## Configuration + +Before running the example, make sure to set the following environment variables: + +- `LAUNCHDARKLY_SDK_KEY`: Your LaunchDarkly SDK key +- `LAUNCHDARKLY_AI_CONFIG_KEY`: Your LaunchDarkly AI configuration key (defaults to 'sample-ai-config' if not set) + +Additionally, ensure you have proper AWS credentials configured to access Bedrock services. + +## Usage + +The main script (`index.js`) demonstrates how to: + +1. Initialize the LaunchDarkly SDK +2. Set up a user context +3. Initialize the LaunchDarkly AI client +4. Retrieve an AI model configuration +5. Send a prompt to AWS Bedrock +6. Track token usage + +To run the example: + +```bash +node index.js +``` diff --git a/packages/sdk/ai/examples/bedrock/index.js b/packages/sdk/ai/examples/bedrock/index.js new file mode 100644 index 0000000000..8f192f874e --- /dev/null +++ b/packages/sdk/ai/examples/bedrock/index.js @@ -0,0 +1,70 @@ +const LaunchDarkly = require('@launchdarkly/node-server-sdk'); +const LaunchDarklyAI = require('@launchdarkly/ai'); +const { OpenAITokenUsage } = require('@launchdarkly/ai'); // Adjusted require +const { BedrockRuntimeClient, ConverseCommand } = require("@aws-sdk/client-bedrock-runtime"); + +const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY; +const aiConfigKey = process.env.LAUNCHDARKLY_AI_CONFIG_KEY || 'sample-ai-config'; +const ci = process.env.CI; +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 = LaunchDarkly.init(sdkKey); + +// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard +// soon after you run the demo. +const context = { + kind: 'user', + key: 'example-user-key', + name: 'Sandy', + }; + +console.log("*** SDK successfully initialized"); + +async function main() { + let tracker; + let configValue; + try { + await ldClient.waitForInitialization({timeout: 10}); + const aiClient = LaunchDarklyAI.init(ldClient); + + configValue = await aiClient.modelConfig(aiConfigKey, context, false, { myVariable: "My User Defined Variable" }); + tracker = configValue.tracker; + + + + + } catch (error) { + console.log(`*** SDK failed to initialize: ${error}`); + process.exit(1); + } + const conversation = [ + { + role: "user", + content: [{ text: "Hello, how are you?" }], + }, + ]; + + const completion = await tracker.trackBedrockConverse(await awsClient.send( + new ConverseCommand({modelId: configValue.config.config.modelId, messages: mapPromptToConversation(configValue.config.prompt)}) + )); + console.log("AI Response:", completion.output.message.content[0].text); + console.log("Success."); +} + +function mapPromptToConversation(prompt) { + return prompt.map(item => ({ + role: item.role, + content: [{ text: item.content }] + })); + } + +main(); \ No newline at end of file diff --git a/packages/sdk/ai/examples/bedrock/package.json b/packages/sdk/ai/examples/bedrock/package.json new file mode 100644 index 0000000000..a2966514d7 --- /dev/null +++ b/packages/sdk/ai/examples/bedrock/package.json @@ -0,0 +1,33 @@ +{ + "name": "@launchdarkly/hello-ai-bedrock", + "version": "0.1.0", + "description": "LaunchDarkly AI SDK for Node.js", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "build": "tsc", + "test": "jest", + "lint": "eslint . --ext .ts" + }, + "keywords": [ + "launchdarkly", + "ai", + "llm" + ], + "author": "LaunchDarkly", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/client-bedrock-runtime": "^3.679.0", + "@launchdarkly/ai": "0.1.0" + }, + "devDependencies": { + "eslint": "^8.45.0" + }, + "directories": { + "example": "example" + }, + "repository": { + "type": "git", + "url": "github.com/launchdarkly/js-core" + } +} diff --git a/packages/sdk/ai/examples/openai/README.md b/packages/sdk/ai/examples/openai/README.md new file mode 100644 index 0000000000..36f65e4c4d --- /dev/null +++ b/packages/sdk/ai/examples/openai/README.md @@ -0,0 +1,40 @@ +# LaunchDarkly AI SDK for OpenAI Example + +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. + +## Installation + +To install the package and its dependencies, run: + +```bash +npm install +``` + +## Configuration + +Before running the example, make sure to set the following environment variables: + +- `LAUNCHDARKLY_SDK_KEY`: Your LaunchDarkly SDK key +- `LAUNCHDARKLY_AI_CONFIG_KEY`: Your LaunchDarkly AI configuration key (defaults to 'sample-ai-config' if not set) +- `OPENAI_API_KEY`: Your OpenAI API key + +## Usage + +The main script (`index.js`) demonstrates how to: + +1. Initialize the LaunchDarkly SDK +2. Set up a user context +3. Initialize the LaunchDarkly AI client +4. Retrieve an AI model configuration +5. Send a prompt to OpenAI +6. Track token usage + +To run the example: + +```bash +node index.js +``` + +## Note + +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. \ No newline at end of file diff --git a/packages/sdk/ai/examples/openai/index.js b/packages/sdk/ai/examples/openai/index.js new file mode 100644 index 0000000000..412978af02 --- /dev/null +++ b/packages/sdk/ai/examples/openai/index.js @@ -0,0 +1,59 @@ +const LaunchDarkly = require('@launchdarkly/node-server-sdk'); +const LaunchDarklyAI = require('@launchdarkly/ai'); +const { OpenAITokenUsage } = require('@launchdarkly/ai'); // Adjusted require +const OpenAI = require('openai'); + +const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY; +const aiConfigKey = process.env.LAUNCHDARKLY_AI_CONFIG_KEY || 'sample-ai-config'; +const ci = process.env.CI; + +const client = new OpenAI({ + apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted + }); + +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 = LaunchDarkly.init(sdkKey); + +// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard +// soon after you run the demo. +const context = { + kind: 'user', + key: 'example-user-key', + name: 'Sandy', + }; + +console.log("*** SDK successfully initialized"); + +async function main() { + try { + await ldClient.waitForInitialization({timeout: 10}); + const aiClient = LaunchDarklyAI.init(ldClient); + + const configValue = await aiClient.modelConfig(aiConfigKey, context, false, { myVariable: "My User Defined Variable" }); + const tracker = configValue.tracker; + const completion = await tracker.trackOpenAI(async () => { + return await client.chat.completions.create({ + messages: configValue.config.prompt, + model: configValue.config.config.modelId, + }); + }); + + 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(); \ No newline at end of file diff --git a/packages/sdk/ai/examples/openai/package.json b/packages/sdk/ai/examples/openai/package.json new file mode 100644 index 0000000000..771645ac9d --- /dev/null +++ b/packages/sdk/ai/examples/openai/package.json @@ -0,0 +1,33 @@ +{ + "name": "@launchdarkly/hello-ai", + "version": "0.1.0", + "description": "LaunchDarkly AI SDK for Node.js", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "build": "tsc", + "test": "jest", + "lint": "eslint . --ext .ts" + }, + "keywords": [ + "launchdarkly", + "ai", + "llm" + ], + "author": "LaunchDarkly", + "license": "Apache-2.0", + "dependencies": { + "@launchdarkly/ai": "0.1.0", + "openai": "^4.58.1" + }, + "devDependencies": { + "eslint": "^8.45.0" + }, + "directories": { + "example": "example" + }, + "repository": { + "type": "git", + "url": "github.com/launchdarkly/js-core" + } + } \ No newline at end of file