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:, context:) ⇒ AIConfigTracker

Returns a new instance of AIConfigTracker.



47
48
49
50
51
52
53
54
# File 'lib/server/ai/ai_config_tracker.rb', line 47

def initialize(ld_client:, variation_key:, config_key:, version:, context:)
  @ld_client = ld_client
  @variation_key = variation_key
  @config_key = config_key
  @version = version
  @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

#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.



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

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



61
62
63
64
65
66
67
68
69
# File 'lib/server/ai/ai_config_tracker.rb', line 61

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



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

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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/server/ai/ai_config_tracker.rb', line 138

def track_error
  @summary.success = false
  @ld_client.track(
    '$ld:ai:generation',
    @context,
    flag_data,
    1
  )
  @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)



105
106
107
108
109
110
111
112
113
114
# File 'lib/server/ai/ai_config_tracker.rb', line 105

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.



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

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/server/ai/ai_config_tracker.rb', line 119

def track_success
  @summary.success = true
  @ld_client.track(
    '$ld:ai:generation',
    @context,
    flag_data,
    1
  )
  @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



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

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



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

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