|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using LaunchDarkly.Sdk.Server.Ai.DataModel; |
| 4 | + |
| 5 | +namespace LaunchDarkly.Sdk.Server.Ai.Config; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Represents an AI configuration, which contains model parameters and prompt messages. |
| 9 | +/// </summary> |
| 10 | +public record LdAiConfig |
| 11 | +{ |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Represents a single message, which is part of a prompt. |
| 15 | + /// </summary> |
| 16 | + public record Message |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// The content of the message, which may contain Mustache templates. |
| 20 | + /// </summary> |
| 21 | + public readonly string Content; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// The role of the message. |
| 25 | + /// </summary> |
| 26 | + public readonly Role Role; |
| 27 | + |
| 28 | + internal Message(string content, Role role) |
| 29 | + { |
| 30 | + Content = content; |
| 31 | + Role = role; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Builder for constructing an LdAiConfig instance, which can be passed as the default |
| 37 | + /// value to the AI Client's <see cref="LdAiClient.ModelConfig"/> method. |
| 38 | + /// </summary> |
| 39 | + public class Builder |
| 40 | + { |
| 41 | + private bool _enabled; |
| 42 | + private readonly List<Message> _prompt; |
| 43 | + private readonly Dictionary<string, object> _modelParams; |
| 44 | + |
| 45 | + internal Builder() |
| 46 | + { |
| 47 | + _enabled = false; |
| 48 | + _prompt = new List<Message>(); |
| 49 | + _modelParams = new Dictionary<string, object>(); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Adds a prompt message with the given content and role. The default role is <see cref="Role.User"/>. |
| 54 | + /// </summary> |
| 55 | + /// <param name="content">the content, which may contain Mustache templates</param> |
| 56 | + /// <param name="role">the role</param> |
| 57 | + /// <returns>a new builder</returns> |
| 58 | + public Builder AddPromptMessage(string content, Role role = Role.User) |
| 59 | + { |
| 60 | + _prompt.Add(new Message(content, role)); |
| 61 | + return this; |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Disables the config. |
| 66 | + /// </summary> |
| 67 | + /// <returns>the builder</returns> |
| 68 | + public Builder Disable() => SetEnabled(false); |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Enables the config. |
| 72 | + /// </summary> |
| 73 | + /// <returns>the builder</returns> |
| 74 | + public Builder Enable() => SetEnabled(true); |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Sets the enabled state of the config based on a boolean. |
| 78 | + /// </summary> |
| 79 | + /// <param name="enabled">whether the config is enabled</param> |
| 80 | + /// <returns>the builder</returns> |
| 81 | + public Builder SetEnabled(bool enabled) |
| 82 | + { |
| 83 | + _enabled = enabled; |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Sets a parameter for the model. The value may be any object. |
| 89 | + /// </summary> |
| 90 | + /// <param name="name">the parameter name</param> |
| 91 | + /// <param name="value">the parameter value</param> |
| 92 | + /// <returns>the builder</returns> |
| 93 | + public Builder SetModelParam(string name, object value) |
| 94 | + { |
| 95 | + _modelParams[name] = value; |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Builds the LdAiConfig instance. |
| 101 | + /// </summary> |
| 102 | + /// <returns>a new LdAiConfig</returns> |
| 103 | + public LdAiConfig Build() |
| 104 | + { |
| 105 | + return new LdAiConfig(_enabled, _prompt, new Meta(), _modelParams); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// The prompts associated with the config. |
| 111 | + /// </summary> |
| 112 | + public readonly IReadOnlyList<Message> Prompt; |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// The model parameters associated with the config. |
| 116 | + /// </summary> |
| 117 | + public readonly IReadOnlyDictionary<string, object> Model; |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + internal LdAiConfig(bool enabled, IEnumerable<Message> prompt, Meta meta, IReadOnlyDictionary<string, object> model) |
| 122 | + { |
| 123 | + Model = model ?? new Dictionary<string, object>(); |
| 124 | + Prompt = prompt?.ToList() ?? new List<Message>(); |
| 125 | + VersionKey = meta?.VersionKey ?? ""; |
| 126 | + Enabled = enabled; |
| 127 | + } |
| 128 | + |
| 129 | + private static LdValue ObjectToValue(object obj) |
| 130 | + { |
| 131 | + if (obj == null) |
| 132 | + { |
| 133 | + return LdValue.Null; |
| 134 | + } |
| 135 | + |
| 136 | + return obj switch |
| 137 | + { |
| 138 | + bool b => LdValue.Of(b), |
| 139 | + double d => LdValue.Of(d), |
| 140 | + string s => LdValue.Of(s), |
| 141 | + IEnumerable<object> list => LdValue.ArrayFrom(list.Select(ObjectToValue)), |
| 142 | + IDictionary<string, object> dict => LdValue.ObjectFrom(dict.ToDictionary(kv => kv.Key, |
| 143 | + kv => ObjectToValue(kv.Value))), |
| 144 | + _ => LdValue.Null |
| 145 | + }; |
| 146 | + } |
| 147 | + |
| 148 | + internal LdValue ToLdValue() |
| 149 | + { |
| 150 | + return LdValue.ObjectFrom(new Dictionary<string, LdValue> |
| 151 | + { |
| 152 | + { "_ldMeta", LdValue.ObjectFrom( |
| 153 | + new Dictionary<string, LdValue> |
| 154 | + { |
| 155 | + { "versionKey", LdValue.Of(VersionKey) }, |
| 156 | + { "enabled", LdValue.Of(Enabled) } |
| 157 | + }) }, |
| 158 | + { "prompt", LdValue.ArrayFrom(Prompt.Select(m => LdValue.ObjectFrom(new Dictionary<string, LdValue> |
| 159 | + { |
| 160 | + { "content", LdValue.Of(m.Content) }, |
| 161 | + { "role", LdValue.Of(m.Role.ToString()) } |
| 162 | + }))) }, |
| 163 | + { "model", ObjectToValue(Model) } |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// Creates a new LdAiConfig builder. |
| 169 | + /// </summary> |
| 170 | + /// <returns>a new builder</returns> |
| 171 | + public static Builder New() => new(); |
| 172 | + |
| 173 | + /// <summary> |
| 174 | + /// Returns true if the config is enabled. |
| 175 | + /// </summary> |
| 176 | + /// <returns>true if enabled</returns> |
| 177 | + public bool Enabled { get; } |
| 178 | + |
| 179 | + |
| 180 | + /// <summary> |
| 181 | + /// This field meant for internal LaunchDarkly usage. |
| 182 | + /// </summary> |
| 183 | + public string VersionKey { get; } |
| 184 | + |
| 185 | + /// <summary> |
| 186 | + /// Convenient helper that returns a disabled LdAiConfig. |
| 187 | + /// </summary> |
| 188 | + public static LdAiConfig Disabled = New().Disable().Build(); |
| 189 | + |
| 190 | + |
| 191 | +} |
0 commit comments