Skip to content

Commit 96b8fb0

Browse files
committed
docs
1 parent 36152b8 commit 96b8fb0

File tree

5 files changed

+76
-71
lines changed

5 files changed

+76
-71
lines changed

pkgs/sdk/server-ai/src/Config/LdAiConfig.cs

Lines changed: 50 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,160 +8,154 @@
88
namespace LaunchDarkly.Sdk.Server.Ai.Config
99
{
1010

11-
1211
/// <summary>
13-
/// TBD
12+
/// Represents an AI configuration, which contains model parameters and prompt messages.
1413
/// </summary>
1514
public record LdAiConfig
1615
{
1716

1817
/// <summary>
19-
/// TBD
18+
/// Represents a single message, which is part of a prompt.
2019
/// </summary>
2120
public record Message
2221
{
2322
/// <summary>
24-
/// TBD
23+
/// The content of the message, which may contain Mustache templates.
2524
/// </summary>
2625
public readonly string Content;
2726

2827
/// <summary>
29-
/// TBD
28+
/// The role of the message.
3029
/// </summary>
3130
public readonly Role Role;
3231

33-
/// <summary>
34-
/// TBD
35-
/// </summary>
36-
/// <param name="content">TBD</param>
37-
/// <param name="role">TBD</param>
38-
public Message(string content, Role role)
32+
internal Message(string content, Role role)
3933
{
4034
Content = content;
4135
Role = role;
4236
}
4337
}
4438

4539
/// <summary>
46-
/// TBD
40+
/// Builder for constructing an LdAiConfig instance, which can be passed as the default
41+
/// value to the AI Client's <see cref="LdAiClient.ModelConfig"/> method.
4742
/// </summary>
4843
public class Builder
4944
{
5045
private readonly List<Message> _prompt;
5146
private bool _enabled;
52-
private Dictionary<string, object> _modelParams;
47+
private readonly Dictionary<string, object> _modelParams;
5348

5449

5550
/// <summary>
56-
/// TBD
51+
/// Constructs a new builder. By default, the config will be disabled, with no prompt
52+
/// messages or model parameters.
5753
/// </summary>
5854
public Builder()
5955
{
60-
_enabled = true;
56+
_enabled = false;
6157
_prompt = new List<Message>();
6258
_modelParams = new Dictionary<string, object>();
6359
}
6460

6561
/// <summary>
66-
/// TBD
62+
/// Adds a prompt message with the given content and role. The default role is <see cref="Role.User"/>.
6763
/// </summary>
68-
/// <param name="content"></param>
69-
/// <param name="role"></param>
70-
/// <returns></returns>
64+
/// <param name="content">the content, which may contain Mustache templates</param>
65+
/// <param name="role">the role</param>
66+
/// <returns>a new builder</returns>
7167
public Builder AddPromptMessage(string content, Role role = Role.User)
7268
{
7369
_prompt.Add(new Message(content, role));
7470
return this;
7571
}
7672

7773
/// <summary>
78-
///
74+
/// Disables the config.
7975
/// </summary>
80-
/// <returns></returns>
81-
public Builder Disable()
82-
{
83-
return SetEnabled(false);
84-
}
76+
/// <returns>the builder</returns>
77+
public Builder Disable() => SetEnabled(false);
8578

8679
/// <summary>
87-
///
80+
/// Enables the config.
8881
/// </summary>
89-
/// <param name="enabled"></param>
90-
/// <returns></returns>
82+
/// <returns>the builder</returns>
83+
public Builder Enable() => SetEnabled(true);
84+
85+
/// <summary>
86+
/// Sets the enabled state of the config based on a boolean.
87+
/// </summary>
88+
/// <param name="enabled">whether the config is enabled</param>
89+
/// <returns>the builder</returns>
9190
public Builder SetEnabled(bool enabled)
9291
{
9392
_enabled = enabled;
9493
return this;
9594
}
9695

9796
/// <summary>
98-
///
97+
/// Sets a parameter for the model. The value may be any object.
9998
/// </summary>
100-
/// <param name="key"></param>
101-
/// <param name="value"></param>
102-
/// <returns></returns>
103-
public Builder SetModelParam(string key, object value)
99+
/// <param name="name">the parameter name</param>
100+
/// <param name="value">the parameter value</param>
101+
/// <returns>the builder</returns>
102+
public Builder SetModelParam(string name, object value)
104103
{
105-
_modelParams[key] = value;
104+
_modelParams[name] = value;
106105
return this;
107106
}
108107

109108
/// <summary>
110-
/// TBD
109+
/// Builds the LdAiConfig instance.
111110
/// </summary>
112-
/// <returns></returns>
111+
/// <returns>a new LdAiConfig</returns>
113112
public LdAiConfig Build()
114113
{
115114
return new LdAiConfig(_enabled, _prompt, new Meta(), _modelParams);
116115
}
117116
}
118117

119118
/// <summary>
120-
/// TBD
119+
/// The prompts associated with the config.
121120
/// </summary>
122121
public readonly IReadOnlyList<Message> Prompt;
123122

124123
/// <summary>
125-
/// TBD
124+
/// The model parameters associated with the config.
126125
/// </summary>
127126
public readonly IReadOnlyDictionary<string, object> Model;
128127

129-
private readonly string _versionKey;
130-
131-
132-
private readonly bool _enabled;
133-
134128
internal LdAiConfig(bool enabled, IEnumerable<Message> prompt, Meta meta, IReadOnlyDictionary<string, object> model)
135129
{
136-
Model = model;
137-
Prompt = prompt?.ToList();
138-
_versionKey = meta?.VersionKey ?? "";
139-
_enabled = enabled;
130+
Model = model ?? new Dictionary<string, object>();
131+
Prompt = prompt?.ToList() ?? new List<Message>();
132+
VersionKey = meta?.VersionKey ?? "";
133+
Enabled = enabled;
140134
}
141135

142136

143137
/// <summary>
144-
/// TBD
138+
/// Creates a new LdAiConfig builder.
145139
/// </summary>
146-
/// <returns></returns>
140+
/// <returns>the builder</returns>
147141
public static Builder New() => new();
148142

149143
/// <summary>
150-
/// TBD
144+
/// Returns true if the config is enabled.
151145
/// </summary>
152-
/// <returns></returns>
153-
public bool IsEnabled() => _enabled;
146+
/// <returns>true if enabled</returns>
147+
public bool Enabled { get; }
154148

155149

156150
/// <summary>
157-
///
151+
/// This field meant for internal LaunchDarkly usage.
158152
/// </summary>
159-
public string VersionKey => _versionKey;
153+
public string VersionKey { get; }
160154

161155
/// <summary>
162-
/// TBD
156+
/// Convenient helper that returns a disabled LdAiConfig.
163157
/// </summary>
164-
public static LdAiConfig Disabled = new LdAiConfig(false, null, null, null);
158+
public static LdAiConfig Disabled = New().Disable().Build();
165159

166160
}
167161
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private static object ValueToObject(LdValue value)
131131
/// <param name="defaultValue">the default config, if unable to retrieve</param>
132132
/// <param name="variables">the list of variables used when interpolating the prompt</param>
133133
/// <returns>an AI config</returns>
134-
public LdAiConfigTracker GetModelConfig(string key, Context context, LdAiConfig defaultValue,
134+
public LdAiConfigTracker ModelConfig(string key, Context context, LdAiConfig defaultValue,
135135
IReadOnlyDictionary<string, object> variables = null)
136136
{
137137

pkgs/sdk/server-ai/test/InterpolationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private string Eval(string prompt, Context context, IReadOnlyDictionary<string,
5151
mockClient.Setup(x => x.GetLogger()).Returns(mockLogger.Object);
5252

5353
var client = new LdAiClient(mockClient.Object);
54-
var tracker = client.GetModelConfig("foo", context, LdAiConfig.Disabled, variables);
54+
var tracker = client.ModelConfig("foo", context, LdAiConfig.Disabled, variables);
5555

5656
return tracker.Config.Prompt[0].Content;
5757
}

pkgs/sdk/server-ai/test/LdAiClientTest.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void ReturnsDefaultConfigWhenFlagNotFound()
4242

4343
var defaultConfig = LdAiConfig.New().AddPromptMessage("Hello").Build();
4444

45-
var tracker = client.GetModelConfig("foo", Context.New(ContextKind.Default, "key"), defaultConfig);
45+
var tracker = client.ModelConfig("foo", Context.New(ContextKind.Default, "key"), defaultConfig);
4646

4747
Assert.Equal(defaultConfig, tracker.Config);
4848
}
@@ -94,10 +94,10 @@ public void ConfigNotEnabledReturnsDisabledInstance(string json)
9494
// All the JSON inputs here are considered disabled, either due to lack of the 'enabled' property,
9595
// or if present, it is set to false. Therefore if the default was returned, we'd see the assertion fail
9696
// (since calling LdAiConfig.New() constructs an enabled config by default.)
97-
var tracker = client.GetModelConfig("foo", Context.New(ContextKind.Default, "key"),
97+
var tracker = client.ModelConfig("foo", Context.New(ContextKind.Default, "key"),
9898
LdAiConfig.New().AddPromptMessage("foo").Build());
9999

100-
Assert.False(tracker.Config.IsEnabled());
100+
Assert.False(tracker.Config.Enabled);
101101
}
102102

103103
[Fact]
@@ -125,10 +125,15 @@ public void ConfigEnabledReturnsInstance()
125125
var client = new LdAiClient(mockClient.Object);
126126

127127
// We shouldn't get this default.
128-
var tracker = client.GetModelConfig("foo", context,
128+
var tracker = client.ModelConfig("foo", context,
129129
LdAiConfig.New().AddPromptMessage("Goodbye!").Build());
130130

131-
Assert.Equal(new List<LdAiConfig.Message>{ new("Hello!", Role.System) }, tracker.Config.Prompt);
131+
Assert.Collection(tracker.Config.Prompt,
132+
message =>
133+
{
134+
Assert.Equal("Hello!", message.Content);
135+
Assert.Equal(Role.System, message.Role);
136+
});
132137
}
133138
}
134139
}

