No results for ""
EXPAND ALL
  • Home
  • API docs

Go (AI) SDK reference

Read time: 5 minutes
Last edited: Dec 17, 2024
The AI configs product is available for early access

The AI configs product is only available in early access for customers on Foundation and Enterprise 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 Go (AI) SDK is currently in an alpha version.

Overview

This topic documents how to get started with the Go (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 repositoryldai
Sample application
Published modulepkg.go.dev

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 Go (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 Go (AI) SDK to customize your AI config based on the context that you provide. This means both the message 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 Go (AI) SDK in your application.

Install the SDK

First, install the AI SDK as a dependency in your application. How you do this depends on what dependency management system you are using:

  • If you are using the standard Go modules system, import the SDK packages in your code and go build will automatically download them. The SDK and its dependencies are modules.
  • Otherwise, use the go get command and specify the SDK version, such as go get github.com/launchdarkly/go-server-sdk/ldai.

The Go (AI) SDK is built on the Go SDK, so you'll need to install that as well.

Here's how:

Initialize the client

After you install and import the SDK, create a single, shared instance of LDClient. Then, 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 Go SDK uses an SDK key

The Go SDK uses 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:

This example assumes you've imported the LaunchDarkly SDK package as ld, as shown above.

Best practices for error handling

The second return type in these code samples ( _ ) represents an error in case the LaunchDarkly client does not initialize. Consider naming the return value and using it with proper error handling.

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:

Customize an AI config

Then, use Config to customize the AI config. 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:

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:

Alternatively, you can use the SDK's other Track* functions to record these metrics manually. The TrackMetric function is expecting a response, so you may need to do this if your application requires streaming.

To learn more, read Tracking AI metrics.

Supported features

This SDK supports the following features: