feat(scoring): add ergonomics helpers for TemplateMetric authoring#931
Closed
wprazuch wants to merge 1 commit into
Closed
feat(scoring): add ergonomics helpers for TemplateMetric authoring#931wprazuch wants to merge 1 commit into
wprazuch wants to merge 1 commit into
Conversation
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
a394a42 to
c89d9b9
Compare
Completes the ~20-30 LOC authoring promise by giving TemplateMetric
subclasses ready-made helpers. Stacked on wprazuch/metric-abstractions.
New:
- TemplateMetric._render(template, input) — Jinja2 rendering with both
NEL-native ({{ response }}, {{ target }}, {{ metadata.* }}) and
SDK-native ({{ output_text }}, {{ reference }}) variable names, so
templates authored against either vocabulary work unchanged. Strict
undefined-variable handling raises instead of silently rendering empty.
- CorpusTemplateMetric(TemplateMetric) — base class for metrics with
both row-level and corpus-level scores. Subclasses implement _score()
and _corpus_score(); defaults wrap each in a MetricResult. score_names()
includes both '<type>' and '<type>_corpus'. Empty inputs -> None.
- SecretsMixin — mixin that satisfies MetricWithSecrets protocol.
Subclasses declare secret_env_vars: ClassVar[tuple[str, ...]]. Secrets
are eagerly loaded from os.environ at construction, with async
resolve_secrets() as a fallback path (NMP Platform flow). Resolved
values are stored as SecretStr private attrs; get_secret(env_var)
returns the plaintext value or None.
Tests (+18 new, 42 total):
- _render: NEL-native names, SDK-native aliases, metadata/config access,
StrictUndefined raises on missing variables.
- CorpusTemplateMetric: satisfies both Metric + CorpusMetric protocols,
row-level default, corpus-level default, empty-inputs, score_names
includes both.
- SecretsMixin: satisfies MetricWithSecrets, declares env vars, reads
env at construction, async resolver fills gaps, resolver is skipped
when already loaded.
- Target ergonomics proof: _TinyLengthMetric in ~15 LOC of user code
demonstrates the authoring pattern.
NMP can pick these up as they land. The contract API is stable — helpers
are additive (subclassing-based), so SDK concrete metrics can adopt them
incrementally without breakage.
Signed-off-by: Wojciech Prazuch <wprazuch@nvidia.com>
e5f90a3 to
78bccb8
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Completes the ~20-30 LOC authoring promise by giving TemplateMetric subclasses ready-made helpers. Stacked on wprazuch/metric-abstractions.
New:
TemplateMetric._render(template, input) — Jinja2 rendering with both NEL-native ({{ response }}, {{ target }}, {{ metadata.* }}) and SDK-native ({{ output_text }}, {{ reference }}) variable names, so templates authored against either vocabulary work unchanged. Strict undefined-variable handling raises instead of silently rendering empty.
CorpusTemplateMetric(TemplateMetric) — base class for metrics with both row-level and corpus-level scores. Subclasses implement _score() and _corpus_score(); defaults wrap each in a MetricResult. score_names() includes both '' and '_corpus'. Empty inputs -> None.
SecretsMixin — mixin that satisfies MetricWithSecrets protocol. Subclasses declare secret_env_vars: ClassVar[tuple[str, ...]]. Secrets are eagerly loaded from os.environ at construction, with async resolve_secrets() as a fallback path (NMP Platform flow). Resolved values are stored as SecretStr private attrs; get_secret(env_var) returns the plaintext value or None.
Tests (+18 new, 42 total):
NMP can pick these up as they land. The contract API is stable — helpers are additive (subclassing-based), so SDK concrete metrics can adopt them incrementally without breakage.