.NET AI SDK reference

The AI configs product is available for early access

The AI configs product is only available in early access for customers on select plans. To request early access, navigate to AI configs and join the waitlist.


The AI SDKs are designed for use with the AI configs product. The .NET AI SDK is currently in an alpha version.

Overview

This topic documents how to get started with the .NET AI SDK, and links to reference information on all of the supported features.

SDK quick links

LaunchDarkly’s SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and sample applications:

ResourceLocation
SDK API documentationSDK API docs
GitHub repositoryserver-ai
Sample application
Published moduleNuGet
For use in server-side applications only

This SDK is intended for use in multi-user .NET server applications. To learn more about LaunchDarkly’s different SDK types, read Client-side, server-side, and edge SDKs.

Get started

LaunchDarkly AI SDKs interact with AI configs. AI configs are the LaunchDarkly resources that manage model configurations and messages for your generative AI applications.

Try the Quickstart

This reference guide describes working specifically with the .NET AI SDK. For a complete introduction to LaunchDarkly AI SDKs and how they interact with AI configs, read Quickstart for AI configs.

You can use the .NET AI SDK to customize your AI config based on the context that you provide. This means both the messages and the model evaluation in your generative AI application are specific to each end user, at runtime. You can also use the AI SDKs to record metrics from your AI model generation, including duration and tokens.

Follow these instructions to start using the .NET AI SDK in your application.

Install the SDK

First, install the AI SDK as a dependency in your application using your application’s dependency manager. If you want to depend on a specific version, refer to the SDK releases page to identify the latest version.

The .NET AI SDK is built on the .NET (server-side) SDK, so you’ll need to install that as well. The .NET AI SDK requires version 8 or higher of the .NET (server-side) SDK.

Here’s how:

Shell
$Install-Package LaunchDarkly.ServerSdk
>Install-Package LaunchDarkly.ServerSdk.Ai

Next, import the namespaces in your application code. The namespace is not the same as the package name:

.NET AI SDK
1using LaunchDarkly.Sdk.Server.Ai;
2using LaunchDarkly.Sdk.Server.Ai.Adapters;
3using LaunchDarkly.Sdk.Server.Ai.Config;

Initialize the client

After you install and import the SDK, create a single, shared instance of LdClient. When the LdClient is initialized, use it to initialize the LdAiClient. The LdAiClient is how you interact with AI configs. Specify the SDK key to authorize your application to connect to a particular environment within LaunchDarkly.

The .NET SDKs use an SDK key

The .NET AI and server-side SDKs use an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.

Here’s how:

.NET AI SDK
1var baseClient = new LdClient(Configuration.Builder("sdk-key-123").StartWaitTime(TimeSpan.FromSeconds(5)).Build()));
2var aiClient = new LdAiClient(new LdClientAdapter(baseClient));

Configure the context

Next, configure the context that will use the AI config, that is, the context that will encounter generated AI content in your application. The context attributes determine which variation of the AI config LaunchDarkly serves to the end user, based on the targeting rules in your AI config. If you are using template variables in the messages in your AI config’s variations, the context attributes also fill in values for the template variables.

Here’s how:

.NET AI SDK
1var context = Context.Builder("context-key-123abc")
2 .Name("Sandy")
3 .Build();

Customize an AI config

Then, use Config to customize the AI config. You need to call Config each time you generate content from your AI model.

This function returns the customized messages and model. Customization means that any variables you include in the messages when you define the AI config variation have their values set to the context attributes and variables you pass to Config. Then, you can pass the customized messages directly to your AI.

The customization process within the AI SDK is similar to evaluating flags in one of LaunchDarkly’s client-side, server-side, or edge SDKs, in that the SDK completes the customization without a separate network call.

Here’s how:

.NET AI SDK
1var fallbackConfig = LdAiConfig.New()
2 .SetModelName("my-default-model")
3 .SetModelParam("temperature", LdValue.Of(0.8))
4 .AddMessage("", Role.system)
5 .SetModelProviderName('my-default-provider')
6 .SetEnabled(true)
7 .Build()
8
9var tracker = aiClient.Config(
10 "ai-config-key-123abc",
11 context,
12 fallbackConfig,
13 new Dictionary<string, object> {
14 { "exampleCustomVariable", "exampleCustomValue" }
15 }
16);

To learn more, read Customizing AI configs.

Record metrics from AI model generation

Finally, use the TrackRequest function to record metrics from your AI model generation.

Here’s how:

.NET AI SDK, any model
1var response = tracker.TrackRequest(Task.Run(() =>
2 {
3 // Make request to a provider, which automatically tracks metrics in LaunchDarkly.
4 // When sending the request to a provider, use details from tracker.Config.
5 // For instance, you can pass tracker.Config.Model and tracker.Config.Messages.
6 // Optionally, return response metadata, for example to do your own additional logging.
7 //
8 // CAUTION: If the call inside of Task.Run() throws an exception,
9 // the SDK will re-throw that exception
10
11 return new Response
12 {
13 Usage = new Usage { Total = 1, Input = 1, Output = 1 }, /* Token usage data */
14 Metrics = new Metrics { LatencyMs = 100 } /* Metrics data */
15 };
16 }
17));

If you would like to do any additional tracking, besides what LaunchDarkly provides, it is your responsibility to fill in the Response object with the data you want to track.

Make sure to call Config each time you generate content from your AI model:

.NET AI SDK, next request to provider
1var tracker = aiClient.Config(
2 "ai-config-key-123abc",
3 context,
4 fallbackConfig,
5 new Dictionary<string, object> {
6 { "exampleCustomVariable", "exampleCustomValue" }
7 }
8);
9
10var response = tracker.TrackRequest(...)

To learn more, read Tracking AI metrics.

Supported features

This SDK supports the following features:

Built with