Class: LaunchDarkly::Server::AI::Client
- Inherits:
-
Object
- Object
- LaunchDarkly::Server::AI::Client
- Defined in:
- lib/server/ai/client.rb
Overview
The Client class is the main entry point for the LaunchDarkly AI SDK.
Instance Attribute Summary collapse
-
#ld_client ⇒ Object
readonly
Returns the value of attribute ld_client.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#config(config_key, context, default_value = nil, variables = nil) ⇒ AIConfig
Retrieves the AIConfig.
-
#initialize(ld_client) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(ld_client) ⇒ Client
Returns a new instance of Client.
133 134 135 136 137 138 |
# File 'lib/server/ai/client.rb', line 133 def initialize(ld_client) raise ArgumentError, 'LDClient instance is required' unless ld_client.is_a?(LaunchDarkly::LDClient) @ld_client = ld_client @logger = LaunchDarkly::Server::AI.default_logger end |
Instance Attribute Details
#ld_client ⇒ Object (readonly)
Returns the value of attribute ld_client.
131 132 133 |
# File 'lib/server/ai/client.rb', line 131 def ld_client @ld_client end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
131 132 133 |
# File 'lib/server/ai/client.rb', line 131 def logger @logger end |
Instance Method Details
#config(config_key, context, default_value = nil, variables = nil) ⇒ AIConfig
Retrieves the AIConfig
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/server/ai/client.rb', line 149 def config(config_key, context, default_value = nil, variables = nil) variation = @ld_client.variation( config_key, context, default_value.respond_to?(:to_h) ? default_value.to_h : nil ) all_variables = variables ? variables.dup : {} all_variables[:ldctx] = context.to_h # Process messages and provider configuration = nil if variation[:messages].is_a?(Array) && variation[:messages].all? { |msg| msg.is_a?(Hash) } = variation[:messages].map do || next unless [:content].is_a?(String) Message.new( [:role], Mustache.render([:content], all_variables) ) end end if (provider_config = variation[:provider]) && provider_config.is_a?(Hash) provider_config = ProviderConfig.new(provider_config.fetch(:name, '')) end if (model = variation[:model]) && model.is_a?(Hash) parameters = variation[:model][:parameters] custom = variation[:model][:custom] model = ModelConfig.new( name: variation[:model][:name], parameters: parameters, custom: custom ) end tracker = LaunchDarkly::Server::AI::AIConfigTracker.new( ld_client: @ld_client, variation_key: variation.dig(:_ldMeta, :variationKey) || '', config_key: config_key, version: variation.dig(:_ldMeta, :version) || 1, context: context ) AIConfig.new( enabled: variation.dig(:_ldMeta, :enabled) || false, messages: , tracker: tracker, model: model, provider: provider_config ) end |