Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,20 @@ The main namespace for the LaunchDarkly AI server-side .NET SDK.

You will most often use <xref:LaunchDarkly.Sdk.Server.Ai.LdAiClient> (the AI client) as well as
the <xref:LaunchDarkly.Sdk.Context> type from <xref:LaunchDarkly.Sdk>.

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);
```
2 changes: 1 addition & 1 deletion pkgs/sdk/server-ai/src/Interfaces/ILdAiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface ILdAiClient
/// a prompt message.
///
/// </summary>
/// <param name="key">the flag key</param>
/// <param name="key">the AI config key</param>
/// <param name="context">the context</param>
/// <param name="defaultValue">the default config, if unable to retrieve from LaunchDarkly</param>
/// <param name="variables">the list of variables used when interpolating the prompt</param>
Expand Down
30 changes: 26 additions & 4 deletions pkgs/sdk/server-ai/src/Interfaces/ILdAiConfigTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@
namespace LaunchDarkly.Sdk.Server.Ai.Interfaces;

/// <summary>
/// Represents the interface of the AI Config Tracker, useful for mocking.
/// A utility capable of generating events related to a specific AI model
/// configuration.
/// </summary>
public interface ILdAiConfigTracker
{
/// <summary>
/// The retrieved AI model configuration.
/// The AI model configuration retrieved from LaunchDarkly, or a default value if unable to retrieve.
/// </summary>
public LdAiConfig Config { get; }

/// <summary>
/// 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.
/// </summary>
/// <param name="durationMs">the duration in milliseconds</param>
public void TrackDuration(float durationMs);

/// <summary>
/// Tracks the duration of a task, and returns the result of the task.
///
/// </summary>
/// <param name="task">the task</param>
/// <typeparam name="T">type of the task's result</typeparam>
Expand All @@ -44,6 +48,24 @@ public interface ILdAiConfigTracker
/// <summary>
/// Tracks a request to a provider. The request is a task that returns a <see cref="Response"/>, which
/// contains information about the request such as token usage and metrics.
///
/// It is the responsibility of the caller to fill in the <see cref="Response"/> object with any details
/// that should be tracked.
///
/// Example:
/// <code>
/// 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 }
/// }));
/// </code>
///
/// If no latency statistic is explicitly returned in the <see cref="Response"/>, then the duration of the
/// Task is automatically measured and recorded as the latency metric associated with this request.
///
/// </summary>
/// <param name="request">a task representing the request</param>
/// <returns>the task</returns>
Expand All @@ -52,6 +74,6 @@ public interface ILdAiConfigTracker
/// <summary>
/// Tracks token usage related to this config.
/// </summary>
/// <param name="usage">the usage</param>
/// <param name="usage">the token usage</param>
public void TrackTokens(Usage usage);
}
8 changes: 4 additions & 4 deletions pkgs/sdk/server-ai/src/LdAiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,13 +26,12 @@ public sealed class LdAiClient : ILdAiClient
///
/// Example:
/// <code>
/// 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));
/// </code>
///
/// </summary>
/// <param name="client">a LaunchDarkly Server-side SDK client instance</param>
/// <param name="client">an object satisfying <see cref="ILaunchDarklyClient"/>, such as an <see cref="LdClientAdapter"/></param>
public LdAiClient(ILaunchDarklyClient client)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
Expand Down
Loading