Class: LaunchDarkly::Server::AI::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(ld_client) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


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_clientObject (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

#loggerObject (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

Parameters:

  • config_key (String)

    The key of the configuration flag

  • context (LDContext)

    The context used when evaluating the flag

  • default_value (AIConfig) (defaults to: nil)

    The default value to use if the flag is not found

  • variables (Hash) (defaults to: nil)

    Optional variables for rendering messages

Returns:

  • (AIConfig)

    An AIConfig instance containing the configuration data



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
  messages = nil
  if variation[:messages].is_a?(Array) && variation[:messages].all? { |msg| msg.is_a?(Hash) }
    messages = variation[:messages].map do |message|
      next unless message[:content].is_a?(String)

      Message.new(
        message[:role],
        Mustache.render(message[: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: messages,
    tracker: tracker,
    model: model,
    provider: provider_config
  )
end