-
Notifications
You must be signed in to change notification settings - Fork 32
[LEADS-349] Calculate aggregated score from key metrics #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xmican10
wants to merge
1
commit into
lightspeed-core:main
Choose a base branch
from
xmican10:LEADS-349-calculate-aggregated-score-from-key-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,181 @@ | ||||||
| """Quality score models for aggregated quality assessment. | ||||||
|
|
||||||
| Provides Pydantic models for computing and reporting an aggregated quality score | ||||||
| from selected metrics using weighted averaging based on sample sizes. | ||||||
| """ | ||||||
|
|
||||||
| import logging | ||||||
| from typing import Optional | ||||||
|
|
||||||
| from pydantic import BaseModel, Field | ||||||
|
|
||||||
| from lightspeed_evaluation.core.models.summary import MetricStats, ScoreStatistics | ||||||
|
|
||||||
| logger = logging.getLogger(__name__) | ||||||
|
|
||||||
|
|
||||||
| class QualityMetricResult(BaseModel): | ||||||
| """Quality metric result using composition to add weight to score statistics.""" | ||||||
|
|
||||||
| statistics: ScoreStatistics = Field( | ||||||
| description="Score statistics for this quality metric" | ||||||
| ) | ||||||
| weight: float = Field( | ||||||
| default=0.0, | ||||||
| description="Weight proportion (sample_size / total_samples) used in weighted average", | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| class QualityReport(BaseModel): | ||||||
| """Aggregated quality score from selected metrics.""" | ||||||
|
|
||||||
| aggregated_quality_score: float = Field( | ||||||
| default=0.0, description="Weighted average of quality score metrics" | ||||||
| ) | ||||||
| quality_metrics: dict[str, QualityMetricResult] = Field( | ||||||
| default_factory=dict, | ||||||
| description="Individual metrics used in quality score calculation", | ||||||
| ) | ||||||
| extra_metrics: dict[str, ScoreStatistics] = Field( | ||||||
| default_factory=dict, | ||||||
| description="Other evaluated metrics calculated, not used for quality score calculation", | ||||||
| ) | ||||||
| warnings: list[str] = Field( | ||||||
| default_factory=list, | ||||||
| description="Warnings about quality metrics configuration or usage", | ||||||
| ) | ||||||
| api_latency: float = Field( | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| default=0.0, description="[Placeholder] Average API response time in seconds" | ||||||
| ) | ||||||
| api_tokens: int = Field( | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| default=0, | ||||||
| description="[Placeholder] Total number of tokens consumed across all API calls", | ||||||
| ) | ||||||
|
|
||||||
| @staticmethod | ||||||
| def create_report( | ||||||
| by_metric: dict[str, MetricStats], | ||||||
| quality_score_metrics: list[str], | ||||||
| ) -> Optional["QualityReport"]: | ||||||
| """Creates a quality report with aggregated quality score from selected metrics. | ||||||
|
|
||||||
| Separates metrics into quality metrics (used for quality score calculation) and | ||||||
| extra metrics (evaluated but not included in quality score). | ||||||
|
|
||||||
| Args: | ||||||
| by_metric: Dictionary mapping metric identifiers to their computed statistics. | ||||||
| quality_score_metrics: Metric identifiers to include in quality score calculation. | ||||||
| All specified metrics must exist in by_metric. | ||||||
|
|
||||||
| Returns: | ||||||
| QualityReport with aggregated quality score and separated quality/extra metrics, | ||||||
| or None if all quality score metrics have zero samples. | ||||||
|
|
||||||
| Raises: | ||||||
| ValueError: If any quality_score_metrics are not found in by_metric. | ||||||
| """ | ||||||
| warnings: list[str] = [] | ||||||
|
|
||||||
| # Validate all quality score metrics exist in computed metrics (by_metric) | ||||||
| missing_metrics = [m for m in quality_score_metrics if m not in by_metric] | ||||||
| if missing_metrics: | ||||||
| warning_msg = ( | ||||||
| "WARNING: " | ||||||
| f"Quality score metrics {missing_metrics} were excluded from " | ||||||
| "quality score computation. " | ||||||
| f"Reason: Not found in evaluation results." | ||||||
| ) | ||||||
| warnings.append(warning_msg) | ||||||
| logger.warning(warning_msg) | ||||||
|
|
||||||
| quality_score_metrics = list( | ||||||
| set(quality_score_metrics) - set(missing_metrics) | ||||||
| ) | ||||||
|
|
||||||
| # Calculate total samples from quality score metrics only | ||||||
| total_samples = 0 | ||||||
| for metric_id in quality_score_metrics: | ||||||
| score_stats = by_metric[metric_id].score_statistics | ||||||
| if score_stats is not None: | ||||||
| total_samples += score_stats.count | ||||||
| if total_samples == 0: | ||||||
| logger.warning( | ||||||
| "CRITICAL: Quality score computation failed. " | ||||||
| "All configured quality metrics have zero evaluation results." | ||||||
| ) | ||||||
| return None | ||||||
|
|
||||||
| quality_metrics: dict[str, QualityMetricResult] = {} | ||||||
| extra_metrics: dict[str, ScoreStatistics] = {} | ||||||
|
|
||||||
| # Separate quality metrics from extra metrics | ||||||
| for metric_id in by_metric: | ||||||
| if metric_id in quality_score_metrics: | ||||||
| score_stats = by_metric[metric_id].score_statistics | ||||||
|
|
||||||
| # Skip if score_statistics is None | ||||||
| if score_stats is None: | ||||||
| warning_msg = ( | ||||||
| f"WARNING: Quality score metric '{metric_id}' " | ||||||
| "was excluded from quality score computation. " | ||||||
| "Reason: Missing score statistics data." | ||||||
| ) | ||||||
| warnings.append(warning_msg) | ||||||
| logger.warning(warning_msg) | ||||||
| continue | ||||||
|
xmican10 marked this conversation as resolved.
|
||||||
|
|
||||||
| sample_size = score_stats.count | ||||||
|
|
||||||
| # Skip metrics with zero samples | ||||||
| if sample_size == 0: | ||||||
| warning_msg = ( | ||||||
| f"WARNING: Quality score metric '{metric_id}' " | ||||||
| "was excluded from quality score computation. " | ||||||
| "Reason: Zero evaluation results for this metric." | ||||||
| ) | ||||||
| warnings.append(warning_msg) | ||||||
| logger.warning(warning_msg) | ||||||
| continue | ||||||
|
|
||||||
| weight = sample_size / total_samples | ||||||
|
|
||||||
| quality_metrics[metric_id] = QualityMetricResult( | ||||||
| statistics=score_stats, | ||||||
| weight=weight, | ||||||
| ) | ||||||
| else: | ||||||
| stats = by_metric[metric_id].score_statistics | ||||||
| if stats is not None: | ||||||
| extra_metrics[metric_id] = stats | ||||||
|
|
||||||
| # Calculate aggregated quality score | ||||||
| aggregated_score = QualityReport._calculate_quality_score(quality_metrics) | ||||||
|
|
||||||
| return QualityReport( | ||||||
| aggregated_quality_score=aggregated_score, | ||||||
| quality_metrics=quality_metrics, | ||||||
| extra_metrics=extra_metrics, | ||||||
| warnings=warnings, | ||||||
| ) | ||||||
|
|
||||||
| @staticmethod | ||||||
| def _calculate_quality_score( | ||||||
| quality_metrics: dict[str, QualityMetricResult], | ||||||
| ) -> float: | ||||||
| """Calculate weighted average quality score from quality metrics. | ||||||
|
|
||||||
| Computes a weighted average where each metric's weight is proportional to its | ||||||
| sample size relative to the total samples across all quality metrics. | ||||||
|
|
||||||
| Args: | ||||||
| quality_metrics: Dictionary of quality metric results with statistics and | ||||||
| weights. Each metric contains statistics with a mean score and a weight | ||||||
| (sample_size / total_samples). | ||||||
|
|
||||||
| Returns: | ||||||
| Weighted average quality score computed as sum of (mean * weight) for all metrics. | ||||||
| """ | ||||||
| weighted_sum = 0.0 | ||||||
| for metric in quality_metrics.values(): | ||||||
| weighted_sum += metric.statistics.mean * metric.weight | ||||||
| return weighted_sum | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.