seldon-runtime 0.1.1.dev3__py3-none-any.whl → 0.1.1.dev4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- seldon_runtime/__init__.py +1 -1
- seldon_runtime/artifact.py +26 -11
- seldon_runtime/components.py +112 -1
- seldon_runtime/conditional.py +2 -0
- seldon_runtime/contracts.py +72 -1
- seldon_runtime/executor.py +27 -13
- seldon_runtime/program_validation.py +2 -0
- seldon_runtime/semantic.py +140 -0
- seldon_runtime/span_review.py +339 -0
- {seldon_runtime-0.1.1.dev3.dist-info → seldon_runtime-0.1.1.dev4.dist-info}/METADATA +47 -7
- {seldon_runtime-0.1.1.dev3.dist-info → seldon_runtime-0.1.1.dev4.dist-info}/RECORD +16 -15
- {seldon_runtime-0.1.1.dev3.dist-info → seldon_runtime-0.1.1.dev4.dist-info}/WHEEL +0 -0
- {seldon_runtime-0.1.1.dev3.dist-info → seldon_runtime-0.1.1.dev4.dist-info}/licenses/LICENSE +0 -0
- {seldon_runtime-0.1.1.dev3.dist-info → seldon_runtime-0.1.1.dev4.dist-info}/licenses/NOTICE +0 -0
- {seldon_runtime-0.1.1.dev3.dist-info → seldon_runtime-0.1.1.dev4.dist-info}/licenses/THIRD_PARTY_NOTICES.md +0 -0
- {seldon_runtime-0.1.1.dev3.dist-info → seldon_runtime-0.1.1.dev4.dist-info}/top_level.txt +0 -0
seldon_runtime/__init__.py
CHANGED
seldon_runtime/artifact.py
CHANGED
|
@@ -27,7 +27,7 @@ from .program_validation import validate_program
|
|
|
27
27
|
from .provider_error import ProviderError
|
|
28
28
|
|
|
29
29
|
MAX_ARTIFACT_BYTES = 2 * 1024 * 1024
|
|
30
|
-
RUNTIME_VERSION = "0.1.1.
|
|
30
|
+
RUNTIME_VERSION = "0.1.1.dev4"
|
|
31
31
|
_SHA256 = r"^[0-9a-f]{64}$"
|
|
32
32
|
Capability = Literal["chat", "structured_output", "tools", "choice", "extract"]
|
|
33
33
|
OptimizerContract = Literal[
|
|
@@ -87,7 +87,7 @@ class ArtifactManifest(_Data):
|
|
|
87
87
|
compiler_version: str = Field(min_length=1, max_length=64, pattern=r"^[a-zA-Z0-9.+_-]+$")
|
|
88
88
|
# Explicit reviewed reader compatibility, never a version-prefix/range guess.
|
|
89
89
|
# dev3 adds explicit native bindings; older artifacts retain their restrictions.
|
|
90
|
-
runtime_version: Literal["0.1.1.dev1", "0.1.1.dev2", "0.1.1.dev3"]
|
|
90
|
+
runtime_version: Literal["0.1.1.dev1", "0.1.1.dev2", "0.1.1.dev3", "0.1.1.dev4"]
|
|
91
91
|
optimizer_contract_version: OptimizerContract
|
|
92
92
|
instruction_envelope: Literal["instruction-envelope-v1"] | None
|
|
93
93
|
conditional: bool = Field(strict=True)
|
|
@@ -164,7 +164,12 @@ def _parse(data: bytes) -> ArtifactManifest:
|
|
|
164
164
|
raise ValueError
|
|
165
165
|
manifest = ArtifactManifest.model_validate(raw)
|
|
166
166
|
native = [step for step in manifest.plan.steps if step.op in {"choice", "extract"}]
|
|
167
|
-
if native and manifest.runtime_version
|
|
167
|
+
if native and manifest.runtime_version not in {"0.1.1.dev3", "0.1.1.dev4"}:
|
|
168
|
+
raise ValueError
|
|
169
|
+
if (
|
|
170
|
+
manifest.spec.reference_program is not None
|
|
171
|
+
and manifest.runtime_version != RUNTIME_VERSION
|
|
172
|
+
):
|
|
168
173
|
raise ValueError
|
|
169
174
|
if (
|
|
170
175
|
digest({k: v for k, v in raw.items() if k != "manifest_digest"})
|
|
@@ -178,16 +183,26 @@ def _parse(data: bytes) -> ArtifactManifest:
|
|
|
178
183
|
raise ValueError
|
|
179
184
|
plan = manifest.plan
|
|
180
185
|
contract = manifest.optimizer_contract_version
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
if
|
|
187
|
-
|
|
188
|
-
or contract in {f"optimizer-correctness-v{i}" for i in range(1, 5)}
|
|
186
|
+
checked_plans = [plan]
|
|
187
|
+
if manifest.spec.reference_program is not None:
|
|
188
|
+
checked_plans.append(manifest.spec.reference_program)
|
|
189
|
+
from .semantic import SPAN_REVIEW_OPERATORS
|
|
190
|
+
|
|
191
|
+
if manifest.runtime_version != RUNTIME_VERSION and any(
|
|
192
|
+
step.op in SPAN_REVIEW_OPERATORS for checked in checked_plans for step in checked.steps
|
|
189
193
|
):
|
|
190
194
|
raise ValueError
|
|
195
|
+
for checked in checked_plans:
|
|
196
|
+
if checked.version == "function-plan-v4" and contract in {
|
|
197
|
+
"optimizer-correctness-v1",
|
|
198
|
+
"optimizer-correctness-v2",
|
|
199
|
+
}:
|
|
200
|
+
raise ValueError
|
|
201
|
+
if checked.version == "function-plan-v5" and (
|
|
202
|
+
not manifest.conditional
|
|
203
|
+
or contract in {f"optimizer-correctness-v{i}" for i in range(1, 5)}
|
|
204
|
+
):
|
|
205
|
+
raise ValueError
|
|
191
206
|
validate_program(plan, manifest.spec)
|
|
192
207
|
required = _required_capabilities(plan, manifest.spec)
|
|
193
208
|
if set(manifest.models) != set(required):
|
seldon_runtime/components.py
CHANGED
|
@@ -45,7 +45,7 @@ def collection(properties, maximum, required=None):
|
|
|
45
45
|
"items": {
|
|
46
46
|
"type": "object",
|
|
47
47
|
"properties": properties,
|
|
48
|
-
"required":
|
|
48
|
+
"required": list(properties) if required is None else required,
|
|
49
49
|
},
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -84,7 +84,106 @@ LINKS = collection(
|
|
|
84
84
|
32,
|
|
85
85
|
)
|
|
86
86
|
|
|
87
|
+
SPAN_TEXT = {"type": "string", "maxLength": 262144}
|
|
88
|
+
SPAN_LABEL = {"type": "string", "minLength": 1, "maxLength": 128}
|
|
89
|
+
SPAN_PROPERTIES = {
|
|
90
|
+
"text": SPAN_TEXT,
|
|
91
|
+
"label": SPAN_LABEL,
|
|
92
|
+
"start": {"type": "integer", "minimum": 0},
|
|
93
|
+
"end": {"type": "integer", "minimum": 1},
|
|
94
|
+
"accepted": {"type": "boolean"},
|
|
95
|
+
"source_priority": {"type": "integer", "minimum": 0},
|
|
96
|
+
"source_instance": {"type": "string", "maxLength": 256},
|
|
97
|
+
"score_group": {"type": "string", "maxLength": 256},
|
|
98
|
+
"score": {"type": "number"},
|
|
99
|
+
}
|
|
100
|
+
REVIEW_CANDIDATES = collection(
|
|
101
|
+
{
|
|
102
|
+
**SPAN_PROPERTIES,
|
|
103
|
+
"id": {"type": "string", "minLength": 1, "maxLength": 128},
|
|
104
|
+
"needs_review": {"type": "boolean"},
|
|
105
|
+
},
|
|
106
|
+
1000,
|
|
107
|
+
["id", "text", "label", "start", "end"],
|
|
108
|
+
)
|
|
109
|
+
REVIEW_DECISIONS = collection(
|
|
110
|
+
{
|
|
111
|
+
"id": {"type": "string", "maxLength": 128},
|
|
112
|
+
"decision": {"enum": [None, "keep", "drop", "reclass"]},
|
|
113
|
+
"proposed_label": {"type": ["string", "null"], "maxLength": 128},
|
|
114
|
+
},
|
|
115
|
+
1000,
|
|
116
|
+
[],
|
|
117
|
+
)
|
|
118
|
+
REVIEW_SPANS = collection(
|
|
119
|
+
SPAN_PROPERTIES, 1000, ["text", "label", "start", "end", "accepted", "source_priority"]
|
|
120
|
+
)
|
|
121
|
+
SPAN_POLICY = {
|
|
122
|
+
"type": "object",
|
|
123
|
+
"properties": {
|
|
124
|
+
"allowed_labels": {"type": ["array", "null"], "maxItems": 128, "items": SPAN_LABEL},
|
|
125
|
+
"excluded_labels": {"type": "array", "maxItems": 128, "items": SPAN_LABEL},
|
|
126
|
+
},
|
|
127
|
+
"additionalProperties": False,
|
|
128
|
+
}
|
|
129
|
+
SPAN_QUOTES = collection(
|
|
130
|
+
{"label": SPAN_LABEL, "reason": {"type": ["string", "null"]}}, 1000, ["label"]
|
|
131
|
+
)
|
|
132
|
+
GROUNDED_QUOTES = {
|
|
133
|
+
"type": "object",
|
|
134
|
+
"properties": {
|
|
135
|
+
"spans": REVIEW_SPANS,
|
|
136
|
+
"failed_quotes": collection(
|
|
137
|
+
{
|
|
138
|
+
"index": {"type": "integer", "minimum": 0},
|
|
139
|
+
"quote": {"type": "object"},
|
|
140
|
+
"reason": {
|
|
141
|
+
"enum": [
|
|
142
|
+
"non_verbatim_value",
|
|
143
|
+
"missing_context",
|
|
144
|
+
"non_verbatim_context",
|
|
145
|
+
"zero_contextual_matches",
|
|
146
|
+
]
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
1000,
|
|
150
|
+
),
|
|
151
|
+
"failed_count": {"type": "integer", "minimum": 0, "maximum": 1000},
|
|
152
|
+
"repair_quotes": {"type": "array", "maxItems": 1000, "items": {"type": "object"}},
|
|
153
|
+
"normalized_quotes": SPAN_QUOTES,
|
|
154
|
+
},
|
|
155
|
+
"required": ["spans", "failed_quotes", "failed_count", "repair_quotes", "normalized_quotes"],
|
|
156
|
+
"additionalProperties": False,
|
|
157
|
+
}
|
|
158
|
+
|
|
87
159
|
SEMANTIC_IO = {
|
|
160
|
+
"apply_span_decisions": (
|
|
161
|
+
[
|
|
162
|
+
SPAN_TEXT,
|
|
163
|
+
REVIEW_CANDIDATES,
|
|
164
|
+
{"anyOf": [REVIEW_DECISIONS, {"type": "object"}]},
|
|
165
|
+
SPAN_POLICY,
|
|
166
|
+
],
|
|
167
|
+
REVIEW_SPANS,
|
|
168
|
+
),
|
|
169
|
+
"ground_span_quotes": (
|
|
170
|
+
[SPAN_TEXT, {"anyOf": [SPAN_QUOTES, {"type": "object"}]}, SPAN_POLICY],
|
|
171
|
+
GROUNDED_QUOTES,
|
|
172
|
+
),
|
|
173
|
+
"reconcile_span_candidates": (
|
|
174
|
+
[
|
|
175
|
+
SPAN_TEXT,
|
|
176
|
+
{
|
|
177
|
+
"type": "object",
|
|
178
|
+
"maxProperties": 1000,
|
|
179
|
+
"additionalProperties": collection(
|
|
180
|
+
SPAN_PROPERTIES, 1000, ["text", "label", "start", "end"]
|
|
181
|
+
),
|
|
182
|
+
},
|
|
183
|
+
],
|
|
184
|
+
REVIEW_SPANS,
|
|
185
|
+
),
|
|
186
|
+
"encode_json": ([{}], {"type": "string", "maxLength": 262144}),
|
|
88
187
|
"align_spans": (
|
|
89
188
|
[STRING, collection({"text": STRING, "label": STRING}, 32)],
|
|
90
189
|
collection(
|
|
@@ -134,6 +233,18 @@ SEMANTIC_IO = {
|
|
|
134
233
|
}
|
|
135
234
|
|
|
136
235
|
CONFIG_FIELDS = {
|
|
236
|
+
"decisions_field": {"type": "string", "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,63}$"},
|
|
237
|
+
"quotes_field": {"type": "string", "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,63}$"},
|
|
238
|
+
"quote_field": {"type": "string", "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,63}$"},
|
|
239
|
+
"before_field": {"type": "string", "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,63}$"},
|
|
240
|
+
"after_field": {"type": "string", "pattern": "^[A-Za-z_][A-Za-z0-9_]{0,63}$"},
|
|
241
|
+
"source_priority": {"type": "integer", "minimum": 0},
|
|
242
|
+
"field_order": {
|
|
243
|
+
"type": "array",
|
|
244
|
+
"maxItems": 64,
|
|
245
|
+
"uniqueItems": True,
|
|
246
|
+
"items": {"type": "string", "minLength": 1, "maxLength": 128},
|
|
247
|
+
},
|
|
137
248
|
"casefold": {"type": "boolean"},
|
|
138
249
|
"unique": {"type": "boolean"},
|
|
139
250
|
"size": {"type": "integer", "minimum": 1, "maximum": 16384},
|
seldon_runtime/conditional.py
CHANGED
|
@@ -225,6 +225,8 @@ def binding_states(plan, spec):
|
|
|
225
225
|
if step.bind_call not in calls:
|
|
226
226
|
raise ValueError("unknown_call")
|
|
227
227
|
shared[step.bind_call] = step.id
|
|
228
|
+
if spec.reference_program is not None:
|
|
229
|
+
shared[step.id] = step.id
|
|
228
230
|
after[step.id] = shared
|
|
229
231
|
return before
|
|
230
232
|
|
seldon_runtime/contracts.py
CHANGED
|
@@ -545,6 +545,7 @@ class FunctionSpec(Contract):
|
|
|
545
545
|
template_syntax: Literal["python_format", "jinja_variables"] | None = None
|
|
546
546
|
bindings: list[Binding] = Field(default_factory=list, max_length=64)
|
|
547
547
|
output_schema: dict[str, Any]
|
|
548
|
+
reference_program: Plan | None = None
|
|
548
549
|
evaluator: Literal["exact", "records", "decimal", "rubric"] = "exact"
|
|
549
550
|
evaluation_pending: bool = False
|
|
550
551
|
rubric: Rubric | None = None
|
|
@@ -566,6 +567,8 @@ class FunctionSpec(Contract):
|
|
|
566
567
|
value.pop("evaluation", None)
|
|
567
568
|
if self.input_schema is None:
|
|
568
569
|
value.pop("input_schema", None)
|
|
570
|
+
if self.reference_program is None:
|
|
571
|
+
value.pop("reference_program", None)
|
|
569
572
|
if self.preparation is None:
|
|
570
573
|
value.pop("preparation", None)
|
|
571
574
|
if self.template_syntax is None:
|
|
@@ -622,13 +625,49 @@ class FunctionSpec(Contract):
|
|
|
622
625
|
ids = [c.id for c in self.calls]
|
|
623
626
|
if len(set(ids)) != len(ids) or "input" in ids:
|
|
624
627
|
raise ValueError("duplicate_call_id")
|
|
628
|
+
if self.reference_program is not None:
|
|
629
|
+
program = self.reference_program
|
|
630
|
+
if program.fallback:
|
|
631
|
+
raise ValueError("reference_program_has_fallback")
|
|
632
|
+
declared = {call.id: call for call in self.calls}
|
|
633
|
+
bound = []
|
|
634
|
+
for step in program.steps:
|
|
635
|
+
if step.op in {"generated_kernel", "choice", "extract"}:
|
|
636
|
+
raise ValueError("unsupported_reference_operation")
|
|
637
|
+
if step.op == "llm":
|
|
638
|
+
call = declared.get(step.call)
|
|
639
|
+
if call is None or step.id != call.id or step.model != call.model:
|
|
640
|
+
raise ValueError("reference_call_mismatch")
|
|
641
|
+
if (
|
|
642
|
+
step.instruction_mode != "original_call_ref"
|
|
643
|
+
or step.prompt is not None
|
|
644
|
+
or step.messages is not None
|
|
645
|
+
or step.output_schema is not None
|
|
646
|
+
or step.inputs
|
|
647
|
+
or step.config
|
|
648
|
+
or step.output_binding != "call"
|
|
649
|
+
or step.over is not None
|
|
650
|
+
or step.max_chunks is not None
|
|
651
|
+
):
|
|
652
|
+
raise ValueError("reference_call_modified")
|
|
653
|
+
bound.append(step.call)
|
|
654
|
+
if len(bound) != len(self.calls) or set(bound) != set(declared):
|
|
655
|
+
raise ValueError("reference_calls_incomplete")
|
|
656
|
+
from .program_validation import validate_program
|
|
657
|
+
|
|
658
|
+
validate_program(program, self)
|
|
625
659
|
occupied = set()
|
|
626
660
|
for binding in self.bindings:
|
|
627
661
|
if binding.call not in ids:
|
|
628
662
|
raise ValueError("unknown_call")
|
|
629
663
|
call = self.calls[ids.index(binding.call)]
|
|
630
664
|
root, _ = reference(binding.source)
|
|
631
|
-
|
|
665
|
+
prior_roots = ids[: ids.index(binding.call)]
|
|
666
|
+
if self.reference_program is not None:
|
|
667
|
+
steps = self.reference_program.steps
|
|
668
|
+
call_position = next(i for i, step in enumerate(steps) if step.call == binding.call)
|
|
669
|
+
prior_roots = [step.id for step in steps[:call_position]]
|
|
670
|
+
if root != "input" and root not in prior_roots:
|
|
632
671
|
raise ValueError("forward_reference")
|
|
633
672
|
if binding.message >= len(call.messages):
|
|
634
673
|
raise ValueError("missing_message")
|
|
@@ -682,6 +721,10 @@ class Step(Contract):
|
|
|
682
721
|
"merge_spans",
|
|
683
722
|
"merge_records",
|
|
684
723
|
"merge_labels",
|
|
724
|
+
"apply_span_decisions",
|
|
725
|
+
"ground_span_quotes",
|
|
726
|
+
"reconcile_span_candidates",
|
|
727
|
+
"encode_json",
|
|
685
728
|
]
|
|
686
729
|
inputs: list[str] = Field(default_factory=list, max_length=32)
|
|
687
730
|
config: dict[str, Any] = Field(default_factory=dict)
|
|
@@ -858,3 +901,31 @@ class Plan(Contract):
|
|
|
858
901
|
elif learned > 3:
|
|
859
902
|
raise ValueError("model_call_limit")
|
|
860
903
|
return self
|
|
904
|
+
|
|
905
|
+
|
|
906
|
+
def reference_plan(
|
|
907
|
+
spec: FunctionSpec,
|
|
908
|
+
*,
|
|
909
|
+
model_overrides: dict[str, str] | None = None,
|
|
910
|
+
version: str = "function-plan-v2",
|
|
911
|
+
) -> Plan:
|
|
912
|
+
"""Preserve declared workflow control and deterministic processing in every reader."""
|
|
913
|
+
overrides = model_overrides or {}
|
|
914
|
+
if not set(overrides) <= {call.id for call in spec.calls}:
|
|
915
|
+
raise ValueError("unknown_call")
|
|
916
|
+
if spec.reference_program is not None:
|
|
917
|
+
values = spec.reference_program.model_dump(mode="json")
|
|
918
|
+
for step in values["steps"]:
|
|
919
|
+
if step["op"] == "llm":
|
|
920
|
+
step["model"] = overrides.get(step["call"], step["model"])
|
|
921
|
+
return Plan.model_validate(values)
|
|
922
|
+
return Plan(
|
|
923
|
+
version=version,
|
|
924
|
+
steps=[
|
|
925
|
+
Step(id=call.id, op="llm", model=overrides.get(call.id, call.model), call=call.id)
|
|
926
|
+
for call in spec.calls
|
|
927
|
+
],
|
|
928
|
+
)
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
FunctionSpec.model_rebuild()
|
seldon_runtime/executor.py
CHANGED
|
@@ -17,7 +17,15 @@ from typing import Any
|
|
|
17
17
|
|
|
18
18
|
from jsonschema import Draft202012Validator
|
|
19
19
|
|
|
20
|
-
from .contracts import
|
|
20
|
+
from .contracts import (
|
|
21
|
+
FunctionSpec,
|
|
22
|
+
Plan,
|
|
23
|
+
canonical,
|
|
24
|
+
digest,
|
|
25
|
+
reference,
|
|
26
|
+
reference_plan,
|
|
27
|
+
semantic_plan,
|
|
28
|
+
)
|
|
21
29
|
from .provider_error import ProviderError
|
|
22
30
|
|
|
23
31
|
|
|
@@ -381,7 +389,8 @@ def step_payload(spec, step, inputs, prior, values, *, instruction_envelope=None
|
|
|
381
389
|
|
|
382
390
|
if instruction_envelope != INSTRUCTION_ENVELOPE_CONTRACT:
|
|
383
391
|
raise ValueError("instruction_envelope_unsupported")
|
|
384
|
-
|
|
392
|
+
bindings = {**values, **prior} if spec.reference_program is not None else prior
|
|
393
|
+
payload = render_request(spec, step.call, inputs, bindings)
|
|
385
394
|
if "max_tokens" not in payload:
|
|
386
395
|
# Explicit null freezes provider omission even when a run normally sizes
|
|
387
396
|
# output itself; the request marker also reserves the endpoint ceiling.
|
|
@@ -598,6 +607,9 @@ async def _execute(
|
|
|
598
607
|
if not probe and len(canonical(value).encode()) > 262144:
|
|
599
608
|
raise ValueError("intermediate_size_limit")
|
|
600
609
|
values[step.id] = value
|
|
610
|
+
if spec.reference_program is not None:
|
|
611
|
+
# Match validation's latest dominating producer for step/call collisions.
|
|
612
|
+
prior[step.id] = value
|
|
601
613
|
if trace is not None:
|
|
602
614
|
record["output"] = evidence_value(value)
|
|
603
615
|
predecessor = step.id
|
|
@@ -683,8 +695,9 @@ async def execute(
|
|
|
683
695
|
in {EVALUATION_FIDELITY_CONTRACT, SPAN_GROUNDING_CONTRACT, CORRECTNESS_REPAIR_CONTRACT}
|
|
684
696
|
and conditional is not False
|
|
685
697
|
)
|
|
686
|
-
|
|
687
|
-
|
|
698
|
+
checked_plans = [plan]
|
|
699
|
+
if spec.reference_program is not None:
|
|
700
|
+
checked_plans.append(spec.reference_program)
|
|
688
701
|
chunking = {
|
|
689
702
|
CONTEXT_CHUNKING_CONTRACT,
|
|
690
703
|
"optimizer-correctness-v4",
|
|
@@ -693,12 +706,15 @@ async def execute(
|
|
|
693
706
|
SPAN_GROUNDING_CONTRACT,
|
|
694
707
|
CORRECTNESS_REPAIR_CONTRACT,
|
|
695
708
|
}
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
contract not in chunking
|
|
700
|
-
|
|
701
|
-
|
|
709
|
+
for checked in checked_plans:
|
|
710
|
+
if checked.version == "function-plan-v5" and not v5_allowed:
|
|
711
|
+
raise ValueError("plan_version_unsupported")
|
|
712
|
+
if checked.version == "function-plan-v4" and contract not in chunking:
|
|
713
|
+
raise ValueError("plan_version_unsupported")
|
|
714
|
+
if any(step.config.get("view") == "bindings" for step in checked.steps) and (
|
|
715
|
+
contract not in chunking
|
|
716
|
+
):
|
|
717
|
+
raise ValueError("view_requires_v3")
|
|
702
718
|
from .deadline import admission_deadline
|
|
703
719
|
|
|
704
720
|
with admission_deadline(time.monotonic() + timeout):
|
|
@@ -829,9 +845,7 @@ async def _execute_with_fallback(
|
|
|
829
845
|
)
|
|
830
846
|
if not plan.fallback or not allowed:
|
|
831
847
|
raise
|
|
832
|
-
original =
|
|
833
|
-
steps=[Step(id=c.id, op="llm", model=c.model, call=c.id) for c in spec.calls]
|
|
834
|
-
)
|
|
848
|
+
original = reference_plan(spec, version="function-plan-v1")
|
|
835
849
|
fallback_phase = True
|
|
836
850
|
remaining = timeout - (time.monotonic() - start)
|
|
837
851
|
result = await _execute(
|
|
@@ -134,4 +134,6 @@ def validate_program(plan, spec):
|
|
|
134
134
|
if step.over:
|
|
135
135
|
produced = {"type": "array", "maxItems": 32, "items": {"type": "object"}}
|
|
136
136
|
schemas[step.id] = produced
|
|
137
|
+
if spec.reference_program is not None and not conditional:
|
|
138
|
+
completed[step.id] = step.id
|
|
137
139
|
return plan
|
seldon_runtime/semantic.py
CHANGED
|
@@ -4,6 +4,7 @@ Offsets are half-open Unicode codepoints in the original source. Retrieval is
|
|
|
4
4
|
lexical candidate generation, not semantic truth; omission is explicit evidence.
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
|
+
import json
|
|
7
8
|
import re
|
|
8
9
|
import unicodedata
|
|
9
10
|
from decimal import Decimal
|
|
@@ -22,12 +23,25 @@ OPERATORS = {
|
|
|
22
23
|
"merge_spans": (2, set()),
|
|
23
24
|
"merge_records": (1, {"fields", "sums"}),
|
|
24
25
|
"merge_labels": (1, {"rule", "order"}),
|
|
26
|
+
"apply_span_decisions": (4, {"decisions_field"}),
|
|
27
|
+
"ground_span_quotes": (
|
|
28
|
+
3,
|
|
29
|
+
{"quote_field", "before_field", "after_field", "source_priority", "quotes_field"},
|
|
30
|
+
),
|
|
31
|
+
"reconcile_span_candidates": (2, set()),
|
|
32
|
+
"encode_json": (1, {"field_order"}),
|
|
25
33
|
}
|
|
26
34
|
MERGE_RULES = frozenset({"all_agree", "any_true", "max_ordinal", "union"})
|
|
27
35
|
|
|
28
36
|
NEW_OPERATORS = {"align_spans", "project_records"}
|
|
29
37
|
# Advertised only with the chunking construction. Legacy registries stay unchanged.
|
|
30
38
|
CHUNK_OPERATORS = {"merge_spans", "merge_records", "merge_labels"}
|
|
39
|
+
SPAN_REVIEW_OPERATORS = {
|
|
40
|
+
"apply_span_decisions",
|
|
41
|
+
"ground_span_quotes",
|
|
42
|
+
"reconcile_span_candidates",
|
|
43
|
+
"encode_json",
|
|
44
|
+
}
|
|
31
45
|
|
|
32
46
|
|
|
33
47
|
UNALIGNED_MODES = frozenset({"fail", "drop"})
|
|
@@ -371,6 +385,18 @@ def validate_config(op, inputs, config):
|
|
|
371
385
|
raise ValueError("invalid_kernel_configuration")
|
|
372
386
|
if not Draft202012Validator(semantic_contract(op)["config_schema"]).is_valid(config):
|
|
373
387
|
raise ValueError("invalid_kernel_configuration")
|
|
388
|
+
if (
|
|
389
|
+
op == "ground_span_quotes"
|
|
390
|
+
and len(
|
|
391
|
+
{
|
|
392
|
+
config.get("quote_field", "text"),
|
|
393
|
+
config.get("before_field", "prefix"),
|
|
394
|
+
config.get("after_field", "suffix"),
|
|
395
|
+
}
|
|
396
|
+
)
|
|
397
|
+
!= 3
|
|
398
|
+
):
|
|
399
|
+
raise ValueError("invalid_kernel_configuration")
|
|
374
400
|
if op == "project_records":
|
|
375
401
|
from .contracts import reference
|
|
376
402
|
|
|
@@ -659,6 +685,71 @@ def merge_labels(chunks, rule="all_agree", order=None):
|
|
|
659
685
|
return merged
|
|
660
686
|
|
|
661
687
|
|
|
688
|
+
def _bounded_json(value):
|
|
689
|
+
"""Bound serialization work before asking the stdlib encoder to traverse it."""
|
|
690
|
+
remaining = 10000
|
|
691
|
+
|
|
692
|
+
def visit(item, depth):
|
|
693
|
+
nonlocal remaining
|
|
694
|
+
remaining -= 1
|
|
695
|
+
if remaining < 0 or depth > 64:
|
|
696
|
+
raise ValueError("input_limit")
|
|
697
|
+
if isinstance(item, (list, dict)):
|
|
698
|
+
if len(item) > 1000:
|
|
699
|
+
raise ValueError("input_limit")
|
|
700
|
+
if isinstance(item, dict):
|
|
701
|
+
if any(not isinstance(key, str) for key in item):
|
|
702
|
+
raise ValueError("invalid_input_shape")
|
|
703
|
+
children = item.values()
|
|
704
|
+
else:
|
|
705
|
+
children = item
|
|
706
|
+
for child in children:
|
|
707
|
+
visit(child, depth + 1)
|
|
708
|
+
elif item is not None and type(item) not in {str, int, float, bool}:
|
|
709
|
+
raise ValueError("invalid_input_shape")
|
|
710
|
+
|
|
711
|
+
visit(value, 0)
|
|
712
|
+
try:
|
|
713
|
+
encoded = json.dumps(value, ensure_ascii=False, allow_nan=False)
|
|
714
|
+
if len(encoded.encode("utf-8")) > 262144:
|
|
715
|
+
raise ValueError("input_limit")
|
|
716
|
+
except (TypeError, UnicodeEncodeError, OverflowError) as exc:
|
|
717
|
+
raise ValueError("invalid_input_shape") from exc
|
|
718
|
+
return encoded
|
|
719
|
+
|
|
720
|
+
|
|
721
|
+
def encode_json(value, field_order=None):
|
|
722
|
+
"""Serialize bounded JSON with optional recursive explicit dictionary order."""
|
|
723
|
+
encoded = _bounded_json(value)
|
|
724
|
+
if not field_order:
|
|
725
|
+
return encoded
|
|
726
|
+
order = {name: index for index, name in enumerate(field_order)}
|
|
727
|
+
|
|
728
|
+
def ordered(item):
|
|
729
|
+
if isinstance(item, dict):
|
|
730
|
+
return {
|
|
731
|
+
key: ordered(item[key])
|
|
732
|
+
for key in sorted(item, key=lambda name: order.get(name, len(order)))
|
|
733
|
+
}
|
|
734
|
+
if isinstance(item, list):
|
|
735
|
+
return [ordered(child) for child in item]
|
|
736
|
+
return item
|
|
737
|
+
|
|
738
|
+
return json.dumps(ordered(value), ensure_ascii=False, allow_nan=False)
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
def _span_records(value, field, schema):
|
|
742
|
+
from jsonschema import Draft202012Validator
|
|
743
|
+
|
|
744
|
+
if field is not None:
|
|
745
|
+
if not isinstance(value, dict):
|
|
746
|
+
raise ValueError("invalid_input_shape")
|
|
747
|
+
value = value.get(field, [])
|
|
748
|
+
if not Draft202012Validator(schema).is_valid(value):
|
|
749
|
+
raise ValueError("invalid_input_shape")
|
|
750
|
+
return value
|
|
751
|
+
|
|
752
|
+
|
|
662
753
|
def kernel(op, args, config, diagnostics=None):
|
|
663
754
|
validate_config(op, args, config)
|
|
664
755
|
from jsonschema import Draft202012Validator
|
|
@@ -667,6 +758,55 @@ def kernel(op, args, config, diagnostics=None):
|
|
|
667
758
|
|
|
668
759
|
if not Draft202012Validator(semantic_contract(op)["input_schema"]).is_valid(args):
|
|
669
760
|
raise ValueError("invalid_input_shape")
|
|
761
|
+
if op in SPAN_REVIEW_OPERATORS:
|
|
762
|
+
from .components import REVIEW_DECISIONS, SPAN_QUOTES
|
|
763
|
+
from .span_review import (
|
|
764
|
+
apply_span_decisions,
|
|
765
|
+
ground_span_quotes,
|
|
766
|
+
reconcile_span_candidates,
|
|
767
|
+
)
|
|
768
|
+
|
|
769
|
+
_bounded_json(args)
|
|
770
|
+
if op == "apply_span_decisions":
|
|
771
|
+
result = apply_span_decisions(
|
|
772
|
+
args[0],
|
|
773
|
+
args[1],
|
|
774
|
+
_span_records(args[2], config.get("decisions_field"), REVIEW_DECISIONS),
|
|
775
|
+
**args[3],
|
|
776
|
+
)
|
|
777
|
+
elif op == "ground_span_quotes":
|
|
778
|
+
quotes = _span_records(args[1], config.get("quotes_field"), SPAN_QUOTES)
|
|
779
|
+
if "quotes_field" in config:
|
|
780
|
+
# Provider response envelopes are data, not trusted provenance.
|
|
781
|
+
# Only declared quotation fields may affect grounded admissions.
|
|
782
|
+
quote_fields = {
|
|
783
|
+
config.get("quote_field", "text"),
|
|
784
|
+
config.get("before_field", "prefix"),
|
|
785
|
+
config.get("after_field", "suffix"),
|
|
786
|
+
"label",
|
|
787
|
+
"reason",
|
|
788
|
+
}
|
|
789
|
+
quotes = [
|
|
790
|
+
{key: value for key, value in quote.items() if key in quote_fields}
|
|
791
|
+
for quote in quotes
|
|
792
|
+
]
|
|
793
|
+
result = ground_span_quotes(
|
|
794
|
+
args[0],
|
|
795
|
+
quotes,
|
|
796
|
+
**args[2],
|
|
797
|
+
**{key: value for key, value in config.items() if key != "quotes_field"},
|
|
798
|
+
)
|
|
799
|
+
elif op == "reconcile_span_candidates":
|
|
800
|
+
spans = []
|
|
801
|
+
for collection in args[1].values():
|
|
802
|
+
if len(spans) + len(collection) > 1000:
|
|
803
|
+
raise ValueError("span_review_item_limit")
|
|
804
|
+
spans.extend(collection)
|
|
805
|
+
result = reconcile_span_candidates(args[0], spans)
|
|
806
|
+
else:
|
|
807
|
+
result = encode_json(args[0], config.get("field_order"))
|
|
808
|
+
_bounded_json(result)
|
|
809
|
+
return result
|
|
670
810
|
if op == "align_spans":
|
|
671
811
|
return align_spans(
|
|
672
812
|
*args,
|
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
"""Bounded source-grounded decisions and quotations for span review.
|
|
2
|
+
|
|
3
|
+
These functions retain overlapping claims. Reconcile the combined review and
|
|
4
|
+
quotation results only after both stages complete; stage-local resolution can
|
|
5
|
+
otherwise discard a claim that a later stage changes.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import math
|
|
9
|
+
import re
|
|
10
|
+
|
|
11
|
+
MAX_ITEMS = 1000
|
|
12
|
+
MAX_TEXT_BYTES = 262144
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _text(text):
|
|
16
|
+
if not isinstance(text, str):
|
|
17
|
+
raise ValueError("invalid_span_review_text")
|
|
18
|
+
try:
|
|
19
|
+
size = len(text.encode("utf-8"))
|
|
20
|
+
except UnicodeEncodeError as exc:
|
|
21
|
+
raise ValueError("invalid_span_review_text") from exc
|
|
22
|
+
if size > MAX_TEXT_BYTES:
|
|
23
|
+
raise ValueError("span_review_text_limit")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _items(items, maximum):
|
|
27
|
+
if type(maximum) is not int or not 1 <= maximum <= MAX_ITEMS:
|
|
28
|
+
raise ValueError("invalid_span_review_limit")
|
|
29
|
+
if not isinstance(items, list) or len(items) > maximum:
|
|
30
|
+
raise ValueError("span_review_item_limit")
|
|
31
|
+
if any(not isinstance(item, dict) for item in items):
|
|
32
|
+
raise ValueError("invalid_span_review_record")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _label(label):
|
|
36
|
+
if not isinstance(label, str):
|
|
37
|
+
raise ValueError("invalid_span_review_label")
|
|
38
|
+
return label.strip().casefold()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _policy(allowed, excluded):
|
|
42
|
+
if allowed is not None and not isinstance(allowed, (list, tuple, set, frozenset)):
|
|
43
|
+
raise ValueError("invalid_span_review_labels")
|
|
44
|
+
if not isinstance(excluded, (list, tuple, set, frozenset)):
|
|
45
|
+
raise ValueError("invalid_span_review_labels")
|
|
46
|
+
return None if allowed is None else {_label(x) for x in allowed}, {_label(x) for x in excluded}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _eligible(label, allowed, excluded):
|
|
50
|
+
return bool(label) and label not in excluded and (allowed is None or label in allowed)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _metadata(candidate, accepted):
|
|
54
|
+
result = {"accepted": accepted}
|
|
55
|
+
priority = candidate.get("source_priority", MAX_ITEMS)
|
|
56
|
+
if type(priority) is not int or priority < 0:
|
|
57
|
+
raise ValueError("invalid_span_review_priority")
|
|
58
|
+
result["source_priority"] = priority
|
|
59
|
+
for field in ("source_instance", "score_group"):
|
|
60
|
+
if field in candidate:
|
|
61
|
+
if not isinstance(candidate[field], str):
|
|
62
|
+
raise ValueError("invalid_span_review_provenance")
|
|
63
|
+
result[field] = candidate[field]
|
|
64
|
+
score = candidate.get("score")
|
|
65
|
+
if score is not None:
|
|
66
|
+
if (
|
|
67
|
+
isinstance(score, bool)
|
|
68
|
+
or not isinstance(score, (int, float))
|
|
69
|
+
or not math.isfinite(score)
|
|
70
|
+
):
|
|
71
|
+
raise ValueError("invalid_span_review_score")
|
|
72
|
+
if accepted:
|
|
73
|
+
result["score"] = score
|
|
74
|
+
return result
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def apply_span_decisions(
|
|
78
|
+
text, candidates, decisions, *, allowed_labels=None, excluded_labels=(), max_items=MAX_ITEMS
|
|
79
|
+
):
|
|
80
|
+
"""Apply request-local keep/drop/reclass decisions to authoritative candidates.
|
|
81
|
+
|
|
82
|
+
Candidate fields: id, text, label, start, end and optional needs_review
|
|
83
|
+
(default true). Missing/null decisions and blank reclass labels protect the
|
|
84
|
+
original claim without promoting it to explicit acceptance. Directly accepted
|
|
85
|
+
candidates ignore decisions. Unknown IDs are ignored; repeated assigned IDs
|
|
86
|
+
fail. Provenance is retained for later cross-stage overlap resolution.
|
|
87
|
+
"""
|
|
88
|
+
_text(text)
|
|
89
|
+
_items(candidates, max_items)
|
|
90
|
+
_items(decisions, max_items)
|
|
91
|
+
allowed, excluded = _policy(allowed_labels, excluded_labels)
|
|
92
|
+
by_id = {}
|
|
93
|
+
assigned = set()
|
|
94
|
+
for candidate in candidates:
|
|
95
|
+
identity = candidate.get("id")
|
|
96
|
+
start, end = candidate.get("start"), candidate.get("end")
|
|
97
|
+
needs_review = candidate.get("needs_review", True)
|
|
98
|
+
if not isinstance(identity, str) or not identity or identity in by_id:
|
|
99
|
+
raise ValueError("invalid_span_review_identity")
|
|
100
|
+
if type(needs_review) is not bool:
|
|
101
|
+
raise ValueError("invalid_span_review_route")
|
|
102
|
+
if (
|
|
103
|
+
type(start) is not int
|
|
104
|
+
or type(end) is not int
|
|
105
|
+
or not 0 <= start < end <= len(text)
|
|
106
|
+
or candidate.get("text") != text[start:end]
|
|
107
|
+
):
|
|
108
|
+
raise ValueError("invalid_span_review_coordinates")
|
|
109
|
+
_label(candidate.get("label"))
|
|
110
|
+
_metadata(candidate, True)
|
|
111
|
+
by_id[identity] = candidate
|
|
112
|
+
if needs_review:
|
|
113
|
+
assigned.add(identity)
|
|
114
|
+
decisions_by_id = {}
|
|
115
|
+
for decision in decisions:
|
|
116
|
+
identity = decision.get("id", "")
|
|
117
|
+
choice = decision.get("decision")
|
|
118
|
+
proposed = decision.get("proposed_label")
|
|
119
|
+
if not isinstance(identity, str) or choice not in (None, "keep", "drop", "reclass"):
|
|
120
|
+
raise ValueError("invalid_span_review_decision")
|
|
121
|
+
if proposed is not None and not isinstance(proposed, str):
|
|
122
|
+
raise ValueError("invalid_span_review_decision")
|
|
123
|
+
if identity not in assigned:
|
|
124
|
+
continue
|
|
125
|
+
if identity in decisions_by_id:
|
|
126
|
+
raise ValueError("duplicate_span_review_decision")
|
|
127
|
+
decisions_by_id[identity] = decision
|
|
128
|
+
spans = []
|
|
129
|
+
for identity, candidate in by_id.items():
|
|
130
|
+
decision = decisions_by_id.get(identity, {})
|
|
131
|
+
choice = decision.get("decision")
|
|
132
|
+
label = _label(candidate["label"])
|
|
133
|
+
accepted = not candidate.get("needs_review", True)
|
|
134
|
+
if not accepted:
|
|
135
|
+
if choice == "drop":
|
|
136
|
+
continue
|
|
137
|
+
proposed = _label(decision.get("proposed_label") or "")
|
|
138
|
+
if choice == "reclass" and proposed:
|
|
139
|
+
label, accepted = proposed, True
|
|
140
|
+
elif choice == "keep":
|
|
141
|
+
accepted = True
|
|
142
|
+
if _eligible(label, allowed, excluded):
|
|
143
|
+
spans.append(
|
|
144
|
+
{
|
|
145
|
+
"id": identity,
|
|
146
|
+
"text": candidate["text"],
|
|
147
|
+
"label": label,
|
|
148
|
+
"start": candidate["start"],
|
|
149
|
+
"end": candidate["end"],
|
|
150
|
+
**_metadata(candidate, accepted),
|
|
151
|
+
}
|
|
152
|
+
)
|
|
153
|
+
return sorted(spans, key=lambda item: (item["start"], item["end"], item["label"], item["id"]))
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def ground_span_quotes(
|
|
157
|
+
text,
|
|
158
|
+
quotes,
|
|
159
|
+
*,
|
|
160
|
+
allowed_labels=None,
|
|
161
|
+
excluded_labels=(),
|
|
162
|
+
max_items=MAX_ITEMS,
|
|
163
|
+
quote_field="text",
|
|
164
|
+
before_field="prefix",
|
|
165
|
+
after_field="suffix",
|
|
166
|
+
source_priority=MAX_ITEMS,
|
|
167
|
+
):
|
|
168
|
+
"""Ground all exact contextual occurrences; report failures for bounded repair.
|
|
169
|
+
|
|
170
|
+
Quote fields default to text, label, prefix and suffix; explicit field-name
|
|
171
|
+
mappings preserve an existing response contract. Boundary whitespace in text
|
|
172
|
+
moves into its adjacent context. Empty contexts are
|
|
173
|
+
permitted only for a quotation equal to the entire source. A grounded but
|
|
174
|
+
out-of-policy label is omitted without becoming a repair request. Repair
|
|
175
|
+
data contains only original quotation fields, label and failure categories.
|
|
176
|
+
source_priority supplies the default provenance for grounded quotations.
|
|
177
|
+
"""
|
|
178
|
+
_text(text)
|
|
179
|
+
_items(quotes, max_items)
|
|
180
|
+
if any(
|
|
181
|
+
not isinstance(field, str) or not re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]{0,63}", field)
|
|
182
|
+
for field in (quote_field, before_field, after_field)
|
|
183
|
+
):
|
|
184
|
+
raise ValueError("invalid_span_review_fields")
|
|
185
|
+
if len({quote_field, before_field, after_field}) != 3:
|
|
186
|
+
raise ValueError("invalid_span_review_fields")
|
|
187
|
+
_metadata({"source_priority": source_priority}, True)
|
|
188
|
+
allowed, excluded = _policy(allowed_labels, excluded_labels)
|
|
189
|
+
spans, failed = [], []
|
|
190
|
+
matches_seen = 0
|
|
191
|
+
for index, quote in enumerate(quotes):
|
|
192
|
+
original = quote.get(quote_field)
|
|
193
|
+
before, after = quote.get(before_field, ""), quote.get(after_field, "")
|
|
194
|
+
if not all(isinstance(value, str) for value in (original, before, after)):
|
|
195
|
+
raise ValueError("invalid_span_review_quote")
|
|
196
|
+
label = _label(quote.get("label"))
|
|
197
|
+
value = original.strip()
|
|
198
|
+
before += original[: len(original) - len(original.lstrip())]
|
|
199
|
+
after = original[len(original.rstrip()) :] + after
|
|
200
|
+
starts, cursor = [], 0
|
|
201
|
+
while value:
|
|
202
|
+
start = text.find(value, cursor)
|
|
203
|
+
if start < 0:
|
|
204
|
+
break
|
|
205
|
+
starts.append(start)
|
|
206
|
+
if len(starts) > MAX_ITEMS:
|
|
207
|
+
raise ValueError("span_review_match_limit")
|
|
208
|
+
cursor = start + 1
|
|
209
|
+
reason = None
|
|
210
|
+
if not value or not starts:
|
|
211
|
+
reason = "non_verbatim_value"
|
|
212
|
+
elif not before and not after and value != text:
|
|
213
|
+
reason = "missing_context"
|
|
214
|
+
elif (before and not any(text.endswith(before, 0, start) for start in starts)) or (
|
|
215
|
+
after and not any(text.startswith(after, start + len(value)) for start in starts)
|
|
216
|
+
):
|
|
217
|
+
reason = "non_verbatim_context"
|
|
218
|
+
matches = [
|
|
219
|
+
start
|
|
220
|
+
for start in starts
|
|
221
|
+
if text.endswith(before, 0, start) and text.startswith(after, start + len(value))
|
|
222
|
+
]
|
|
223
|
+
if reason is None and not matches:
|
|
224
|
+
reason = "zero_contextual_matches"
|
|
225
|
+
if reason is not None:
|
|
226
|
+
failed.append({"index": index, "quote": dict(quote), "reason": reason})
|
|
227
|
+
continue
|
|
228
|
+
matches_seen += len(matches)
|
|
229
|
+
if matches_seen > MAX_ITEMS:
|
|
230
|
+
raise ValueError("span_review_match_limit")
|
|
231
|
+
if not _eligible(label, allowed, excluded):
|
|
232
|
+
continue
|
|
233
|
+
for start in matches:
|
|
234
|
+
spans.append(
|
|
235
|
+
{
|
|
236
|
+
"text": value,
|
|
237
|
+
"label": label,
|
|
238
|
+
"start": start,
|
|
239
|
+
"end": start + len(value),
|
|
240
|
+
**_metadata({"source_priority": source_priority, **quote}, True),
|
|
241
|
+
}
|
|
242
|
+
)
|
|
243
|
+
return {
|
|
244
|
+
"spans": spans,
|
|
245
|
+
"failed_quotes": failed,
|
|
246
|
+
"failed_count": len(failed),
|
|
247
|
+
"normalized_quotes": [dict(quote) for quote in quotes],
|
|
248
|
+
"repair_quotes": [
|
|
249
|
+
{
|
|
250
|
+
quote_field: failure["quote"][quote_field],
|
|
251
|
+
before_field: failure["quote"].get(before_field, ""),
|
|
252
|
+
after_field: failure["quote"].get(after_field, ""),
|
|
253
|
+
"label": failure["quote"]["label"],
|
|
254
|
+
"failure_categories": [failure["reason"]],
|
|
255
|
+
}
|
|
256
|
+
for failure in failed
|
|
257
|
+
],
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def reconcile_span_candidates(text, spans, *, max_items=MAX_ITEMS):
|
|
262
|
+
"""Coalesce identical claims then greedily select nonoverlapping source spans.
|
|
263
|
+
|
|
264
|
+
Longer spans precede shorter ones, with earlier starts breaking ties. At
|
|
265
|
+
identical bounds explicit acceptance outranks protected fallback; then source
|
|
266
|
+
priority breaks ties. Scores rank only a tied group sharing one declared
|
|
267
|
+
calibration domain. No score crosses model/calibration boundaries.
|
|
268
|
+
"""
|
|
269
|
+
_text(text)
|
|
270
|
+
_items(spans, max_items)
|
|
271
|
+
groups = {}
|
|
272
|
+
for span in spans:
|
|
273
|
+
start, end = span.get("start"), span.get("end")
|
|
274
|
+
if (
|
|
275
|
+
type(start) is not int
|
|
276
|
+
or type(end) is not int
|
|
277
|
+
or not 0 <= start < end <= len(text)
|
|
278
|
+
or span.get("text") != text[start:end]
|
|
279
|
+
or type(span.get("accepted", True)) is not bool
|
|
280
|
+
):
|
|
281
|
+
raise ValueError("invalid_span_review_coordinates")
|
|
282
|
+
label = _label(span.get("label"))
|
|
283
|
+
if not label:
|
|
284
|
+
raise ValueError("invalid_span_review_label")
|
|
285
|
+
_metadata(span, span.get("accepted", True))
|
|
286
|
+
groups.setdefault((start, end, label), []).append(span)
|
|
287
|
+
coalesced = []
|
|
288
|
+
for (start, end, label), equivalents in groups.items():
|
|
289
|
+
explicit = [span for span in equivalents if span.get("accepted", True)]
|
|
290
|
+
priority = min(
|
|
291
|
+
(span.get("source_priority", MAX_ITEMS) for span in explicit), default=MAX_ITEMS
|
|
292
|
+
)
|
|
293
|
+
primary = [span for span in explicit if span.get("source_priority", MAX_ITEMS) == priority]
|
|
294
|
+
domains = {span.get("score_group") for span in primary}
|
|
295
|
+
domain = next(iter(domains)) if len(domains) == 1 and None not in domains else None
|
|
296
|
+
scores = [span["score"] for span in primary if span.get("score") is not None]
|
|
297
|
+
item = {
|
|
298
|
+
"text": text[start:end],
|
|
299
|
+
"label": label,
|
|
300
|
+
"start": start,
|
|
301
|
+
"end": end,
|
|
302
|
+
"accepted": bool(explicit),
|
|
303
|
+
"source_priority": priority,
|
|
304
|
+
}
|
|
305
|
+
if domain:
|
|
306
|
+
item["score_group"] = domain
|
|
307
|
+
if scores:
|
|
308
|
+
item["score"] = max(scores)
|
|
309
|
+
coalesced.append(item)
|
|
310
|
+
geometry = {}
|
|
311
|
+
for item in coalesced:
|
|
312
|
+
geometry.setdefault(
|
|
313
|
+
(item["start"], item["end"], item["accepted"], item["source_priority"]), []
|
|
314
|
+
).append(item)
|
|
315
|
+
score_ranks = {}
|
|
316
|
+
for tied in geometry.values():
|
|
317
|
+
domains = {item.get("score_group") for item in tied}
|
|
318
|
+
comparable = len(domains) == 1 and None not in domains
|
|
319
|
+
for item in tied:
|
|
320
|
+
score_ranks[id(item)] = -item.get("score", 0) if comparable else 0
|
|
321
|
+
ordered = sorted(
|
|
322
|
+
coalesced,
|
|
323
|
+
key=lambda item: (
|
|
324
|
+
-(item["end"] - item["start"]),
|
|
325
|
+
item["start"],
|
|
326
|
+
item["end"],
|
|
327
|
+
not item["accepted"],
|
|
328
|
+
item["source_priority"],
|
|
329
|
+
score_ranks[id(item)],
|
|
330
|
+
item["label"],
|
|
331
|
+
),
|
|
332
|
+
)
|
|
333
|
+
retained = []
|
|
334
|
+
for item in ordered:
|
|
335
|
+
if not any(
|
|
336
|
+
item["start"] < other["end"] and other["start"] < item["end"] for other in retained
|
|
337
|
+
):
|
|
338
|
+
retained.append(item)
|
|
339
|
+
return sorted(retained, key=lambda item: (item["start"], item["end"], item["label"]))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: seldon-runtime
|
|
3
|
-
Version: 0.1.1.
|
|
3
|
+
Version: 0.1.1.dev4
|
|
4
4
|
Summary: Standalone Seldon Functions interpreter and local execution client
|
|
5
5
|
License-Expression: Apache-2.0
|
|
6
6
|
Project-URL: Source, https://github.com/Deep-Insight-Labs/seldon-runtime
|
|
@@ -44,8 +44,8 @@ CLI can compile Functions, produce comparison reports and explicitly export
|
|
|
44
44
|
selected artifacts today; it requires no platform account. This standalone path is separate from hosted Runner bundle
|
|
45
45
|
eligibility and fallback.
|
|
46
46
|
|
|
47
|
-
The source checkout targets **0.1.1.
|
|
48
|
-
artifact bindings. Install this checkout with `python -m pip install .` to use
|
|
47
|
+
The source checkout targets **0.1.1.dev4 (unreleased)**, adding declared reference
|
|
48
|
+
workflows alongside standalone native artifact bindings. Install this checkout with `python -m pip install .` to use
|
|
49
49
|
that capability; the published dev2 package does not accept native artifacts.
|
|
50
50
|
|
|
51
51
|
## Install
|
|
@@ -175,9 +175,9 @@ a development environment with `python -m pip install '.[schema]'` followed by
|
|
|
175
175
|
that generator's `--check` mode, independently of the runtime compatibility matrix.
|
|
176
176
|
|
|
177
177
|
The manifest freezes the compiler version and an explicit runtime version. New
|
|
178
|
-
exports target the source runtime version (`0.1.1.
|
|
179
|
-
the reviewed set `0.1.1.dev1`, `0.1.1.dev2` and `0.1.1.
|
|
180
|
-
their pin and hashes. Native artifacts
|
|
178
|
+
exports target the source runtime version (`0.1.1.dev4`). Its reader accepts
|
|
179
|
+
the reviewed set `0.1.1.dev1`, `0.1.1.dev2`, `0.1.1.dev3` and `0.1.1.dev4`; original manifests retain
|
|
180
|
+
their pin and hashes. Native artifacts require dev3 or dev4; older runtime contracts
|
|
181
181
|
remain chat/deterministic-only. dev0 and future versions are rejected until explicitly
|
|
182
182
|
reviewed; there is no version-prefix or range-based compatibility inference.
|
|
183
183
|
The manifest also freezes the optimizer contract (v1–v8), evaluation semantics,
|
|
@@ -215,7 +215,7 @@ policy before invoking a provider; your transport still owns TLS, credential ori
|
|
|
215
215
|
binding, response-size limits and protocol decoding. Known transport failures use
|
|
216
216
|
`ProviderError`; untyped failures stop without automatic fallback or retry.
|
|
217
217
|
|
|
218
|
-
The source
|
|
218
|
+
The source dev4 handoff supports OpenAI-compatible chat, native `choice`/`extract`
|
|
219
219
|
and deterministic operators, including versioned chunking/conditional graphs.
|
|
220
220
|
Native bindings declare `protocol="system_one"` with `capabilities=("choice",)` or
|
|
221
221
|
`protocol="fastino_gliner"` with `capabilities=("extract",)`, a nonempty `upstream_model`
|
|
@@ -300,3 +300,43 @@ No stored registry token is required. Registry versions are immutable.
|
|
|
300
300
|
Apache-2.0; see [LICENSE](LICENSE) and [NOTICE](NOTICE). Optional GLiNER2 code and
|
|
301
301
|
downloaded weights retain their own upstream licenses. The release is scoped to
|
|
302
302
|
this repository; it does not license the separately operated Seldon service.
|
|
303
|
+
|
|
304
|
+
## Declared reference workflows
|
|
305
|
+
|
|
306
|
+
An optional `FunctionSpec.reference_program` supplies the existing workflow as a
|
|
307
|
+
bounded `Plan`. Use it when the reference performs deterministic processing or
|
|
308
|
+
conditional execution around its saved LLM calls. Without it, reference execution
|
|
309
|
+
remains the ordered saved calls and returns the last result. Omitted or null programs
|
|
310
|
+
preserve existing source serialization and digests.
|
|
311
|
+
|
|
312
|
+
Each saved call must appear exactly once statically, with its original call ID,
|
|
313
|
+
model and request; branches may skip a call at execution time. The program may use
|
|
314
|
+
the existing deterministic operators and forward-only branch/merge/return nodes,
|
|
315
|
+
within the existing step limits. Generated kernels, native model steps, added paid
|
|
316
|
+
calls, altered prompts/configuration and reference fallback are rejected. Input and
|
|
317
|
+
intermediate JSON pointers are validated using the existing program/schema rules.
|
|
318
|
+
|
|
319
|
+
For example, two saved calls can finish with an `object` step whose `inputs` are
|
|
320
|
+
`["validate#", "augment#"]` and whose `config` is
|
|
321
|
+
`{"fields": ["validation", "augmentation"]}`. This returns both results without
|
|
322
|
+
another model request. A downstream deterministic operator can instead produce the
|
|
323
|
+
final domain output. The final value must satisfy the Function's output schema.
|
|
324
|
+
|
|
325
|
+
The compiler's direct substitutions preserve this exact graph and replace only
|
|
326
|
+
LLM models. Explicit failure fallback executes the complete declared reference,
|
|
327
|
+
including its original branching and output processing. Both selected and reference
|
|
328
|
+
graphs must satisfy frozen conditional/chunking permissions before execution.
|
|
329
|
+
Artifacts containing `reference_program` require runtime dev4; older readers fail
|
|
330
|
+
closed rather than discard workflow behavior. Existing dev1/dev2 chat and dev3
|
|
331
|
+
native artifacts remain readable with their original version pins and hashes.
|
|
332
|
+
|
|
333
|
+
The v7 component registry additionally exposes bounded `apply_span_decisions`,
|
|
334
|
+
`ground_span_quotes`, `reconcile_span_candidates`, and `encode_json` kernels.
|
|
335
|
+
Span decisions operate on authoritative source coordinates. Quotation grounding
|
|
336
|
+
retains every exact contextual occurrence and exposes failures for conditional
|
|
337
|
+
repair; raw model responses cannot supply trusted provenance. Reconcile all
|
|
338
|
+
stage claims together before resolving overlaps. Missing configured response
|
|
339
|
+
collections default to empty; explicit nulls and invalid types fail.
|
|
340
|
+
`encode_json` emits standard JSON spacing, supports explicit field order, and
|
|
341
|
+
bounds depth, node count, collection sizes and UTF-8 output bytes. These kernels
|
|
342
|
+
require the dev4 artifact reader and are hidden from historical compiler registries.
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
seldon_runtime/__init__.py,sha256=
|
|
2
|
-
seldon_runtime/artifact.py,sha256=
|
|
1
|
+
seldon_runtime/__init__.py,sha256=aQo_6iL8DOVK7kKAbAWvGwYNUi1KraTK8dHzXCMBZnU,102
|
|
2
|
+
seldon_runtime/artifact.py,sha256=ayLIbmq66uL72vJbiW1nSEICXCRwgpoZuM_QZJ7NYFA,15552
|
|
3
3
|
seldon_runtime/attempt_bound.py,sha256=uXGBUBg09Uie75EfSTs_QfSn52tntoJDb8YQ6V_w9GY,1258
|
|
4
|
-
seldon_runtime/components.py,sha256=
|
|
5
|
-
seldon_runtime/conditional.py,sha256=
|
|
4
|
+
seldon_runtime/components.py,sha256=3VuPfX5JM5rt0lylqUBr9UXcrNlusRsZ-7EsK_0so_0,16149
|
|
5
|
+
seldon_runtime/conditional.py,sha256=R0dLX2eLrkUotCcl0X3ZaO-f7tNAAIv3jLBGhc64ycM,10952
|
|
6
6
|
seldon_runtime/contract_versions.py,sha256=MnUAEwmnCiJ6O67WwbtOGi33PGZvIyN-O92TZwkek34,1037
|
|
7
|
-
seldon_runtime/contracts.py,sha256=
|
|
7
|
+
seldon_runtime/contracts.py,sha256=p-B_E470NErKJQ-K7GM5Xb46qIY5SIdKNYZJmVLNOag,37834
|
|
8
8
|
seldon_runtime/deadline.py,sha256=L8xBebhoLM2vlI0C5EhLbELflCaag_AgBD-AptKJDws,1879
|
|
9
|
-
seldon_runtime/executor.py,sha256=
|
|
9
|
+
seldon_runtime/executor.py,sha256=3qSPB9T0nxaqtYh5OrrQ1YOes81ion7GpF3tZBIEahw,35462
|
|
10
10
|
seldon_runtime/gliner_normalize.py,sha256=V-29aGos27Ay6Eczz3AJ9yroEaZ7BSNw-v20ZqKsjs8,11384
|
|
11
11
|
seldon_runtime/instruction_envelope.py,sha256=S-riu21kqicDyPnZgKPlpWH_J7IpNo_AGRJ4Jnn1zq4,2719
|
|
12
12
|
seldon_runtime/json_pointer.py,sha256=r__ji3JyTNDn-bGfc9oVIoA1FAq2UBvbQ-05UBi6_8g,849
|
|
@@ -16,11 +16,12 @@ seldon_runtime/local_telemetry.py,sha256=fd2u7d7Fr6OOPS50CkmS5tSAXT0DfWrGE-X-Izw
|
|
|
16
16
|
seldon_runtime/native_adapter.py,sha256=LKtOUi4c-lrMeHiwcbL3n8bUJO5e5A2mYD4GYLobvto,7139
|
|
17
17
|
seldon_runtime/native_schema.py,sha256=M2V3FdlGRxeBJ6RjGTb8iLcqmmMhclK6Auh1nh8ic1M,1515
|
|
18
18
|
seldon_runtime/output_budget.py,sha256=Q4NULJWvBlYJmjn_k9QS4z5AVbXqbxP4zkBQIQbM37Y,8214
|
|
19
|
-
seldon_runtime/program_validation.py,sha256=
|
|
19
|
+
seldon_runtime/program_validation.py,sha256=gdE4VMPQjAEYSfBDP7QBhSsYg8iEZmfS12ocMPBC33A,6152
|
|
20
20
|
seldon_runtime/provider_error.py,sha256=IM9H1rDFN-obkDlzDr0f-CXbf5RsYu5jXsj3chTNX3Y,1689
|
|
21
21
|
seldon_runtime/research_kernels.py,sha256=cMOSYJQD67ngbhcCvkq6pNxrHrgAluBgcMxICo9n-Xw,2162
|
|
22
22
|
seldon_runtime/runner.py,sha256=AAcZoFr4caUT6BEtjCelc58zvul2wT2hZOAigE2_g9k,20911
|
|
23
|
-
seldon_runtime/semantic.py,sha256=
|
|
23
|
+
seldon_runtime/semantic.py,sha256=pB-NlATLV7s5LcXO2wJ8EtSNqtS9JELhFFZGVxdqQY0,35210
|
|
24
|
+
seldon_runtime/span_review.py,sha256=qJ29h6dbzhKK5kSgAFpFcFeel0cUmhXp4lLvBOnEggg,13491
|
|
24
25
|
seldon_runtime/system_one.py,sha256=p7vXIo1j-3B2mWGKS43cUCLZ-N4hh2ScrYcVst50BVk,7119
|
|
25
26
|
seldon_runtime/telemetry.py,sha256=2ik9i1vTnTx5U42tPMhsmDC_EcernSov_dxgzAF04uE,6984
|
|
26
27
|
seldon_runtime/templates.py,sha256=zp48gE-ZEsvd3aU-bH8W7d8-MtS35AqzmSQtpuz7W-4,4379
|
|
@@ -28,10 +29,10 @@ seldon_runtime/tokenizer.py,sha256=k3XrK3WdyqX2TXga-yFdXfA7hAutiHWPyRXU-2txMqk,2
|
|
|
28
29
|
seldon_runtime/tool_wire.py,sha256=s2x3TgbjP26hwe_twAS-ArlbLhEXUQUpohK4VoeXgwE,4279
|
|
29
30
|
seldon_runtime/data/o200k_base.tiktoken,sha256=RGqVOMtsNI41FhINfAiwn1fDZJXirP_-WaW_iwz7Gi0,3613922
|
|
30
31
|
seldon_runtime/data/provenance.json,sha256=nagan6hzVlnwrFuCm_HswBgn8EYPQ_Q16-d7FJ7Whk8,847
|
|
31
|
-
seldon_runtime-0.1.1.
|
|
32
|
-
seldon_runtime-0.1.1.
|
|
33
|
-
seldon_runtime-0.1.1.
|
|
34
|
-
seldon_runtime-0.1.1.
|
|
35
|
-
seldon_runtime-0.1.1.
|
|
36
|
-
seldon_runtime-0.1.1.
|
|
37
|
-
seldon_runtime-0.1.1.
|
|
32
|
+
seldon_runtime-0.1.1.dev4.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
33
|
+
seldon_runtime-0.1.1.dev4.dist-info/licenses/NOTICE,sha256=EqBHq_jwh5GaGUPaaOicfkve1QvR9sRF3iHj6BQaZ-k,674
|
|
34
|
+
seldon_runtime-0.1.1.dev4.dist-info/licenses/THIRD_PARTY_NOTICES.md,sha256=Nh_1NO9xL86j3OQFXPbBsO__nZKxBe_RXzRViv2cJpk,3232
|
|
35
|
+
seldon_runtime-0.1.1.dev4.dist-info/METADATA,sha256=aHH1s2aY6ncxpgUBhlVS9TNn30Kqmzu3Hx4NNHfsxuw,17906
|
|
36
|
+
seldon_runtime-0.1.1.dev4.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
37
|
+
seldon_runtime-0.1.1.dev4.dist-info/top_level.txt,sha256=OIOoWE_VYJXj3yVmPF3EeSqNJ5DNWZbmSEWB5JMLcTU,15
|
|
38
|
+
seldon_runtime-0.1.1.dev4.dist-info/RECORD,,
|
|
File without changes
|
{seldon_runtime-0.1.1.dev3.dist-info → seldon_runtime-0.1.1.dev4.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|