-
Notifications
You must be signed in to change notification settings - Fork 11
Add metric versionning #113
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7d18efe
Stamp turn_taking MetricScore outputs with a version string
gabegma 006cc8c
Auto-stamp version + prompt_hash on every MetricScore
gabegma 6d66128
Remove duplicated code in `get_prompt()`
JosephMarinier bd30a7c
Remove unnecessary default value
JosephMarinier 1b3602d
Move src/eva/models/versioning.py to src/eva/metrics/
JosephMarinier 0cb0f5e
Regenerate metric signatures in pre-commit
JosephMarinier c692de9
Avoid circular import
JosephMarinier 1d8a8d7
Specify `uv run` in `pre-commit`
JosephMarinier 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #!/usr/bin/env python3 | ||
| """Regenerate tests/fixtures/metric_signatures.json. | ||
|
|
||
| Run this after intentionally changing a metric's logic and bumping its | ||
| `version` class attribute (or after editing its judge prompt template). | ||
| The drift test (tests/unit/metrics/test_metric_signatures.py) compares | ||
| the current state against this fixture and fails on any unintended drift. | ||
|
|
||
| Usage: | ||
| python scripts/regen_metric_signatures.py | ||
| """ | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
|
|
||
| from eva.metrics.signatures import compute_all_metric_signatures | ||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parent.parent | ||
| FIXTURE_PATH = REPO_ROOT / "tests" / "fixtures" / "metric_signatures.json" | ||
|
|
||
|
|
||
| def main() -> None: | ||
| signatures = compute_all_metric_signatures() | ||
| FIXTURE_PATH.parent.mkdir(parents=True, exist_ok=True) | ||
| FIXTURE_PATH.write_text(json.dumps(signatures, indent=2, sort_keys=True) + "\n") | ||
| print(f"Wrote {len(signatures)} metric signatures to {FIXTURE_PATH.relative_to(REPO_ROOT)}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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
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
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
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
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
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,78 @@ | ||
| """Compute drift signatures for metric classes. | ||
|
|
||
| A metric's "signature" captures everything we want to detect changes to: | ||
| - `version`: the manually-bumped string on the class | ||
| - `source_hash`: sha256[:12] of `inspect.getsource(cls)` (class body) | ||
| - `prompt_hash`: sha256[:12] of the unrendered judge prompt template, or | ||
| None for non-judge metrics | ||
|
|
||
| The drift test compares the current signatures against a checked-in fixture | ||
| and fails if anything changed without an explicit version bump + fixture regen. | ||
| """ | ||
|
|
||
| import hashlib | ||
| import inspect | ||
|
|
||
| # Importing the metric subpackages forces all concrete metric classes to be | ||
| # registered as BaseMetric subclasses, so walking __subclasses__ finds them. | ||
| import eva.metrics.accuracy # noqa: F401 | ||
| import eva.metrics.diagnostic # noqa: F401 | ||
| import eva.metrics.experience # noqa: F401 | ||
| import eva.metrics.validation # noqa: F401 | ||
| from eva.metrics.base import AudioJudgeMetric, BaseMetric, TextJudgeMetric | ||
| from eva.metrics.versioning import hash_prompt_template | ||
| from eva.utils.prompt_manager import get_prompt_manager | ||
|
|
||
|
|
||
| def _all_concrete_versioned_metric_classes() -> dict[str, type[BaseMetric]]: | ||
| """Walk BaseMetric subclasses; return concrete classes that set a version. | ||
|
|
||
| Keyed on class qualname so co-named classes (e.g., the cascade vs S2S | ||
| variants of `agent_speech_fidelity`) get distinct entries. | ||
| """ | ||
| result: dict[str, type[BaseMetric]] = {} | ||
|
|
||
| def walk(cls: type) -> None: | ||
| for sub in cls.__subclasses__(): | ||
| walk(sub) | ||
| if inspect.isabstract(sub): | ||
| continue | ||
| # `version` is None on BaseMetric; only concrete classes that | ||
| # deliberately set it are participating. | ||
| if getattr(sub, "version", None) is None: | ||
| continue | ||
| result[sub.__qualname__] = sub | ||
|
|
||
| walk(BaseMetric) | ||
| return result | ||
|
|
||
|
|
||
| def _source_hash(cls: type) -> str: | ||
| """sha256[:12] of the class body source.""" | ||
| return hashlib.sha256(inspect.getsource(cls).encode("utf-8")).hexdigest()[:12] | ||
|
|
||
|
|
||
| def _prompt_hash_for_metric(cls: type[BaseMetric]) -> str | None: | ||
| """Return the prompt template hash for judge metrics, or None. | ||
|
|
||
| All judge metrics in this codebase use `judge.{name}.user_prompt`. | ||
| A judge metric without a corresponding template raises KeyError — | ||
| that's a configuration bug we want surfaced. | ||
| """ | ||
| if not issubclass(cls, TextJudgeMetric | AudioJudgeMetric): | ||
| return None | ||
| template = get_prompt_manager().get_template(f"judge.{cls.name}.user_prompt") | ||
| return hash_prompt_template(template) | ||
|
|
||
|
|
||
| def compute_all_metric_signatures() -> dict[str, dict[str, str | None]]: | ||
| """Return {class_qualname: {version, source_hash, prompt_hash}} for every concrete metric.""" | ||
| out: dict[str, dict[str, str | None]] = {} | ||
| for qualname, cls in _all_concrete_versioned_metric_classes().items(): | ||
| out[qualname] = { | ||
| "name": cls.name, | ||
| "version": cls.version, | ||
| "source_hash": _source_hash(cls), | ||
| "prompt_hash": _prompt_hash_for_metric(cls), | ||
| } | ||
| return out |
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
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,24 @@ | ||
| """Per-record version + prompt-hash stamping for MetricScore. | ||
|
|
||
| MetricsRunner sets these contextvars around every metric.compute() call. | ||
| The MetricScore Pydantic model has a model_validator that reads them and | ||
| auto-fills the version/prompt_hash fields when unset, so all scores and | ||
| sub-scores built inside that compute() inherit the right values without | ||
| each call site having to thread them through explicitly. | ||
|
|
||
| Both contextvars default to None, which means "not currently inside a | ||
| metric compute() call" — that's the state during JSON deserialization | ||
| (loading metrics.json from disk), so existing on-disk values are | ||
| preserved instead of being overwritten with None. | ||
| """ | ||
|
|
||
| import hashlib | ||
| from contextvars import ContextVar | ||
|
|
||
| _CURRENT_METRIC_VERSION: ContextVar[str | None] = ContextVar("current_metric_version", default=None) | ||
| _CURRENT_PROMPT_HASH: ContextVar[str | None] = ContextVar("current_prompt_hash", default=None) | ||
|
|
||
|
|
||
| def hash_prompt_template(template: str) -> str: | ||
| """Return sha256[:12] of an unrendered prompt template string.""" | ||
| return hashlib.sha256(template.encode()).hexdigest()[:12] |
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.
Uh oh!
There was an error while loading. Please reload this page.