Class: LaunchDarkly::Server::AI::AIConfigTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/server/ai/ai_config_tracker.rb

Overview

The AIConfigTracker class is used to track AI configuration usage.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ld_client:, variation_key:, config_key:, version:, model_name:, provider_name:, context:) ⇒ AIConfigTracker

Initialize a new AIConfigTracker instance.

Parameters:

  • ld_client (LDClient)

    The LaunchDarkly client instance

  • variation_key (String)

    The variation key from the flag evaluation

  • config_key (String)

    The configuration key

  • version (Integer)

    The version number

  • model_name (String)

    The name of the AI model being used

  • provider_name (String)

    The name of the AI provider

  • context (LDContext)

    The context used for the flag evaluation



58
59
60
61
62
63
64
65
66
67
# File 'lib/server/ai/ai_config_tracker.rb', line 58

def initialize(ld_client:, variation_key:, config_key:, version:, model_name:, provider_name:, context:)
  @ld_client = ld_client
  @variation_key = variation_key
  @config_key = config_key
  @version = version
  @model_name = model_name
  @provider_name = provider_name
  @context = context
  @summary = MetricSummary.new
end

Instance Attribute Details

#config_keyObject (readonly)

Returns the value of attribute config_key.



45
46
47
# File 'lib/server/ai/ai_config_tracker.rb', line 45

def config_key
  @config_key
end

#contextObject (readonly)

Returns the value of attribute context.



45
46
47
# File 'lib/server/ai/ai_config_tracker.rb', line 45

def context
  @context
end

#ld_clientObject (readonly)

Returns the value of attribute ld_client.



45
46
47
# File 'lib/server/ai/ai_config_tracker.rb', line 45

def ld_client
  @ld_client
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



45
46
47
# File 'lib/server/ai/ai_config_tracker.rb', line 45

def model_name
  @model_name
end

#provider_nameObject (readonly)

Returns the value of attribute provider_name.



45
46
47
# File 'lib/server/ai/ai_config_tracker.rb', line 45

def provider_name
  @provider_name
end

#summaryObject (readonly)

Returns the value of attribute summary.



45
46
47
# File 'lib/server/ai/ai_config_tracker.rb', line 45

def summary
  @summary
end

#variation_keyObject (readonly)

Returns the value of attribute variation_key.



45
46
47
# File 'lib/server/ai/ai_config_tracker.rb', line 45

def variation_key
  @variation_key
end

#versionObject (readonly)

Returns the value of attribute version.



45
46
47
# File 'lib/server/ai/ai_config_tracker.rb', line 45

def version
  @version
end

Instance Method Details

#track_bedrock_converse_metrics { ... } ⇒ Hash

Track AWS Bedrock conversation operations. This method tracks the duration, token usage, and success/error status.

Yields:

  • The block to track.

Returns:

  • (Hash)

    The original response hash.



214
215
216
217
218
219
220
221
222
# File 'lib/server/ai/ai_config_tracker.rb', line 214

def track_bedrock_converse_metrics(&block)
  result = track_duration_of(&block)
  track_success
  track_tokens(bedrock_to_token_usage(result[:usage])) if result[:usage]
  result
rescue StandardError
  track_error
  raise
end

#track_duration(duration) ⇒ Object

Track the duration of an AI operation

Parameters:

  • duration (Integer)

    The duration in milliseconds



74
75
76
77
78
79
80
81
82
# File 'lib/server/ai/ai_config_tracker.rb', line 74

def track_duration(duration)
  @summary.duration = duration
  @ld_client.track(
    '$ld:ai:duration:total',
    @context,
    flag_data,
    duration
  )
end

#track_duration_of { ... } ⇒ Object

Track the duration of a block of code

Yields:

  • The block to measure

Returns:

  • The result of the block



90
91
92
93
94
95
96
# File 'lib/server/ai/ai_config_tracker.rb', line 90

def track_duration_of(&block)
  start_time = Time.now
  yield
ensure
  duration = ((Time.now - start_time) * 1000).to_i
  track_duration(duration)
end

#track_errorObject

Track an error in AI generation



145
146
147
148
149
150
151
152
153
# File 'lib/server/ai/ai_config_tracker.rb', line 145

def track_error
  @summary.success = false
  @ld_client.track(
    '$ld:ai:generation:error',
    @context,
    flag_data,
    1
  )
end

#track_feedback(kind:) ⇒ Object

Track user feedback

Parameters:

  • kind (Symbol)

    The kind of feedback (:positive or :negative)



118
119
120
121
122
123
124
125
126
127
# File 'lib/server/ai/ai_config_tracker.rb', line 118

def track_feedback(kind:)
  @summary.feedback = kind
  event_name = kind == :positive ? '$ld:ai:feedback:user:positive' : '$ld:ai:feedback:user:negative'
  @ld_client.track(
    event_name,
    @context,
    flag_data,
    1
  )
end

#track_openai_metrics { ... } ⇒ Object

Track OpenAI-specific operations. This method tracks the duration, token usage, and success/error status. If the provided block raises, this method will also raise. A failed operation will not have any token usage data.

Yields:

  • The block to track.

Returns:

  • The result of the tracked block.



197
198
199
200
201
202
203
204
205
# File 'lib/server/ai/ai_config_tracker.rb', line 197

def track_openai_metrics(&block)
  result = track_duration_of(&block)
  track_success
  track_tokens(openai_to_token_usage(result[:usage])) if result[:usage]
  result
rescue StandardError
  track_error
  raise
end

#track_successObject

Track a successful AI generation



132
133
134
135
136
137
138
139
140
# File 'lib/server/ai/ai_config_tracker.rb', line 132

def track_success
  @summary.success = true
  @ld_client.track(
    '$ld:ai:generation:success',
    @context,
    flag_data,
    1
  )
end

#track_time_to_first_token(time_to_first_token) ⇒ Object

Track time to first token

Parameters:

  • duration (Integer)

    The duration in milliseconds



103
104
105
106
107
108
109
110
111
# File 'lib/server/ai/ai_config_tracker.rb', line 103

def track_time_to_first_token(time_to_first_token)
  @summary.time_to_first_token = time_to_first_token
  @ld_client.track(
    '$ld:ai:tokens:ttf',
    @context,
    flag_data,
    time_to_first_token
  )
end

#track_tokens(token_usage) ⇒ Object

Track token usage

Parameters:

  • token_usage (TokenUsage)

    An object containing token usage details



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
# File 'lib/server/ai/ai_config_tracker.rb', line 160

def track_tokens(token_usage)
  @summary.usage = token_usage
  if token_usage.total.positive?
    @ld_client.track(
      '$ld:ai:tokens:total',
      @context,
      flag_data,
      token_usage.total
    )
  end
  if token_usage.input.positive?
    @ld_client.track(
      '$ld:ai:tokens:input',
      @context,
      flag_data,
      token_usage.input
    )
  end
  return unless token_usage.output.positive?

  @ld_client.track(
    '$ld:ai:tokens:output',
    @context,
    flag_data,
    token_usage.output
  )
end