File size: 8,114 Bytes
1a48d6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from __future__ import annotations
from typing import TYPE_CHECKING
from . import media_state_prompt, po_image_supplement, text_ar, text_list_completion
from . import grounding, identifier_spelling
from .backend import ClairicFlashModel
from .media import media_warmup_groups

if TYPE_CHECKING:
    from typing import Any
    from .api import PredictRequest
    from .backend import EncodedRequest


class Clairic27BModel(ClairicFlashModel):
    RELEASE_MODEL_ID = "clairic-27b"
    COMPACT_LIST_PROMPTS = False
    DATE_ROLE_GUIDANCE = False
    SOURCE_ROW_ELIGIBILITY = True
    FINANCIAL_SERVICE_GUIDANCE = False
    FINANCIAL_FEE_GUIDANCE = True
    SPELL_VISUAL_PO_REFERENCES = True
    FOCUS_SPARSE_IMAGES = False
    PRESERVE_IMAGE_ENTITY_NAMES = True
    FOCUS_IMAGE_PARTY_NAMES = False
    LITERAL_IMAGE_PAYMENT_ROWS = True
    BIND_IMAGE_FINANCIAL_LINES = False
    IMAGE_TABLE_ROW_SCALE = 4
    BIND_IMAGE_RAGGED_ROWS = False

    def encode(self, request: PredictRequest, **options: Any) -> EncodedRequest:
        supplement_disabled = options.pop("po_supplement_disabled", False)
        encoded = self._encode_without_po_supplement(request, **options)
        detail = any(
            (
                options.get(name)
                for name in (
                    "identifier_view",
                    "party_name_view",
                    "physical_row_view",
                    "identifier_spelling_disabled",
                    "pdf_spelling_view",
                    "po_detail_view",
                )
            )
        )
        if (
            type(self) is not Clairic27BModel
            or supplement_disabled
            or detail
            or (not po_image_supplement.selected_fields(request, encoded))
        ):
            return encoded
        if (
            self.settings.model_id != "clairic-27b"
            or self.max_concurrency != self.settings.max_concurrency
            or self.max_concurrency != 1
        ):
            return encoded
        return po_image_supplement.prepare(
            request,
            encoded,
            encode_child=lambda child: self.encode(child, po_supplement_disabled=True),
            tokenizer=self.tokenizer,
            max_length=self.settings.max_length,
            media_groups=media_warmup_groups,
        )

    def _encode_without_po_supplement(
        self, request: PredictRequest, **options: Any
    ) -> EncodedRequest:
        detail = any(
            (
                options.get(name)
                for name in (
                    "identifier_view",
                    "party_name_view",
                    "physical_row_view",
                    "identifier_spelling_disabled",
                    "pdf_spelling_view",
                    "po_detail_view",
                )
            )
        )
        if type(self) is Clairic27BModel and options.get("po_detail_view"):
            options = {**options, "pdf_spelling_view": True}
        encoded = super().encode(request, **options)
        if type(self) is not Clairic27BModel or detail:
            return encoded
        (record, forecasts, tasks, typed, eos) = encoded
        active = {name for (name, _, _) in tasks}
        contracts = record.get("_meta", {}).get("identifier_spelling", {})
        po_fields = {
            name: request.fields[name]
            for (name, contract) in contracts.items()
            if name in active
            and contract.get("wire") == identifier_spelling.VERSION
            and (len(request.images) in (1, 2))
            and (not request.documents)
        }
        number_fields = {
            name: spec
            for (name, spec) in request.fields.items()
            if name in active
            and spec.get("type") == "number"
            and (spec.get("source_only") is True)
            and (not spec.get("knowledge"))
            and (not grounding.state_only(spec))
            and (len(request.images) > 1 or request.documents)
        }
        selected = {**po_fields, **number_fields}
        if not selected:
            return encoded
        originals = record.get("_original_images", ())
        if len(originals) < len(request.images) or (
            not request.documents and len(originals) != len(request.images)
        ):
            raise ValueError(
                "27B scalar detail must retain every original image in order"
            )
        (child, _, replacements, child_typed, child_eos) = super().encode(
            request.model_copy(update={"fields": selected}),
            identifier_view=True,
            pdf_spelling_view=True,
            image_views=lambda: originals,
            image_view_key="27b-literal-scalars-all-content2-v1",
        )
        if child_typed is not None or child_eos != eos:
            raise ValueError(
                "27B scalar detail must retain the original scalar AR contract"
            )
        by_name = {task[0]: task for task in replacements}
        if set(by_name) != set(selected) or len(replacements) != len(selected):
            raise ValueError(
                "27B scalar detail tasks differ from selected public fields"
            )
        updated = [by_name.get(task[0], task) for task in tasks]
        salts = dict(record.get("_ar_task_salts", {}))
        for name in selected:
            record["fields"][name] = child["fields"][name]
            record["_ar_constraints"][name] = child["_ar_constraints"][name]
            salts[name] = child["_cache_salt"]
        record["_meta"]["model_document_scalar_view"] = {
            "version": "27b-literal-scalars-all-content2-v1",
            "fields": list(selected),
            "po_fields": list(po_fields),
            "number_fields": list(number_fields),
            "image_count": len(originals),
        }
        record.update(
            _ar_tasks=updated,
            _ar_task_salts=salts,
            _ar_prefix=[],
            _ar_group_prefixes=[],
        )
        record["_ar_media_groups"] = media_warmup_groups(
            updated,
            default_salt=record["_cache_salt"],
            task_salts=salts,
            annotations=record["_meta"]["extraction_contract"]["fields"],
        )
        return (record, forecasts, updated, typed, eos)

    def _encode_request(self, request: PredictRequest) -> EncodedRequest:
        encoded = super()._encode_request(request)
        if (
            type(self) is Clairic27BModel
            and self.settings.model_id == "clairic-27b"
            and (self.max_concurrency == self.settings.max_concurrency == 1)
            and bool(
                getattr(request, "images", ()) or getattr(request, "documents", ())
            )
        ):
            encoded[0]["_first_item_list_gating"] = True
            fallback = encoded[0].get("_identifier_spelling_fallback")
            if fallback is not None:
                fallback["_first_item_list_gating"] = True
            encoded = media_state_prompt.prepare(
                request,
                encoded,
                tokenizer=self.tokenizer,
                max_length=self.settings.max_length,
                media_groups=media_warmup_groups,
            )
        return encoded

    async def _ar_all(
        self, record: dict[str, Any], eos: int
    ) -> dict[str, dict[str, Any]]:
        caller_record = record
        if len(record["_ar_tasks"]) < 64 and (not record.get("_media_payload")):
            record = {**record, "_ar_prefix": None, "_ar_group_prefixes": []}
        if (
            type(self) is Clairic27BModel
            and self.settings.model_id == "clairic-27b"
            and (self.max_concurrency == self.settings.max_concurrency == 1)
            and text_list_completion.eligible(record, record["_ar_tasks"])
        ):
            predictions = await text_ar.text_ar_all(self, record, eos)
            if record is not caller_record:
                caller_record["_unexecuted_list_items"] = record[
                    "_unexecuted_list_items"
                ]
            return predictions
        return await super()._ar_all(record, eos)