diff --git a/pkgs/sdk/server-ai/docs-src/namespaces/LaunchDarkly.Sdk.Server.Ai.md b/pkgs/sdk/server-ai/docs-src/namespaces/LaunchDarkly.Sdk.Server.Ai.md index 72ff6c52..1842c508 100644 --- a/pkgs/sdk/server-ai/docs-src/namespaces/LaunchDarkly.Sdk.Server.Ai.md +++ b/pkgs/sdk/server-ai/docs-src/namespaces/LaunchDarkly.Sdk.Server.Ai.md @@ -2,3 +2,20 @@ The main namespace for the LaunchDarkly AI server-side .NET SDK. You will most often use (the AI client) as well as the type from . + +To get started, follow this pattern: + +```csharp +using LaunchDarkly.Sdk.Server.Ai; +using LaunchDarkly.Sdk.Server.Ai.Adapters; + +// This is a standard LaunchDarkly server-side .NET client instance. +var baseClient = new LdClient(Configuration.Builder("sdk-key").Build()); + +// The AI client wraps the base client, providing additional AI-related functionality. +var aiClient = new LdAiClient(new LdClientAdapter(baseClient)); + +// Pass in the key of the AI config, a context, and a default value in case the config can't be +// retrieved from LaunchDarkly. +var myModelConfig = aiClient.ModelConfig("my-model-config", Context.New("user-key"), LdAiConfig.Disabled); +``` diff --git a/pkgs/sdk/server-ai/src/Interfaces/ILdAiClient.cs b/pkgs/sdk/server-ai/src/Interfaces/ILdAiClient.cs index 715d9fe3..f9571d1e 100644 --- a/pkgs/sdk/server-ai/src/Interfaces/ILdAiClient.cs +++ b/pkgs/sdk/server-ai/src/Interfaces/ILdAiClient.cs @@ -19,7 +19,7 @@ public interface ILdAiClient /// a prompt message. /// /// - /// the flag key + /// the AI config key /// the context /// the default config, if unable to retrieve from LaunchDarkly /// the list of variables used when interpolating the prompt diff --git a/pkgs/sdk/server-ai/src/Interfaces/ILdAiConfigTracker.cs b/pkgs/sdk/server-ai/src/Interfaces/ILdAiConfigTracker.cs index 335354bf..a8d2c4ed 100644 --- a/pkgs/sdk/server-ai/src/Interfaces/ILdAiConfigTracker.cs +++ b/pkgs/sdk/server-ai/src/Interfaces/ILdAiConfigTracker.cs @@ -6,23 +6,27 @@ namespace LaunchDarkly.Sdk.Server.Ai.Interfaces; /// -/// Represents the interface of the AI Config Tracker, useful for mocking. +/// A utility capable of generating events related to a specific AI model +/// configuration. /// public interface ILdAiConfigTracker { /// - /// The retrieved AI model configuration. + /// The AI model configuration retrieved from LaunchDarkly, or a default value if unable to retrieve. /// public LdAiConfig Config { get; } /// - /// Tracks a duration metric related to this config. + /// Tracks a duration metric related to this config. For example, if a particular operation + /// related to usage of the AI model takes 100ms, this can be tracked and made available in + /// LaunchDarkly. /// /// the duration in milliseconds public void TrackDuration(float durationMs); /// /// Tracks the duration of a task, and returns the result of the task. + /// /// /// the task /// type of the task's result @@ -44,6 +48,24 @@ public interface ILdAiConfigTracker /// /// Tracks a request to a provider. The request is a task that returns a , which /// contains information about the request such as token usage and metrics. + /// + /// It is the responsibility of the caller to fill in the object with any details + /// that should be tracked. + /// + /// Example: + /// + /// var response = tracker.TrackRequest(Task.Run(() => new Response { + /// // 1. Make a request to the AI provider + /// // 2. Identify relevant statistics returned in the response + /// // 3. Return a Response object containing the relevant statistics + /// Usage = new Usage { Total = 1, Input = 1, Output = 1 }, + /// Metrics = new Metrics { LatencyMs = 100 } + /// })); + /// + /// + /// If no latency statistic is explicitly returned in the , then the duration of the + /// Task is automatically measured and recorded as the latency metric associated with this request. + /// /// /// a task representing the request /// the task @@ -52,6 +74,6 @@ public interface ILdAiConfigTracker /// /// Tracks token usage related to this config. /// - /// the usage + /// the token usage public void TrackTokens(Usage usage); } diff --git a/pkgs/sdk/server-ai/src/LdAiClient.cs b/pkgs/sdk/server-ai/src/LdAiClient.cs index 928f2f63..423fa67c 100644 --- a/pkgs/sdk/server-ai/src/LdAiClient.cs +++ b/pkgs/sdk/server-ai/src/LdAiClient.cs @@ -3,6 +3,7 @@ using System.Collections.Immutable; using System.Linq; using System.Text.Json; +using LaunchDarkly.Sdk.Server.Ai.Adapters; using LaunchDarkly.Sdk.Server.Ai.Config; using LaunchDarkly.Sdk.Server.Ai.DataModel; using LaunchDarkly.Sdk.Server.Ai.Interfaces; @@ -25,13 +26,12 @@ public sealed class LdAiClient : ILdAiClient /// /// Example: /// - /// var config = Configuration.Builder("my-sdk-key").Build(); - /// var client = new LdClient(config); - /// var aiClient = new LdAiClient(client); + /// var baseClient = new LdClient(Configuration.Builder("my-sdk-key").Build()); + /// var aiClient = new LdAiClient(new LdClientAdapter(baseClient)); /// /// /// - /// a LaunchDarkly Server-side SDK client instance + /// an object satisfying , such as an public LdAiClient(ILaunchDarklyClient client) { _client = client ?? throw new ArgumentNullException(nameof(client));