Class: LaunchDarkly::Server::AI::AIConfigTracker
- Inherits:
-
Object
- Object
- LaunchDarkly::Server::AI::AIConfigTracker
- Defined in:
- lib/server/ai/ai_config_tracker.rb
Overview
The AIConfigTracker class is used to track AI configuration usage.
Instance Attribute Summary collapse
-
#config_key ⇒ Object
readonly
Returns the value of attribute config_key.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#ld_client ⇒ Object
readonly
Returns the value of attribute ld_client.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#provider_name ⇒ Object
readonly
Returns the value of attribute provider_name.
-
#summary ⇒ Object
readonly
Returns the value of attribute summary.
-
#variation_key ⇒ Object
readonly
Returns the value of attribute variation_key.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#initialize(ld_client:, variation_key:, config_key:, version:, model_name:, provider_name:, context:) ⇒ AIConfigTracker
constructor
Initialize a new AIConfigTracker instance.
-
#track_bedrock_converse_metrics { ... } ⇒ Hash
Track AWS Bedrock conversation operations.
-
#track_duration(duration) ⇒ Object
Track the duration of an AI operation.
-
#track_duration_of { ... } ⇒ Object
Track the duration of a block of code.
-
#track_error ⇒ Object
Track an error in AI generation.
-
#track_feedback(kind:) ⇒ Object
Track user feedback.
-
#track_openai_metrics { ... } ⇒ Object
Track OpenAI-specific operations.
-
#track_success ⇒ Object
Track a successful AI generation.
-
#track_time_to_first_token(time_to_first_token) ⇒ Object
Track time to first token.
-
#track_tokens(token_usage) ⇒ Object
Track token usage.
Constructor Details
#initialize(ld_client:, variation_key:, config_key:, version:, model_name:, provider_name:, context:) ⇒ AIConfigTracker
Initialize a new AIConfigTracker instance.
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_key ⇒ Object (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 |
#context ⇒ Object (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_client ⇒ Object (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_name ⇒ Object (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_name ⇒ Object (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 |
#summary ⇒ Object (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_key ⇒ Object (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 |
#version ⇒ Object (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.
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
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
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_error ⇒ Object
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
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.
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_success ⇒ Object
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
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
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 |