Skip to content

Commit ce64ccf

Browse files
committed
add ALL of the interfaces
1 parent 8c18ca5 commit ce64ccf

File tree

4 files changed

+71
-39
lines changed

4 files changed

+71
-39
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface ILdAiClient
1111

1212
/// <summary>
1313
/// Retrieves a LaunchDarkly AI config identified by the given key. The return value
14-
/// is an <see cref="LdAiConfigTracker"/>, which makes the configuration available and
14+
/// is an <see cref="ILdAiConfigTracker"/>, which makes the configuration available and
1515
/// provides convenience methods for generating events related to model usage.
1616
///
1717
/// Any variables provided will be interpolated into the prompt's messages.
@@ -24,6 +24,6 @@ public interface ILdAiClient
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>
2626
/// <returns>an AI config tracker</returns>
27-
public LdAiConfigTracker ModelConfig(string key, Context context, LdAiConfig defaultValue,
27+
public ILdAiConfigTracker ModelConfig(string key, Context context, LdAiConfig defaultValue,
2828
IReadOnlyDictionary<string, object> variables = null);
2929
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using LaunchDarkly.Sdk.Server.Ai.Config;
4+
using LaunchDarkly.Sdk.Server.Ai.Metrics;
5+
6+
namespace LaunchDarkly.Sdk.Server.Ai.Interfaces;
7+
8+
/// <summary>
9+
/// Represents the interface of the AI Config Tracker, useful for mocking.
10+
/// </summary>
11+
public interface ILdAiConfigTracker
12+
{
13+
/// <summary>
14+
/// The retrieved AI model configuration.
15+
/// </summary>
16+
public LdAiConfig Config { get; }
17+
18+
/// <summary>
19+
/// Tracks a duration metric related to this config.
20+
/// </summary>
21+
/// <param name="durationMs">the duration in milliseconds</param>
22+
public void TrackDuration(float durationMs);
23+
24+
/// <summary>
25+
/// Tracks the duration of a task, and returns the result of the task.
26+
/// </summary>
27+
/// <param name="task">the task</param>
28+
/// <typeparam name="T">type of the task's result</typeparam>
29+
/// <returns>the task</returns>
30+
public Task<T> TrackDurationOfTask<T>(Task<T> task);
31+
32+
/// <summary>
33+
/// Tracks feedback (positive or negative) related to the output of the model.
34+
/// </summary>
35+
/// <param name="feedback">the feedback</param>
36+
/// <exception cref="ArgumentOutOfRangeException">thrown if the feedback value is not <see cref="Feedback.Positive"/> or <see cref="Feedback.Negative"/></exception>
37+
public void TrackFeedback(Feedback feedback);
38+
39+
/// <summary>
40+
/// Tracks a generation event related to this config.
41+
/// </summary>
42+
public void TrackSuccess();
43+
44+
/// <summary>
45+
/// Tracks a request to a provider. The request is a task that returns a <see cref="ProviderResponse"/>, which
46+
/// contains information about the request such as token usage and metrics.
47+
/// </summary>
48+
/// <param name="request">a task representing the request</param>
49+
/// <returns>the task</returns>
50+
public Task<ProviderResponse> TrackRequest(Task<ProviderResponse> request);
51+
52+
/// <summary>
53+
/// Tracks token usage related to this config.
54+
/// </summary>
55+
/// <param name="usage">the usage</param>
56+
public void TrackTokens(Usage usage);
57+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public LdAiClient(ILaunchDarklyClient client)
4343
private const string LdContextVariable = "ldctx";
4444

4545
/// <inheritdoc/>
46-
public LdAiConfigTracker ModelConfig(string key, Context context, LdAiConfig defaultValue,
46+
public ILdAiConfigTracker ModelConfig(string key, Context context, LdAiConfig defaultValue,
4747
IReadOnlyDictionary<string, object> variables = null)
4848
{
4949

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

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@ namespace LaunchDarkly.Sdk.Server.Ai;
1111
/// <summary>
1212
/// A tracker capable of reporting events related to a particular AI configuration.
1313
/// </summary>
14-
public class LdAiConfigTracker
14+
public class LdAiConfigTracker : ILdAiConfigTracker
1515
{
16-
/// <summary>
17-
/// The retrieved AI model configuration.
18-
/// </summary>
19-
public readonly LdAiConfig Config;
20-
21-
2216
private readonly ILaunchDarklyClient _client;
2317
private readonly Context _context;
2418
private readonly LdValue _trackData;
@@ -52,20 +46,16 @@ public LdAiConfigTracker(ILaunchDarklyClient client, string configKey, LdAiConfi
5246
});
5347
}
5448

55-
/// <summary>
56-
/// Tracks a duration metric related to this config.
57-
/// </summary>
58-
/// <param name="durationMs">the duration in milliseconds</param>
49+
50+
/// <inheritdoc/>
51+
public LdAiConfig Config { get; }
52+
53+
/// <inheritdoc/>
5954
public void TrackDuration(float durationMs) =>
6055
_client.Track(Duration, _context, _trackData, durationMs);
6156

6257

63-
/// <summary>
64-
/// Tracks the duration of a task, and returns the result of the task.
65-
/// </summary>
66-
/// <param name="task">the task</param>
67-
/// <typeparam name="T">type of the task's result</typeparam>
68-
/// <returns>the task</returns>
58+
/// <inheritdoc/>
6959
public async Task<T> TrackDurationOfTask<T>(Task<T> task)
7060
{
7161
var result = await MeasureDurationOfTaskMs(task);
@@ -81,11 +71,7 @@ private static async Task<Tuple<T, long>> MeasureDurationOfTaskMs<T>(Task<T> tas
8171
return Tuple.Create(result, sw.ElapsedMilliseconds);
8272
}
8373

84-
/// <summary>
85-
/// Tracks feedback (positive or negative) related to the output of the model.
86-
/// </summary>
87-
/// <param name="feedback">the feedback</param>
88-
/// <exception cref="ArgumentOutOfRangeException">thrown if the feedback value is not <see cref="Feedback.Positive"/> or <see cref="Feedback.Negative"/></exception>
74+
/// <inheritdoc/>
8975
public void TrackFeedback(Feedback feedback)
9076
{
9177
switch (feedback)
@@ -101,21 +87,13 @@ public void TrackFeedback(Feedback feedback)
10187
}
10288
}
10389

104-
/// <summary>
105-
/// Tracks a generation event related to this config.
106-
/// </summary>
90+
/// <inheritdoc/>
10791
public void TrackSuccess()
10892
{
10993
_client.Track(Generation, _context, _trackData, 1);
11094
}
11195

112-
113-
/// <summary>
114-
/// Tracks a request to a provider. The request is a task that returns a <see cref="ProviderResponse"/>, which
115-
/// contains information about the request such as token usage and metrics.
116-
/// </summary>
117-
/// <param name="request">a task representing the request</param>
118-
/// <returns>the task</returns>
96+
/// <inheritdoc/>
11997
public async Task<ProviderResponse> TrackRequest(Task<ProviderResponse> request)
12098
{
12199
var (result, durationMs) = await MeasureDurationOfTaskMs(request);
@@ -131,10 +109,7 @@ public async Task<ProviderResponse> TrackRequest(Task<ProviderResponse> request)
131109
return result;
132110
}
133111

134-
/// <summary>
135-
/// Tracks token usage related to this config.
136-
/// </summary>
137-
/// <param name="usage">the usage</param>
112+
/// <inheritdoc/>
138113
public void TrackTokens(Usage usage)
139114
{
140115
if (usage.Total is > 0)

0 commit comments

Comments
 (0)