Repository navigation
Expand file tree
/
Copy pathbase.py
More file actions
65 lines (53 loc) · 2.03 KB
/
Copy pathbase.py
File metadata and controls
65 lines (53 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""BaseMetric abstract class."""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING
from minival.types import TestCase, TestCaseParams
if TYPE_CHECKING:
from minival.llm.base import BaseLLM
class BaseMetric(ABC):
name: str
score: float | None = None
reason: str | None = None
success: bool | None = None
def __init__(
self,
threshold: float = 0.5,
model: str | "BaseLLM" = "claude-sonnet-4-6",
include_reason: bool = True,
strict_mode: bool = False,
verbose_mode: bool = False,
):
from minival.llm import initialize_model
self.threshold = 1.0 if strict_mode else threshold
self.model = initialize_model(model)
self.include_reason = include_reason
self.strict_mode = strict_mode
self.verbose_mode = verbose_mode
@abstractmethod
def measure(self, test_case: TestCase) -> float:
"""Run evaluation synchronously and return score (0.0–1.0)."""
...
@abstractmethod
async def a_measure(self, test_case: TestCase) -> float:
"""Run evaluation asynchronously and return score (0.0–1.0)."""
...
@property
def required_params(self) -> list[TestCaseParams]:
"""Declare which TestCase fields this metric requires."""
return []
def is_successful(self) -> bool:
if self.score is None:
raise RuntimeError("measure() or a_measure() must be called first.")
if self.strict_mode:
return self.score == 1.0
return self.score >= self.threshold
def _validate_test_case(self, test_case: TestCase) -> None:
for param in self.required_params:
value = getattr(test_case, param.value)
if value is None:
case_name = test_case.name or "(unnamed)"
raise ValueError(
f"Metric '{self.name}' requires field '{param.value}' "
f"but it is None on test case '{case_name}'."
)