|
| 1 | +# LaunchDarkly AI SDK LangChain Provider for Server-Side JavaScript |
| 2 | + |
| 3 | +[![NPM][server-ai-langchain-npm-badge]][server-ai-langchain-npm-link] |
| 4 | +[![Actions Status][server-ai-langchain-ci-badge]][server-ai-langchain-ci] |
| 5 | +[![Documentation][server-ai-langchain-ghp-badge]][server-ai-langchain-ghp-link] |
| 6 | +[![NPM][server-ai-langchain-dm-badge]][server-ai-langchain-npm-link] |
| 7 | +[![NPM][server-ai-langchain-dt-badge]][server-ai-langchain-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 | +# ☝️☝️☝️☝️☝️☝️ |
| 15 | + |
| 16 | +## LaunchDarkly overview |
| 17 | + |
| 18 | +[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! |
| 19 | + |
| 20 | +[](https://twitter.com/intent/follow?screen_name=launchdarkly) |
| 21 | + |
| 22 | +## Quick Setup |
| 23 | + |
| 24 | +This package provides LangChain integration for the LaunchDarkly AI SDK. The simplest way to use it is with the LaunchDarkly AI SDK's `initChat` method: |
| 25 | + |
| 26 | +1. Install the required packages: |
| 27 | + |
| 28 | +```shell |
| 29 | +npm install @launchdarkly/server-sdk-ai @launchdarkly/server-sdk-ai-langchain --save |
| 30 | +``` |
| 31 | + |
| 32 | +2. Create a chat session and use it: |
| 33 | + |
| 34 | +```typescript |
| 35 | +import { init } from '@launchdarkly/node-server-sdk'; |
| 36 | +import { initAi } from '@launchdarkly/server-sdk-ai'; |
| 37 | + |
| 38 | +// Initialize LaunchDarkly client |
| 39 | +const ldClient = init(sdkKey); |
| 40 | +const aiClient = initAi(ldClient); |
| 41 | + |
| 42 | +// Create a chat session |
| 43 | +const defaultConfig = { |
| 44 | + enabled: true, |
| 45 | + model: { name: 'gpt-4' }, |
| 46 | + provider: { name: 'openai' } |
| 47 | +}; |
| 48 | +const chat = await aiClient.initChat('my-chat-config', context, defaultConfig); |
| 49 | + |
| 50 | +if (chat) { |
| 51 | + const response = await chat.invoke("What is the capital of France?"); |
| 52 | + console.log(response.message.content); |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +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). |
| 57 | + |
| 58 | +## Advanced Usage |
| 59 | + |
| 60 | +For more control, you can use the LangChain provider package directly with LaunchDarkly configurations: |
| 61 | + |
| 62 | +```typescript |
| 63 | +import { LangChainProvider } from '@launchdarkly/server-sdk-ai-langchain'; |
| 64 | +import { HumanMessage } from '@langchain/core/messages'; |
| 65 | + |
| 66 | +// Create a LangChain model from LaunchDarkly configuration |
| 67 | +const llm = await LangChainProvider.createLangChainModel(aiConfig); |
| 68 | + |
| 69 | +// Convert LaunchDarkly messages to LangChain format and add user message |
| 70 | +const configMessages = aiConfig.messages || []; |
| 71 | +const userMessage = new HumanMessage("What is the capital of France?"); |
| 72 | +const allMessages = [...LangChainProvider.convertMessagesToLangChain(configMessages), userMessage]; |
| 73 | + |
| 74 | +// Track the model call with LaunchDarkly tracking |
| 75 | +const response = await aiConfig.tracker.trackMetricsOf( |
| 76 | + (result) => LangChainProvider.createAIMetrics(result), |
| 77 | + () => llm.invoke(allMessages) |
| 78 | +); |
| 79 | + |
| 80 | +console.log('AI Response:', response.content); |
| 81 | +``` |
| 82 | + |
| 83 | + |
| 84 | +## Contributing |
| 85 | + |
| 86 | +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. |
| 87 | + |
| 88 | +## About LaunchDarkly |
| 89 | + |
| 90 | +- 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: |
| 91 | + - 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. |
| 92 | + - 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?). |
| 93 | + - 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. |
| 94 | + - 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). |
| 95 | + - Disable parts of your application to facilitate maintenance, without taking everything offline. |
| 96 | +- 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. |
| 97 | +- Explore LaunchDarkly |
| 98 | + - [launchdarkly.com](https://www.launchdarkly.com/ 'LaunchDarkly Main Website') for more information |
| 99 | + - [docs.launchdarkly.com](https://docs.launchdarkly.com/ 'LaunchDarkly Documentation') for our documentation and SDK reference guides |
| 100 | + - [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ 'LaunchDarkly API Documentation') for our API documentation |
| 101 | + - [blog.launchdarkly.com](https://blog.launchdarkly.com/ 'LaunchDarkly Blog Documentation') for the latest product updates |
| 102 | + |
| 103 | +[server-ai-langchain-ci-badge]: https://github.com/launchdarkly/js-core/actions/workflows/server-ai-langchain.yml/badge.svg |
| 104 | +[server-ai-langchain-ci]: https://github.com/launchdarkly/js-core/actions/workflows/server-ai-langchain.yml |
| 105 | +[server-ai-langchain-npm-badge]: https://img.shields.io/npm/v/@launchdarkly/server-sdk-ai-langchain.svg?style=flat-square |
| 106 | +[server-ai-langchain-npm-link]: https://www.npmjs.com/package/@launchdarkly/server-sdk-ai-langchain |
| 107 | +[server-ai-langchain-ghp-badge]: https://img.shields.io/static/v1?label=GitHub+Pages&message=API+reference&color=00add8 |
| 108 | +[server-ai-langchain-ghp-link]: https://launchdarkly.github.io/js-core/packages/ai-providers/server-ai-langchain/docs/ |
| 109 | +[server-ai-langchain-dm-badge]: https://img.shields.io/npm/dm/@launchdarkly/server-sdk-ai-langchain.svg?style=flat-square |
| 110 | +[server-ai-langchain-dt-badge]: https://img.shields.io/npm/dt/@launchdarkly/server-sdk-ai-langchain.svg?style=flat-square |
0 commit comments