|
| 1 | +"""Data loader abstractions""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections.abc import Sequence |
| 6 | +from typing import Any, Callable |
| 7 | +from typing_extensions import Protocol |
| 8 | + |
| 9 | +from pydantic import BaseModel, Field |
| 10 | + |
| 11 | +from eval_protocol.models import EvaluationRow |
| 12 | +from eval_protocol.pytest.types import EvaluationTestMode |
| 13 | +from eval_protocol.dataset_logger.dataset_logger import DatasetLogger |
| 14 | + |
| 15 | + |
| 16 | +class DataLoaderContext(BaseModel): |
| 17 | + """Context provided to loader variants when materializing data.""" |
| 18 | + |
| 19 | + max_rows: int | None = Field(default=None, ge=1, description="Maximum number of rows to load") |
| 20 | + preprocess_fn: Callable[[list[EvaluationRow]], list[EvaluationRow]] | None = Field( |
| 21 | + default=None, description="Optional preprocessing function for evaluation rows" |
| 22 | + ) |
| 23 | + logger: DatasetLogger = Field(description="Dataset logger for tracking operations") |
| 24 | + invocation_id: str = Field(description="Unique identifier for this invocation") |
| 25 | + experiment_id: str = Field(description="Unique identifier for this experiment") |
| 26 | + mode: EvaluationTestMode = Field(description="The evaluation test mode") |
| 27 | + |
| 28 | + class Config: |
| 29 | + arbitrary_types_allowed = True # For Callable and DatasetLogger types |
| 30 | + |
| 31 | + |
| 32 | +class DataLoaderResult(BaseModel): |
| 33 | + """Rows and metadata returned by a loader variant.""" |
| 34 | + |
| 35 | + rows: list[EvaluationRow] = Field(description="List of evaluation rows loaded") |
| 36 | + source_id: str = Field(description="Unique identifier for the data source") |
| 37 | + source_metadata: dict[str, Any] = Field( |
| 38 | + default_factory=dict, description="Additional metadata about the data source" |
| 39 | + ) |
| 40 | + raw_payload: Any | None = Field(default=None, description="Raw payload data if available") |
| 41 | + preprocessed: bool = Field(default=False, description="Whether the data has been preprocessed") |
| 42 | + |
| 43 | + class Config: |
| 44 | + arbitrary_types_allowed = True # For Any type in raw_payload |
| 45 | + |
| 46 | + |
| 47 | +class DataLoaderVariant(BaseModel): |
| 48 | + """Single parameterizable variant from a data loader.""" |
| 49 | + |
| 50 | + id: str = Field(description="Unique identifier for this variant") |
| 51 | + description: str = Field(description="Human-readable description of this variant") |
| 52 | + loader: Callable[[DataLoaderContext], DataLoaderResult] = Field( |
| 53 | + description="Function that loads data for this variant" |
| 54 | + ) |
| 55 | + metadata: dict[str, Any] = Field(default_factory=dict, description="Additional metadata for this variant") |
| 56 | + |
| 57 | + class Config: |
| 58 | + arbitrary_types_allowed = True # For Callable type |
| 59 | + |
| 60 | + def load(self, ctx: DataLoaderContext) -> DataLoaderResult: |
| 61 | + """Load a dataset for this variant using the provided context.""" |
| 62 | + |
| 63 | + return self.loader(ctx) |
| 64 | + |
| 65 | + |
| 66 | +class EvaluationDataLoader(Protocol): |
| 67 | + """Protocol for data loaders that can be consumed by ``evaluation_test``.""" |
| 68 | + |
| 69 | + def variants(self) -> Sequence[DataLoaderVariant]: |
| 70 | + """Return parameterizable variants emitted by this loader.""" |
| 71 | + ... |
0 commit comments