|
| 1 | +# LaunchDarkly Server-side AI SDK for Java |
| 2 | + |
| 3 | +This package provides AI Config functionality for the LaunchDarkly Java Server SDK. It lets you manage prompts, models, and providers as LaunchDarkly AI Configs, interpolate variables into messages and instructions, and record AI metrics (duration, token usage, generation success, feedback, tool calls, and online judge evaluations) back to LaunchDarkly. |
| 4 | + |
| 5 | +It is built on top of the [LaunchDarkly Java Server SDK](https://github.com/launchdarkly/java-server-sdk) and is feature-compatible with the [LaunchDarkly Server-side AI SDK for Python](https://github.com/launchdarkly/python-server-sdk-ai). |
| 6 | + |
| 7 | +## LaunchDarkly overview |
| 8 | + |
| 9 | +[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! |
| 10 | + |
| 11 | +[](https://twitter.com/intent/follow?screen_name=launchdarkly) |
| 12 | + |
| 13 | +## Supported Java versions |
| 14 | + |
| 15 | +This package is compatible with Java 8 and above, matching the LaunchDarkly Java Server SDK. |
| 16 | + |
| 17 | +## Getting started |
| 18 | + |
| 19 | +1. Add the `launchdarkly-java-server-sdk-ai` package to your project, alongside the core Java Server SDK: |
| 20 | + |
| 21 | +``` |
| 22 | +dependencies { |
| 23 | + implementation "com.launchdarkly:launchdarkly-java-server-sdk:7+" |
| 24 | + implementation "com.launchdarkly:launchdarkly-java-server-sdk-ai:0.1.0" |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +2. Construct an `LDAIClient`, wrapping your existing `LDClient`: |
| 29 | + |
| 30 | +```java |
| 31 | +import com.launchdarkly.sdk.*; |
| 32 | +import com.launchdarkly.sdk.server.*; |
| 33 | +import com.launchdarkly.sdk.server.ai.*; |
| 34 | +import com.launchdarkly.sdk.server.ai.datamodel.*; |
| 35 | + |
| 36 | +LDClient ldClient = new LDClient("sdk-key-123abc"); |
| 37 | +LDAIClient aiClient = new LDAIClient(ldClient); |
| 38 | +``` |
| 39 | + |
| 40 | +A single `LDAIClient` should be reused for the lifetime of your application. |
| 41 | + |
| 42 | +## Completion configs |
| 43 | + |
| 44 | +A completion config carries chat-style messages, a model, and a provider. Provide a default to use when the AI Config is unavailable, and any variables to interpolate into the messages. |
| 45 | + |
| 46 | +```java |
| 47 | +import java.util.Collections; |
| 48 | +import java.util.Arrays; |
| 49 | + |
| 50 | +LDContext context = LDContext.create("user-key-123abc"); |
| 51 | + |
| 52 | +AICompletionConfig config = aiClient.completionConfig( |
| 53 | + "my-ai-config", |
| 54 | + context, |
| 55 | + AICompletionConfigDefault.builder() |
| 56 | + .enabled(true) |
| 57 | + .model(new ModelConfig("gpt-4")) |
| 58 | + .messages(Arrays.asList( |
| 59 | + LDMessage.system("You are a helpful assistant named {{name}}."))) |
| 60 | + .build(), |
| 61 | + Collections.singletonMap("name", "Bailey")); |
| 62 | + |
| 63 | +if (config.isEnabled()) { |
| 64 | + AIConfigTracker tracker = config.createTracker(); |
| 65 | + String answer = tracker.trackDurationOf(() -> callYourModel(config)); |
| 66 | + tracker.trackSuccess(); |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +Message and instruction content is interpolated with Mustache templating. In addition to any |
| 71 | +variables you supply, an `ldctx` variable is always available, exposing the attributes of the |
| 72 | +evaluation context (for example `{{ldctx.name}}`, or `{{ldctx.user.name}}` for a multi-context). |
| 73 | + |
| 74 | +## Agent configs |
| 75 | + |
| 76 | +An agent config carries freeform `instructions` rather than chat messages. Retrieve a single agent |
| 77 | +with `agentConfig`, or several at once with `agentConfigs`: |
| 78 | + |
| 79 | +```java |
| 80 | +AIAgentConfig agent = aiClient.agentConfig( |
| 81 | + "research_agent", |
| 82 | + context, |
| 83 | + AIAgentConfigDefault.builder() |
| 84 | + .enabled(true) |
| 85 | + .model(new ModelConfig("gpt-4")) |
| 86 | + .instructions("You are a research assistant specializing in {{topic}}.") |
| 87 | + .build(), |
| 88 | + Collections.singletonMap("topic", "climate change")); |
| 89 | +``` |
| 90 | + |
| 91 | +## Tracking metrics |
| 92 | + |
| 93 | +The `AIConfigTracker` returned by `createTracker()` records metrics for a single AI run. Each scalar |
| 94 | +metric (duration, success/error, feedback, tokens, time-to-first-token) is recorded at most once per |
| 95 | +tracker; obtain a new tracker for each run. |
| 96 | + |
| 97 | +```java |
| 98 | +AIConfigTracker tracker = config.createTracker(); |
| 99 | + |
| 100 | +tracker.trackDuration(durationMs); |
| 101 | +tracker.trackTokens(new TokenUsage(/* total */ 30, /* input */ 10, /* output */ 20)); |
| 102 | +tracker.trackSuccess(); // or tracker.trackError(); |
| 103 | +tracker.trackFeedback(FeedbackKind.POSITIVE); |
| 104 | +``` |
| 105 | + |
| 106 | +To associate deferred events (such as user feedback gathered later) with the original run, persist |
| 107 | +the tracker's resumption token and reconstruct the tracker from it: |
| 108 | + |
| 109 | +```java |
| 110 | +String token = tracker.getResumptionToken(); |
| 111 | +// ...later, possibly in a different process... |
| 112 | +AIConfigTracker resumed = aiClient.createTracker(token, context); |
| 113 | +resumed.trackFeedback(FeedbackKind.POSITIVE); |
| 114 | +``` |
| 115 | + |
| 116 | +## Contributing |
| 117 | + |
| 118 | +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. |
| 119 | + |
| 120 | +## About LaunchDarkly |
| 121 | + |
| 122 | +- 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: |
| 123 | + - 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. |
| 124 | + - 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?). |
| 125 | + - 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. |
| 126 | + - 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). |
| 127 | + - Disable parts of your application to facilitate maintenance, without taking everything offline. |
| 128 | +- 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. |
| 129 | +- Explore LaunchDarkly |
| 130 | + - [launchdarkly.com](https://www.launchdarkly.com/ 'LaunchDarkly Main Website') for more information |
| 131 | + - [docs.launchdarkly.com](https://docs.launchdarkly.com/ 'LaunchDarkly Documentation') for our documentation and SDK reference guides |
| 132 | + - [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ 'LaunchDarkly API Documentation') for our API documentation |
| 133 | + - [blog.launchdarkly.com](https://blog.launchdarkly.com/ 'LaunchDarkly Blog Documentation') for the latest product updates |
0 commit comments