| | | 1 | | using System.Diagnostics; |
| | | 2 | | using System.Globalization; |
| | | 3 | | |
| | | 4 | | namespace OpenAiIntegration; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Filterable Langfuse metadata for prediction observations. |
| | | 8 | | /// </summary> |
| | 1 | 9 | | public sealed record PredictionTelemetryMetadata( |
| | 0 | 10 | | string? HomeTeam = null, |
| | 0 | 11 | | string? AwayTeam = null, |
| | 0 | 12 | | int? RepredictionIndex = null) |
| | | 13 | | { |
| | | 14 | | public void ApplyToObservation(Activity? activity) |
| | | 15 | | { |
| | 0 | 16 | | if (activity is null) |
| | | 17 | | { |
| | 0 | 18 | | return; |
| | | 19 | | } |
| | | 20 | | |
| | 0 | 21 | | SetObservationMetadata(activity, "homeTeam", HomeTeam); |
| | 0 | 22 | | SetObservationMetadata(activity, "awayTeam", AwayTeam); |
| | | 23 | | |
| | 0 | 24 | | if (RepredictionIndex.HasValue) |
| | | 25 | | { |
| | 0 | 26 | | SetObservationMetadata(activity, "repredictionIndex", RepredictionIndex.Value.ToString(CultureInfo.Invariant |
| | | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | if (!string.IsNullOrWhiteSpace(HomeTeam) && !string.IsNullOrWhiteSpace(AwayTeam)) |
| | | 30 | | { |
| | 0 | 31 | | SetObservationMetadata(activity, "match", $"{HomeTeam} vs {AwayTeam}"); |
| | | 32 | | } |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public static string BuildDelimitedFilterValue(IEnumerable<string> values) |
| | | 36 | | { |
| | 1 | 37 | | var normalizedValues = values |
| | 1 | 38 | | .Where(value => !string.IsNullOrWhiteSpace(value)) |
| | 1 | 39 | | .Select(value => value.Trim()) |
| | 1 | 40 | | .Distinct(StringComparer.Ordinal) |
| | 1 | 41 | | .OrderBy(value => value, StringComparer.Ordinal) |
| | 1 | 42 | | .ToArray(); |
| | | 43 | | |
| | 1 | 44 | | return normalizedValues.Length == 0 |
| | 1 | 45 | | ? string.Empty |
| | 1 | 46 | | : $"|{string.Join("|", normalizedValues)}|"; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | private static void SetObservationMetadata(Activity activity, string key, string? value) |
| | | 50 | | { |
| | 0 | 51 | | if (string.IsNullOrWhiteSpace(value)) |
| | | 52 | | { |
| | 0 | 53 | | return; |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | activity.SetTag($"langfuse.observation.metadata.{key}", value); |
| | 0 | 57 | | } |
| | | 58 | | } |