|
| 1 | +use std::time::Duration; |
| 2 | + |
| 3 | +use relay_conventions::{ |
| 4 | + GEN_AI_COST_INPUT_TOKENS, GEN_AI_COST_OUTPUT_TOKENS, GEN_AI_COST_TOTAL_TOKENS, |
| 5 | + GEN_AI_REQUEST_MODEL, GEN_AI_RESPONSE_MODEL, GEN_AI_RESPONSE_TPS, |
| 6 | + GEN_AI_USAGE_INPUT_CACHED_TOKENS, GEN_AI_USAGE_INPUT_TOKENS, |
| 7 | + GEN_AI_USAGE_OUTPUT_REASONING_TOKENS, GEN_AI_USAGE_OUTPUT_TOKENS, GEN_AI_USAGE_TOTAL_TOKENS, |
| 8 | +}; |
| 9 | +use relay_event_schema::protocol::Attributes; |
| 10 | +use relay_protocol::Annotated; |
| 11 | + |
| 12 | +use crate::ModelCosts; |
| 13 | +use crate::span::ai; |
| 14 | + |
| 15 | +/// Normalizes AI attributes. |
| 16 | +pub fn normalize_ai( |
| 17 | + attributes: &mut Annotated<Attributes>, |
| 18 | + duration: Option<Duration>, |
| 19 | + costs: Option<&ModelCosts>, |
| 20 | +) { |
| 21 | + let Some(attributes) = attributes.value_mut() else { |
| 22 | + return; |
| 23 | + }; |
| 24 | + |
| 25 | + normalize_total_tokens(attributes); |
| 26 | + normalize_tokens_per_second(attributes, duration); |
| 27 | + normalize_ai_costs(attributes, costs); |
| 28 | +} |
| 29 | + |
| 30 | +/// Calculates the [`GEN_AI_USAGE_TOTAL_TOKENS`] attribute. |
| 31 | +fn normalize_total_tokens(attributes: &mut Attributes) { |
| 32 | + if attributes.contains_key(GEN_AI_USAGE_TOTAL_TOKENS) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + let input_tokens = attributes |
| 37 | + .get_value(GEN_AI_USAGE_INPUT_TOKENS) |
| 38 | + .and_then(|v| v.as_f64()); |
| 39 | + |
| 40 | + let output_tokens = attributes |
| 41 | + .get_value(GEN_AI_USAGE_OUTPUT_TOKENS) |
| 42 | + .and_then(|v| v.as_f64()); |
| 43 | + |
| 44 | + if input_tokens.is_none() && output_tokens.is_none() { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + let total_tokens = input_tokens.unwrap_or(0.0) + output_tokens.unwrap_or(0.0); |
| 49 | + attributes.insert(GEN_AI_USAGE_TOTAL_TOKENS, total_tokens); |
| 50 | +} |
| 51 | + |
| 52 | +/// Calculates the [`GEN_AI_RESPONSE_TPS`] attribute. |
| 53 | +fn normalize_tokens_per_second(attributes: &mut Attributes, duration: Option<Duration>) { |
| 54 | + let Some(duration) = duration.filter(|d| !d.is_zero()) else { |
| 55 | + return; |
| 56 | + }; |
| 57 | + |
| 58 | + if attributes.contains_key(GEN_AI_RESPONSE_TPS) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + let output_tokens = attributes |
| 63 | + .get_value(GEN_AI_USAGE_OUTPUT_TOKENS) |
| 64 | + .and_then(|v| v.as_f64()) |
| 65 | + .filter(|v| *v > 0.0); |
| 66 | + |
| 67 | + if let Some(output_tokens) = output_tokens { |
| 68 | + let tps = output_tokens / duration.as_secs_f64(); |
| 69 | + attributes.insert(GEN_AI_RESPONSE_TPS, tps); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// Calculates model costs and serializes them into attributes. |
| 74 | +fn normalize_ai_costs(attributes: &mut Attributes, model_costs: Option<&ModelCosts>) { |
| 75 | + if attributes.contains_key(GEN_AI_COST_TOTAL_TOKENS) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + let model_cost = attributes |
| 80 | + .get_value(GEN_AI_REQUEST_MODEL) |
| 81 | + .or_else(|| attributes.get_value(GEN_AI_RESPONSE_MODEL)) |
| 82 | + .and_then(|v| v.as_str()) |
| 83 | + .and_then(|model| model_costs?.cost_per_token(model)); |
| 84 | + |
| 85 | + let Some(model_cost) = model_cost else { return }; |
| 86 | + |
| 87 | + let get_tokens = |key| { |
| 88 | + attributes |
| 89 | + .get_value(key) |
| 90 | + .and_then(|v| v.as_f64()) |
| 91 | + .unwrap_or(0.0) |
| 92 | + }; |
| 93 | + |
| 94 | + let tokens = ai::UsedTokens { |
| 95 | + input_tokens: get_tokens(GEN_AI_USAGE_INPUT_TOKENS), |
| 96 | + input_cached_tokens: get_tokens(GEN_AI_USAGE_INPUT_CACHED_TOKENS), |
| 97 | + output_tokens: get_tokens(GEN_AI_USAGE_OUTPUT_TOKENS), |
| 98 | + output_reasoning_tokens: get_tokens(GEN_AI_USAGE_OUTPUT_REASONING_TOKENS), |
| 99 | + }; |
| 100 | + |
| 101 | + let Some(costs) = ai::calculate_costs(model_cost, tokens) else { |
| 102 | + return; |
| 103 | + }; |
| 104 | + |
| 105 | + attributes.insert(GEN_AI_COST_INPUT_TOKENS, costs.input); |
| 106 | + attributes.insert(GEN_AI_COST_OUTPUT_TOKENS, costs.output); |
| 107 | + attributes.insert(GEN_AI_COST_TOTAL_TOKENS, costs.total()); |
| 108 | +} |
0 commit comments