|
| 1 | +# LaunchDarkly AI SDK OpenAI Provider for Server-Side JavaScript |
| 2 | + |
| 3 | +[![NPM][server-ai-openai-npm-badge]][server-ai-openai-npm-link] |
| 4 | +[![Actions Status][server-ai-openai-ci-badge]][server-ai-openai-ci] |
| 5 | +[![Documentation][server-ai-openai-ghp-badge]][server-ai-openai-ghp-link] |
| 6 | +[![NPM][server-ai-openai-dm-badge]][server-ai-openai-npm-link] |
| 7 | +[![NPM][server-ai-openai-dt-badge]][server-ai-openai-npm-link] |
| 8 | + |
| 9 | +# ⛔️⛔️⛔️⛔️ |
| 10 | + |
| 11 | +> [!CAUTION] |
| 12 | +> This library is a alpha version and should not be considered ready for production use while this message is visible. |
| 13 | +
|
| 14 | +> [!NOTE] |
| 15 | +> This provider currently uses OpenAI's completion API. We plan to migrate to the responses API in a future release to take advantage of improved functionality and performance. |
| 16 | +
|
| 17 | +# ☝️☝️☝️☝️☝️☝️ |
| 18 | + |
| 19 | +## LaunchDarkly overview |
| 20 | + |
| 21 | +[LaunchDarkly](https://www.launchdarkly.com) is a feature management platform that serves over 100 billion feature flags daily to help teams build better software, faster. [Get started](https://docs.launchdarkly.com/home/getting-started) using LaunchDarkly today! |
| 22 | + |
| 23 | +[](https://twitter.com/intent/follow?screen_name=launchdarkly) |
| 24 | + |
| 25 | +## Quick Setup |
| 26 | + |
| 27 | +This package provides OpenAI integration for the LaunchDarkly AI SDK. The simplest way to use it is with the LaunchDarkly AI SDK's `initChat` method: |
| 28 | + |
| 29 | +1. Install the required packages: |
| 30 | + |
| 31 | +```shell |
| 32 | +npm install @launchdarkly/server-sdk-ai @launchdarkly/server-sdk-ai-openai --save |
| 33 | +``` |
| 34 | + |
| 35 | +2. Create a chat session and use it: |
| 36 | + |
| 37 | +```typescript |
| 38 | +import { init } from '@launchdarkly/node-server-sdk'; |
| 39 | +import { initAi } from '@launchdarkly/server-sdk-ai'; |
| 40 | + |
| 41 | +// Initialize LaunchDarkly client |
| 42 | +const ldClient = init(sdkKey); |
| 43 | +const aiClient = initAi(ldClient); |
| 44 | + |
| 45 | +// Create a chat session |
| 46 | +const defaultConfig = { |
| 47 | + enabled: true, |
| 48 | + model: { name: 'gpt-4' }, |
| 49 | + provider: { name: 'openai' } |
| 50 | +}; |
| 51 | +const chat = await aiClient.initChat('my-chat-config', context, defaultConfig); |
| 52 | + |
| 53 | +if (chat) { |
| 54 | + const response = await chat.invoke("What is the capital of France?"); |
| 55 | + console.log(response.message.content); |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +For more information about using the LaunchDarkly AI SDK, see the [LaunchDarkly AI SDK documentation](https://github.com/launchdarkly/js-core/tree/main/packages/sdk/server-ai/README.md). |
| 60 | + |
| 61 | +## Advanced Usage |
| 62 | + |
| 63 | +For more control, you can use the OpenAI provider package directly with LaunchDarkly configurations: |
| 64 | + |
| 65 | +```typescript |
| 66 | +import { OpenAIProvider } from '@launchdarkly/server-sdk-ai-openai'; |
| 67 | +import { OpenAI } from 'openai'; |
| 68 | + |
| 69 | +// Create an OpenAI client |
| 70 | +const client = new OpenAI({ |
| 71 | + apiKey: process.env.OPENAI_API_KEY, |
| 72 | +}); |
| 73 | + |
| 74 | +// Combine LaunchDarkly AI Config messages with user message |
| 75 | +const configMessages = aiConfig.messages || []; |
| 76 | +const userMessage = { role: 'user', content: 'What is the capital of France?' }; |
| 77 | +const allMessages = [...configMessages, userMessage]; |
| 78 | + |
| 79 | +// Track the model call with LaunchDarkly tracking |
| 80 | +const response = await aiConfig.tracker.trackMetricsOf( |
| 81 | + (result) => OpenAIProvider.createAIMetrics(result), |
| 82 | + () => client.chat.completions.create({ |
| 83 | + model: 'gpt-4', |
| 84 | + messages: allMessages, |
| 85 | + temperature: 0.7, |
| 86 | + }) |
| 87 | +); |
| 88 | + |
| 89 | +console.log('AI Response:', response.choices[0].message.content); |
| 90 | +``` |
| 91 | + |
| 92 | +## Contributing |
| 93 | + |
| 94 | +We encourage pull requests and other contributions from the community. Check out our [contributing guidelines](CONTRIBUTING.md) for instructions on how to contribute to this SDK. |
| 95 | + |
| 96 | +## About LaunchDarkly |
| 97 | + |
| 98 | +- LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can: |
| 99 | + - Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases. |
| 100 | + - Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?). |
| 101 | + - Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file. |
| 102 | + - Grant access to certain features based on user attributes, like payment plan (eg: users on the 'gold' plan get access to more features than users in the 'silver' plan). |
| 103 | + - Disable parts of your application to facilitate maintenance, without taking everything offline. |
| 104 | +- LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Check out [our documentation](https://docs.launchdarkly.com/sdk) for a complete list. |
| 105 | +- Explore LaunchDarkly |
| 106 | + - [launchdarkly.com](https://www.launchdarkly.com/ 'LaunchDarkly Main Website') for more information |
| 107 | + - [docs.launchdarkly.com](https://docs.launchdarkly.com/ 'LaunchDarkly Documentation') for our documentation and SDK reference guides |
| 108 | + - [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ 'LaunchDarkly API Documentation') for our API documentation |
| 109 | + - [blog.launchdarkly.com](https://blog.launchdarkly.com/ 'LaunchDarkly Blog Documentation') for the latest product updates |
| 110 | + |
| 111 | +[server-ai-openai-ci-badge]: https://github.com/launchdarkly/js-core/actions/workflows/server-ai-openai.yml/badge.svg |
| 112 | +[server-ai-openai-ci]: https://github.com/launchdarkly/js-core/actions/workflows/server-ai-openai.yml |
| 113 | +[server-ai-openai-npm-badge]: https://img.shields.io/npm/v/@launchdarkly/server-sdk-ai-openai.svg?style=flat-square |
| 114 | +[server-ai-openai-npm-link]: https://www.npmjs.com/package/@launchdarkly/server-sdk-ai-openai |
| 115 | +[server-ai-openai-ghp-badge]: https://img.shields.io/static/v1?label=GitHub+Pages&message=API+reference&color=00add8 |
| 116 | +[server-ai-openai-ghp-link]: https://launchdarkly.github.io/js-core/packages/ai-providers/server-ai-openai/docs/ |
| 117 | +[server-ai-openai-dm-badge]: https://img.shields.io/npm/dm/@launchdarkly/server-sdk-ai-openai.svg?style=flat-square |
| 118 | +[server-ai-openai-dt-badge]: https://img.shields.io/npm/dt/@launchdarkly/server-sdk-ai-openai.svg?style=flat-square |
0 commit comments