namespace AIStudio.Assistants.ModelComparison;
///
/// What all stored comparisons say about the models: which model wins, which wins against which, how often each judge
/// agrees with the user, and how fast each model answers.
///
///
/// A comparison of a model with itself, which tests how consistently it answers, says nothing about which model is
/// better: it counts toward the speed and the agreement of the judge, but not toward the rankings and the pairs. A
/// comparison series counts only with its blind tasks, and only once the user saw its result, see
/// ModelComparisonStatisticsBuilder.Add.
///
public sealed record ModelComparisonStatistics
{
///
/// How many stored comparisons of answers there are.
///
public int ComparisonCount { get; init; }
///
/// How many runs the stored comparisons of answers hold.
///
public int RunCount { get; init; }
///
/// How many comparison series count, those whose result the user saw.
///
public int SeriesCount { get; init; }
///
/// How many blind tasks the counted comparison series hold.
///
public int TaskCount { get; init; }
///
/// Whether anything counts at all.
///
public bool IsEmpty => this.ComparisonCount == 0 && this.SeriesCount == 0;
///
/// The models by the votes of the user, the one with the highest share of wins first.
///
public IReadOnlyList VoteRanking { get; init; } = [];
///
/// The models by the verdicts of the judges, the one with the highest share of wins first.
///
public IReadOnlyList JudgeRanking { get; init; } = [];
///
/// Every pair of models compared with each other, the one compared most often first.
///
public IReadOnlyList Pairings { get; init; } = [];
///
/// Every judge, the one with the most compared runs first.
///
public IReadOnlyList JudgeAgreements { get; init; } = [];
///
/// Every compared model, the fastest to complete an answer first.
///
public IReadOnlyList Speeds { get; init; } = [];
///
/// Computes the statistics of the compared answers, one stored comparison at a time.
///
/// The stored comparisons, see ModelComparisonStore.ReadResultsAsync.
/// The cancellation token.
/// The statistics.
public static Task ComputeAsync(IAsyncEnumerable records, CancellationToken token = default) => ComputeAsync(records, [], ModelComparisonResultSource.ANSWERS, token);
///
/// Computes the statistics of the results a source names, one stored comparison at a time.
///
/// The stored comparisons of answers, see ModelComparisonStore.ReadResultsAsync; read only when the source names them.
/// The comparison series, see ModelComparisonStore.ListSeriesAsync.
/// Which results count.
/// The cancellation token.
/// The statistics.
public static async Task ComputeAsync(IAsyncEnumerable records, IEnumerable series, ModelComparisonResultSource source, CancellationToken token = default)
{
var builder = new ModelComparisonStatisticsBuilder();
if (source is not ModelComparisonResultSource.TASKS)
await foreach (var record in records.WithCancellation(token))
builder.Add(record);
if (source is not ModelComparisonResultSource.ANSWERS)
foreach (var oneSeries in series)
builder.Add(oneSeries);
return builder.Build();
}
}