pkgs/sdk/server-ai/test/LdAiConfigTest.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,23 @@ public class LdAiConfigTest
99
[Fact]
1010
public void CanDisableAndEnableConfig()
1111
{
12-
var config1 = LdAiConfig.New().Disable().Build();
13-
Assert.False(config1.IsEnabled());
12+
var config1 = LdAiConfig.New().Enable().Disable().Build();
13+
Assert.False(config1.Enabled);
1414

15-
var config2 = LdAiConfig.New().SetEnabled(false).Build();
16-
Assert.False(config2.IsEnabled());
15+
var config2 = LdAiConfig.New().Disable().Enable().Build();
16+
Assert.True(config2.Enabled);
1717

18-
var config3 = LdAiConfig.New().Disable().SetEnabled(true).Build();
19-
Assert.True(config3.IsEnabled());
18+
var config3 = LdAiConfig.New().SetEnabled(true).SetEnabled(false).Build();
19+
Assert.False(config3.Enabled);
2020

21-
var config4 = LdAiConfig.New().SetEnabled(true).Disable().Build();
22-
Assert.False(config4.IsEnabled());
21+
var config4 = LdAiConfig.New().SetEnabled(false).SetEnabled(true).Build();
22+
Assert.True(config4.Enabled);
23+
24+
var config5 = LdAiConfig.New().SetEnabled(true).Disable().Build();
25+
Assert.False(config5.Enabled);
26+
27+
var config6 = LdAiConfig.New().SetEnabled(false).Enable().Build();
28+
Assert.True(config6.Enabled);
2329
}
2430

2531
[Fact]

0 commit comments

Comments
 (0)