File size: 6,086 Bytes
4949db9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""Validated training sample type.

A TrainingSample that exists is valid — all invariants are checked at
construction time.  Downstream code can serialize without re-checking.
"""

from __future__ import annotations

import json
import os
from dataclasses import dataclass
from typing import Any

from evals.prompts import GENERAL_DIMS
from evals.physics_criteria import ALL_LAWS

ALLOWED_KEYS: frozenset[str] = frozenset(GENERAL_DIMS) | frozenset(ALL_LAWS)


def _resolve_path(video_path: str, base_dir: str | None) -> str:
    if base_dir and not os.path.isabs(video_path):
        video_path = os.path.join(base_dir, video_path)
    return os.path.normpath(video_path)


@dataclass(frozen=True)
class TrainingSample:
    system: str
    user: str
    assistant: str
    video_path: str

    def __post_init__(self) -> None:
        errors: list[str] = []
        if not self.system:
            errors.append("empty system prompt")
        if not self.user:
            errors.append("empty user prompt")
        if not self.assistant:
            errors.append("empty assistant response")
        video_count = self.user.count("<video>") if self.user else 0
        if self.user and video_count != 1:
            errors.append(
                f"user must have exactly 1 <video>, got {video_count}"
            )
        if not self.video_path:
            errors.append("empty video_path")
        elif not os.path.isfile(self.video_path):
            errors.append(f"video not found: {self.video_path}")
        if errors:
            raise ValueError("; ".join(errors))

    # ── serialization ────────────────────────────────────────────────

    def to_swift_dict(self) -> dict[str, Any]:
        return {
            "messages": [
                {"role": "system", "content": self.system},
                {"role": "user", "content": self.user},
                {"role": "assistant", "content": self.assistant},
            ],
            "videos": [self.video_path],
        }

    def to_jsonl(self) -> str:
        return json.dumps(self.to_swift_dict(), ensure_ascii=False)

    # ── factory methods ──────────────────────────────────────────────

    @classmethod
    def json_only(
        cls,
        *,
        system: str,
        user: str,
        video_path: str,
        key: str,
        score: int,
        base_dir: str | None = None,
    ) -> TrainingSample:
        """Single-key JSON target, no CoT."""
        if key not in ALLOWED_KEYS:
            raise ValueError(f"unknown key: {key}")
        if not isinstance(score, int) or not 1 <= score <= 5:
            raise ValueError(f"score must be int 1-5, got {score!r}")
        if not user.startswith("<video>"):
            user = "<video>" + user
        assistant = json.dumps({key: score}, ensure_ascii=False)
        return cls(
            system=system,
            user=user,
            assistant=assistant,
            video_path=_resolve_path(video_path, base_dir),
        )

    @classmethod
    def cot(
        cls,
        *,
        system: str,
        user: str,
        video_path: str,
        key: str,
        score: int,
        reasoning: str,
        base_dir: str | None = None,
    ) -> TrainingSample:
        """JSON target with reasoning, for CoT distillation."""
        if key not in ALLOWED_KEYS:
            raise ValueError(f"unknown key: {key}")
        if not isinstance(score, int) or not 1 <= score <= 5:
            raise ValueError(f"score must be int 1-5, got {score!r}")
        if not reasoning:
            raise ValueError("reasoning must be non-empty for CoT samples")
        if not user.startswith("<video>"):
            user = "<video>" + user
        assistant = json.dumps(
            {"reasoning": reasoning, key: score}, ensure_ascii=False,
        )
        return cls(
            system=system,
            user=user,
            assistant=assistant,
            video_path=_resolve_path(video_path, base_dir),
        )

    @classmethod
    def from_jsonl_row(
        cls,
        row: dict[str, Any],
    ) -> TrainingSample:
        """Parse and validate from a JSONL row dict (json_only format)."""
        messages = row.get("messages")
        if not messages or not isinstance(messages, list) or len(messages) < 2:
            raise ValueError("missing or invalid 'messages'")

        by_role: dict[str, str] = {}
        for m in messages:
            role = m.get("role")
            if role:
                by_role[role] = m.get("content", "")

        if "user" not in by_role or "assistant" not in by_role:
            raise ValueError("missing user or assistant role")
        if "system" not in by_role:
            raise ValueError("missing system role")

        system = by_role["system"]
        user = by_role["user"]
        assistant = by_role["assistant"]

        videos = row.get("videos", [])
        if not videos:
            raise ValueError("missing videos array")
        if len(videos) != 1:
            raise ValueError(f"videos must have length 1, got {len(videos)}")

        if "<video>" in assistant or "<think>" in assistant:
            raise ValueError("assistant contains forbidden tags")
        try:
            parsed = json.loads(assistant)
        except json.JSONDecodeError:
            raise ValueError("assistant is not valid JSON")
        if not isinstance(parsed, dict):
            raise ValueError("assistant must be a JSON object")
        score_keys = {k for k in parsed if k in ALLOWED_KEYS}
        if len(score_keys) != 1:
            raise ValueError(f"assistant must have exactly 1 score key, got {score_keys}")
        k = next(iter(score_keys))
        v = parsed[k]
        if not isinstance(v, int) or not 1 <= v <= 5:
            raise ValueError(f"score must be int 1-5, got {v!r}")

        return cls(system=system, user=user, assistant=assistant, video_path=videos[0])