Skip to content

Commit 8050d23

Browse files
authored
docs: improve AI SDK documentation (#44)
1 parent 1c602ec commit 8050d23

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

pkgs/sdk/server-ai/docs-src/namespaces/LaunchDarkly.Sdk.Server.Ai.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,20 @@ The main namespace for the LaunchDarkly AI server-side .NET SDK.
22

33
You will most often use <xref:LaunchDarkly.Sdk.Server.Ai.LdAiClient> (the AI client) as well as
44
the <xref:LaunchDarkly.Sdk.Context> type from <xref:LaunchDarkly.Sdk>.
5+
6+
To get started, follow this pattern:
7+
8+
```csharp
9+
using LaunchDarkly.Sdk.Server.Ai;
10+
using LaunchDarkly.Sdk.Server.Ai.Adapters;
11+
12+
// This is a standard LaunchDarkly server-side .NET client instance.
13+
var baseClient = new LdClient(Configuration.Builder("sdk-key").Build());
14+
15+
// The AI client wraps the base client, providing additional AI-related functionality.
16+
var aiClient = new LdAiClient(new LdClientAdapter(baseClient));
17+
18+
// Pass in the key of the AI config, a context, and a default value in case the config can't be
19+
// retrieved from LaunchDarkly.
20+
var myModelConfig = aiClient.ModelConfig("my-model-config", Context.New("user-key"), LdAiConfig.Disabled);
21+
```

pkgs/sdk/server-ai/src/Interfaces/ILdAiClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface ILdAiClient
1919
/// a prompt message.
2020
///
2121
/// </summary>
22-
/// <param name="key">the flag key</param>
22+
/// <param name="key">the AI config key</param>
2323
/// <param name="context">the context</param>
2424
/// <param name="defaultValue">the default config, if unable to retrieve from LaunchDarkly</param>
2525
/// <param name="variables">the list of variables used when interpolating the prompt</param>

pkgs/sdk/server-ai/src/Interfaces/ILdAiConfigTracker.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@
66
namespace LaunchDarkly.Sdk.Server.Ai.Interfaces;
77

88
/// <summary>
9-
/// Represents the interface of the AI Config Tracker, useful for mocking.
9+
/// A utility capable of generating events related to a specific AI model
10+
/// configuration.
1011
/// </summary>
1112
public interface ILdAiConfigTracker
1213
{
1314
/// <summary>
14-
/// The retrieved AI model configuration.
15+
/// The AI model configuration retrieved from LaunchDarkly, or a default value if unable to retrieve.
1516
/// </summary>
1617
public LdAiConfig Config { get; }
1718

1819
/// <summary>
19-
/// Tracks a duration metric related to this config.
20+
/// Tracks a duration metric related to this config. For example, if a particular operation
21+
/// related to usage of the AI model takes 100ms, this can be tracked and made available in
22+
/// LaunchDarkly.
2023
/// </summary>
2124
/// <param name="durationMs">the duration in milliseconds</param>
2225
public void TrackDuration(float durationMs);
2326

2427
/// <summary>
2528
/// Tracks the duration of a task, and returns the result of the task.
29+
///
2630
/// </summary>
2731
/// <param name="task">the task</param>
2832
/// <typeparam name="T">type of the task's result</typeparam>
@@ -44,6 +48,24 @@ public interface ILdAiConfigTracker
4448
/// <summary>
4549
/// Tracks a request to a provider. The request is a task that returns a <see cref="Response"/>, which
4650
/// contains information about the request such as token usage and metrics.
51+
///
52+
/// It is the responsibility of the caller to fill in the <see cref="Response"/> object with any details
53+
/// that should be tracked.
54+
///
55+
/// Example:
56+
/// <code>
57+
/// var response = tracker.TrackRequest(Task.Run(() => new Response {
58+
/// // 1. Make a request to the AI provider
59+
/// // 2. Identify relevant statistics returned in the response
60+
/// // 3. Return a Response object containing the relevant statistics
61+
/// Usage = new Usage { Total = 1, Input = 1, Output = 1 },
62+
/// Metrics = new Metrics { LatencyMs = 100 }
63+
/// }));
64+
/// </code>
65+
///
66+
/// If no latency statistic is explicitly returned in the <see cref="Response"/>, then the duration of the
67+
/// Task is automatically measured and recorded as the latency metric associated with this request.
68+
///
4769
/// </summary>
4870
/// <param name="request">a task representing the request</param>
4971
/// <returns>the task</returns>
@@ -52,6 +74,6 @@ public interface ILdAiConfigTracker
5274
/// <summary>
5375
/// Tracks token usage related to this config.
5476
/// </summary>
55-
/// <param name="usage">the usage</param>
77+
/// <param name="usage">the token usage</param>
5678
public void TrackTokens(Usage usage);
5779
}

pkgs/sdk/server-ai/src/LdAiClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Immutable;
44
using System.Linq;
55
using System.Text.Json;
6+
using LaunchDarkly.Sdk.Server.Ai.Adapters;
67
using LaunchDarkly.Sdk.Server.Ai.Config;
78
using LaunchDarkly.Sdk.Server.Ai.DataModel;
89
using LaunchDarkly.Sdk.Server.Ai.Interfaces;
@@ -25,13 +26,12 @@ public sealed class LdAiClient : ILdAiClient
2526
///
2627
/// Example:
2728
/// <code>
28-
/// var config = Configuration.Builder("my-sdk-key").Build();
29-
/// var client = new LdClient(config);
30-
/// var aiClient = new LdAiClient(client);
29+
/// var baseClient = new LdClient(Configuration.Builder("my-sdk-key").Build());
30+
/// var aiClient = new LdAiClient(new LdClientAdapter(baseClient));
3131
/// </code>
3232
///
3333
/// </summary>
34-
/// <param name="client">a LaunchDarkly Server-side SDK client instance</param>
34+
/// <param name="client">an object satisfying <see cref="ILaunchDarklyClient"/>, such as an <see cref="LdClientAdapter"/></param>
3535
public LdAiClient(ILaunchDarklyClient client)
3636
{
3737
_client = client ?? throw new ArgumentNullException(nameof(client));

0 commit comments

Comments
 (0)