seldon-sdk 0.1.0.dev0__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_sdk/__init__.py +23 -0
- seldon_sdk/__init__.pyi +30 -0
- seldon_sdk/auth.py +286 -0
- seldon_sdk/cli.py +344 -0
- seldon_sdk/contract.py +200 -0
- seldon_sdk/decorator.py +114 -0
- seldon_sdk/errors.py +37 -0
- seldon_sdk/files.py +350 -0
- seldon_sdk/local.py +69 -0
- seldon_sdk/lockfile.py +42 -0
- seldon_sdk/management.py +532 -0
- seldon_sdk/preparation.py +225 -0
- seldon_sdk/py.typed +0 -0
- seldon_sdk/runtime.py +375 -0
- seldon_sdk/scanner.py +143 -0
- seldon_sdk/skill/SKILL.md +61 -0
- seldon_sdk/testing.py +32 -0
- seldon_sdk/testing.pyi +12 -0
- seldon_sdk-0.1.0.dev0.dist-info/METADATA +263 -0
- seldon_sdk-0.1.0.dev0.dist-info/RECORD +23 -0
- seldon_sdk-0.1.0.dev0.dist-info/WHEEL +4 -0
- seldon_sdk-0.1.0.dev0.dist-info/entry_points.txt +2 -0
- seldon_sdk-0.1.0.dev0.dist-info/licenses/LICENSE +202 -0
seldon_sdk/contract.py
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"""Explicit typed declarations, bounded schemas and canonical rendering."""
|
|
2
|
+
|
|
3
|
+
import inspect
|
|
4
|
+
import re
|
|
5
|
+
import string
|
|
6
|
+
from typing import get_type_hints
|
|
7
|
+
|
|
8
|
+
from pydantic import ConfigDict, TypeAdapter, create_model
|
|
9
|
+
|
|
10
|
+
from .errors import SeldonContractError
|
|
11
|
+
from .files import ROLES, canonical
|
|
12
|
+
|
|
13
|
+
TOKEN = re.compile(r"\{\{[A-Za-z]\w*\}\}|(?<!\{)\{([A-Za-z]\w*)\}(?!\})")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def template(text, parameters):
|
|
17
|
+
if not isinstance(text, str) or len(text.encode()) > 262144:
|
|
18
|
+
raise SeldonContractError("message_too_large")
|
|
19
|
+
pieces, used, literal_parts = [], [], []
|
|
20
|
+
try:
|
|
21
|
+
parts = list(string.Formatter().parse(text))
|
|
22
|
+
except ValueError:
|
|
23
|
+
raise SeldonContractError("template_syntax_unsupported") from None
|
|
24
|
+
for literal, field, fmt, conversion in parts:
|
|
25
|
+
pieces.append(literal)
|
|
26
|
+
literal_parts.append(literal)
|
|
27
|
+
if field is None:
|
|
28
|
+
continue
|
|
29
|
+
if not re.fullmatch(r"[A-Za-z]\w*", field) or fmt or conversion:
|
|
30
|
+
raise SeldonContractError("template_syntax_unsupported")
|
|
31
|
+
if field not in parameters:
|
|
32
|
+
raise SeldonContractError("unknown_placeholder")
|
|
33
|
+
if field in used:
|
|
34
|
+
raise SeldonContractError("repeated_placeholder_unsupported")
|
|
35
|
+
used.append(field)
|
|
36
|
+
pieces.append("{" + field + "}")
|
|
37
|
+
if TOKEN.search("".join(literal_parts)):
|
|
38
|
+
raise SeldonContractError("literal_braces_unsupported")
|
|
39
|
+
return "".join(pieces), used
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def bounded_schema(schema):
|
|
43
|
+
nodes = 0
|
|
44
|
+
|
|
45
|
+
def walk(value, depth=0, refs=()):
|
|
46
|
+
nonlocal nodes
|
|
47
|
+
nodes += 1
|
|
48
|
+
if depth > 24 or nodes > 2048:
|
|
49
|
+
raise SeldonContractError("schema_complexity_limit")
|
|
50
|
+
if isinstance(value, dict):
|
|
51
|
+
if set(value) & {"pattern", "patternProperties", "$dynamicRef", "$recursiveRef", "$id"}:
|
|
52
|
+
raise SeldonContractError("unsupported_schema_keyword")
|
|
53
|
+
if "$ref" in value:
|
|
54
|
+
ref = value["$ref"]
|
|
55
|
+
if not isinstance(ref, str) or not ref.startswith("#/") or ref in refs:
|
|
56
|
+
raise SeldonContractError("unsupported_schema_reference")
|
|
57
|
+
target = schema
|
|
58
|
+
try:
|
|
59
|
+
for key in ref[2:].split("/"):
|
|
60
|
+
target = target[key.replace("~1", "/").replace("~0", "~")]
|
|
61
|
+
except (KeyError, TypeError):
|
|
62
|
+
raise SeldonContractError("invalid_schema_reference") from None
|
|
63
|
+
walk(target, depth + 1, refs + (ref,))
|
|
64
|
+
for key, child in value.items():
|
|
65
|
+
if key != "$ref":
|
|
66
|
+
walk(child, depth + 1, refs)
|
|
67
|
+
elif isinstance(value, list):
|
|
68
|
+
for child in value:
|
|
69
|
+
walk(child, depth + 1, refs)
|
|
70
|
+
|
|
71
|
+
walk(schema)
|
|
72
|
+
return schema
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def normalized_output(schema):
|
|
76
|
+
bounded_schema(schema)
|
|
77
|
+
|
|
78
|
+
def normalize(value):
|
|
79
|
+
if isinstance(value, list):
|
|
80
|
+
return [normalize(item) for item in value]
|
|
81
|
+
if not isinstance(value, dict):
|
|
82
|
+
return value
|
|
83
|
+
result = {
|
|
84
|
+
key: normalize(item) for key, item in value.items() if key not in {"title", "default"}
|
|
85
|
+
}
|
|
86
|
+
if "properties" in result:
|
|
87
|
+
result["required"] = list(result["properties"])
|
|
88
|
+
result["additionalProperties"] = False
|
|
89
|
+
choices = result.get("anyOf")
|
|
90
|
+
if isinstance(choices, list) and len(choices) == 2:
|
|
91
|
+
nonnull = next((item for item in choices if item.get("type") != "null"), None)
|
|
92
|
+
if {"type": "null"} in choices and nonnull and isinstance(nonnull.get("type"), str):
|
|
93
|
+
result.pop("anyOf")
|
|
94
|
+
result.update(nonnull)
|
|
95
|
+
result["type"] = [nonnull["type"], "null"]
|
|
96
|
+
return result
|
|
97
|
+
|
|
98
|
+
return normalize(schema)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def strict_schema(schema):
|
|
102
|
+
reasons = []
|
|
103
|
+
|
|
104
|
+
def visit(value):
|
|
105
|
+
if isinstance(value, dict):
|
|
106
|
+
if value.get("type") == "object" and value.get("additionalProperties") is not False:
|
|
107
|
+
reasons.append("open_object")
|
|
108
|
+
if not value:
|
|
109
|
+
reasons.append("unconstrained_value")
|
|
110
|
+
for item in value.values():
|
|
111
|
+
visit(item)
|
|
112
|
+
elif isinstance(value, list):
|
|
113
|
+
for item in value:
|
|
114
|
+
visit(item)
|
|
115
|
+
|
|
116
|
+
visit(schema)
|
|
117
|
+
return not reasons, sorted(set(reasons))
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def build(fn, *, model, prompt=None, system=None, messages=None, temperature=0, max_tokens=2048):
|
|
121
|
+
if not isinstance(model, str) or not model:
|
|
122
|
+
raise SeldonContractError("missing_model")
|
|
123
|
+
if (
|
|
124
|
+
isinstance(temperature, bool)
|
|
125
|
+
or not isinstance(temperature, (float, int))
|
|
126
|
+
or not 0 <= temperature <= 2
|
|
127
|
+
):
|
|
128
|
+
raise SeldonContractError("invalid_temperature")
|
|
129
|
+
if type(max_tokens) is not int or max_tokens < 1:
|
|
130
|
+
raise SeldonContractError("invalid_max_tokens")
|
|
131
|
+
signature = inspect.signature(fn)
|
|
132
|
+
hints = get_type_hints(fn)
|
|
133
|
+
fields = {}
|
|
134
|
+
for name, parameter in signature.parameters.items():
|
|
135
|
+
if name in {"self", "cls"} or parameter.kind in {
|
|
136
|
+
parameter.POSITIONAL_ONLY,
|
|
137
|
+
parameter.VAR_POSITIONAL,
|
|
138
|
+
parameter.VAR_KEYWORD,
|
|
139
|
+
}:
|
|
140
|
+
raise SeldonContractError("unsupported_signature")
|
|
141
|
+
if name not in hints:
|
|
142
|
+
raise SeldonContractError("input_annotation_required")
|
|
143
|
+
fields[name] = (
|
|
144
|
+
hints[name],
|
|
145
|
+
... if parameter.default is parameter.empty else parameter.default,
|
|
146
|
+
)
|
|
147
|
+
if "return" not in hints:
|
|
148
|
+
raise SeldonContractError("output_annotation_required")
|
|
149
|
+
inputs = create_model("Inputs", __config__=ConfigDict(extra="forbid"), **fields)
|
|
150
|
+
output = TypeAdapter(hints["return"])
|
|
151
|
+
input_schema = bounded_schema(inputs.model_json_schema())
|
|
152
|
+
output_schema = normalized_output(output.json_schema())
|
|
153
|
+
if output_schema.get("type") not in {"object", "array"}:
|
|
154
|
+
raise SeldonContractError("unsupported_output_type")
|
|
155
|
+
if messages is not None and (prompt is not None or system is not None):
|
|
156
|
+
raise SeldonContractError("conflicting_templates")
|
|
157
|
+
selected = (
|
|
158
|
+
messages
|
|
159
|
+
if messages is not None
|
|
160
|
+
else ([("system", system)] if system else []) + [("user", prompt)]
|
|
161
|
+
)
|
|
162
|
+
if not isinstance(selected, (list, tuple)) or not 1 <= len(selected) <= 32:
|
|
163
|
+
raise SeldonContractError("message_limit")
|
|
164
|
+
prepared, used = [], set()
|
|
165
|
+
for message in selected:
|
|
166
|
+
if not isinstance(message, (list, tuple)) or len(message) != 2 or message[0] not in ROLES:
|
|
167
|
+
raise SeldonContractError("unsupported_message_role")
|
|
168
|
+
content, names = template(message[1], fields)
|
|
169
|
+
used.update(names)
|
|
170
|
+
prepared.append({"role": message[0], "content": content})
|
|
171
|
+
if set(fields) != used:
|
|
172
|
+
raise SeldonContractError("unused_parameter")
|
|
173
|
+
strict, reasons = strict_schema(output_schema)
|
|
174
|
+
contract = {
|
|
175
|
+
"version": "seldon-sdk-contract-v1",
|
|
176
|
+
"model": model,
|
|
177
|
+
"temperature": temperature,
|
|
178
|
+
"max_tokens": max_tokens,
|
|
179
|
+
"messages": prepared,
|
|
180
|
+
"input_schema": input_schema,
|
|
181
|
+
"output_schema": output_schema,
|
|
182
|
+
"response_format": {
|
|
183
|
+
"type": "json_schema",
|
|
184
|
+
"json_schema": {"name": "output", "strict": strict, "schema": output_schema},
|
|
185
|
+
},
|
|
186
|
+
}
|
|
187
|
+
if len(canonical(contract).encode()) > 262144:
|
|
188
|
+
raise SeldonContractError("request_too_large")
|
|
189
|
+
return signature, inputs, output, contract, reasons
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def render(contract, inputs):
|
|
193
|
+
def replace(match):
|
|
194
|
+
value = inputs[match.group(1)]
|
|
195
|
+
return value if isinstance(value, str) else canonical(value)
|
|
196
|
+
|
|
197
|
+
return [
|
|
198
|
+
{"role": item["role"], "content": TOKEN.sub(replace, item["content"])}
|
|
199
|
+
for item in contract["messages"]
|
|
200
|
+
]
|
seldon_sdk/decorator.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""Typed single-operation declarations; function bodies are never executed."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import contextvars
|
|
5
|
+
import functools
|
|
6
|
+
import inspect
|
|
7
|
+
|
|
8
|
+
from pydantic import ValidationError
|
|
9
|
+
|
|
10
|
+
from .contract import build
|
|
11
|
+
from .errors import SeldonConfigError, SeldonInputError
|
|
12
|
+
from .files import bounded_json, digest, resolve_function
|
|
13
|
+
from .runtime import Breaker, invoke
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Function:
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
fn,
|
|
20
|
+
*,
|
|
21
|
+
model=None,
|
|
22
|
+
prompt=None,
|
|
23
|
+
system=None,
|
|
24
|
+
messages=None,
|
|
25
|
+
name=None,
|
|
26
|
+
id=None,
|
|
27
|
+
example=None,
|
|
28
|
+
temperature=0,
|
|
29
|
+
max_tokens=2048,
|
|
30
|
+
**options,
|
|
31
|
+
):
|
|
32
|
+
functools.update_wrapper(self, fn)
|
|
33
|
+
self.original = fn
|
|
34
|
+
self.signature, self.inputs, self.output, self.contract, self.strict_reasons = build(
|
|
35
|
+
fn,
|
|
36
|
+
model=model,
|
|
37
|
+
prompt=prompt,
|
|
38
|
+
system=system,
|
|
39
|
+
messages=messages,
|
|
40
|
+
temperature=temperature,
|
|
41
|
+
max_tokens=max_tokens,
|
|
42
|
+
)
|
|
43
|
+
self.digest = digest(self.contract)
|
|
44
|
+
self.key = name or fn.__module__ + ":" + fn.__qualname__
|
|
45
|
+
self.id = resolve_function(id) if id else None
|
|
46
|
+
self.example = example
|
|
47
|
+
self.options = options
|
|
48
|
+
self.breaker = Breaker()
|
|
49
|
+
self.stub = contextvars.ContextVar("seldon_stub", default=None)
|
|
50
|
+
self.is_async = inspect.iscoroutinefunction(fn)
|
|
51
|
+
|
|
52
|
+
def bind(self, *args, **kwargs):
|
|
53
|
+
try:
|
|
54
|
+
values = self.signature.bind(*args, **kwargs)
|
|
55
|
+
values.apply_defaults()
|
|
56
|
+
result = self.inputs.model_validate(values.arguments, strict=True).model_dump(
|
|
57
|
+
mode="json"
|
|
58
|
+
)
|
|
59
|
+
bounded_json(result)
|
|
60
|
+
return result
|
|
61
|
+
except (TypeError, ValueError, ValidationError):
|
|
62
|
+
raise SeldonInputError("invalid_function_inputs") from None
|
|
63
|
+
|
|
64
|
+
def export(self):
|
|
65
|
+
if self.example is None:
|
|
66
|
+
raise SeldonInputError("example_required")
|
|
67
|
+
example = self.inputs.model_validate(self.example, strict=True).model_dump(mode="json")
|
|
68
|
+
return {
|
|
69
|
+
"kind": "sdk",
|
|
70
|
+
"key": self.key,
|
|
71
|
+
"contract": self.contract,
|
|
72
|
+
"digest": self.digest,
|
|
73
|
+
"example_inputs": example,
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async def _call(self, args, kwargs, *, with_info=False):
|
|
77
|
+
inputs = self.bind(*args, **kwargs)
|
|
78
|
+
stub = self.stub.get()
|
|
79
|
+
if stub is not None:
|
|
80
|
+
from .runtime import CallInfo
|
|
81
|
+
|
|
82
|
+
result = stub(inputs)
|
|
83
|
+
info = CallInfo(path="stub")
|
|
84
|
+
elif self.options.get("local"):
|
|
85
|
+
from .local import invoke_local
|
|
86
|
+
|
|
87
|
+
result, info = await invoke_local(self, inputs)
|
|
88
|
+
else:
|
|
89
|
+
result, info = await invoke(self, inputs, async_mode=self.is_async)
|
|
90
|
+
return (result, info) if with_info else result
|
|
91
|
+
|
|
92
|
+
def _dispatch(self, args, kwargs, with_info):
|
|
93
|
+
if self.is_async:
|
|
94
|
+
return self._call(args, kwargs, with_info=with_info)
|
|
95
|
+
try:
|
|
96
|
+
asyncio.get_running_loop()
|
|
97
|
+
except RuntimeError:
|
|
98
|
+
return asyncio.run(self._call(args, kwargs, with_info=with_info))
|
|
99
|
+
raise SeldonConfigError("sync_function_inside_event_loop")
|
|
100
|
+
|
|
101
|
+
async def aclose(self):
|
|
102
|
+
from .local import close_local
|
|
103
|
+
|
|
104
|
+
await close_local(self)
|
|
105
|
+
|
|
106
|
+
def __call__(self, *args, **kwargs):
|
|
107
|
+
return self._dispatch(args, kwargs, False)
|
|
108
|
+
|
|
109
|
+
def call_with_info(self, *args, **kwargs):
|
|
110
|
+
return self._dispatch(args, kwargs, True)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def function(**settings):
|
|
114
|
+
return lambda fn: Function(fn, **settings)
|
seldon_sdk/errors.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Bounded errors that never include credentials or supplied payloads."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class SeldonError(Exception):
|
|
5
|
+
def __init__(self, code, *, status=None, correlation_id=None):
|
|
6
|
+
super().__init__(code)
|
|
7
|
+
self.code = code
|
|
8
|
+
self.status = status
|
|
9
|
+
self.correlation_id = correlation_id
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class SeldonConfigError(SeldonError):
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class SeldonContractError(SeldonError):
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class SeldonInputError(SeldonContractError):
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class SeldonAuthError(SeldonError):
|
|
25
|
+
pass
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class SeldonSpendError(SeldonError):
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class SeldonOutputValidationError(SeldonError):
|
|
33
|
+
pass
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class SeldonReferenceError(SeldonError):
|
|
37
|
+
pass
|
seldon_sdk/files.py
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
"""Bounded JSON preparation and non-secret local operation associations."""
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import json
|
|
5
|
+
import os
|
|
6
|
+
import re
|
|
7
|
+
import tempfile
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from uuid import UUID
|
|
10
|
+
|
|
11
|
+
from .errors import SeldonContractError
|
|
12
|
+
|
|
13
|
+
MAX_FILE_BYTES = 5 * 1024 * 1024
|
|
14
|
+
ROLES = {"system", "developer", "user", "assistant"}
|
|
15
|
+
REQUEST_FIELDS = {
|
|
16
|
+
"model",
|
|
17
|
+
"messages",
|
|
18
|
+
"response_format",
|
|
19
|
+
"temperature",
|
|
20
|
+
"max_tokens",
|
|
21
|
+
"max_completion_tokens",
|
|
22
|
+
"stream",
|
|
23
|
+
"n",
|
|
24
|
+
"top_p",
|
|
25
|
+
"frequency_penalty",
|
|
26
|
+
"presence_penalty",
|
|
27
|
+
"tools",
|
|
28
|
+
"tool_choice",
|
|
29
|
+
"parallel_tool_calls",
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def canonical(value):
|
|
34
|
+
return json.dumps(
|
|
35
|
+
value, sort_keys=True, separators=(",", ":"), ensure_ascii=False, allow_nan=False
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def digest(value):
|
|
40
|
+
return hashlib.sha256(canonical(value).encode()).hexdigest()
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _invalid_constant(_value):
|
|
44
|
+
raise SeldonContractError("nonfinite_json")
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def read_json(path):
|
|
48
|
+
try:
|
|
49
|
+
with Path(path).open("rb") as source:
|
|
50
|
+
raw = source.read(MAX_FILE_BYTES + 1)
|
|
51
|
+
if len(raw) > MAX_FILE_BYTES:
|
|
52
|
+
raise SeldonContractError("file_too_large")
|
|
53
|
+
return json.loads(raw, parse_constant=_invalid_constant)
|
|
54
|
+
except (OSError, UnicodeError, json.JSONDecodeError):
|
|
55
|
+
raise SeldonContractError("invalid_json_file") from None
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def atomic_json(path, data):
|
|
59
|
+
path = Path(path)
|
|
60
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
61
|
+
fd, temporary = tempfile.mkstemp(dir=path.parent)
|
|
62
|
+
try:
|
|
63
|
+
with os.fdopen(fd, "w") as output:
|
|
64
|
+
output.write(canonical(data) + "\n")
|
|
65
|
+
os.replace(temporary, path)
|
|
66
|
+
finally:
|
|
67
|
+
if os.path.exists(temporary):
|
|
68
|
+
os.unlink(temporary)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def bounded_json(value):
|
|
72
|
+
count = 0
|
|
73
|
+
|
|
74
|
+
def visit(item, depth):
|
|
75
|
+
nonlocal count
|
|
76
|
+
count += 1
|
|
77
|
+
if depth > 32 or count > 20000:
|
|
78
|
+
raise SeldonContractError("json_complexity_limit")
|
|
79
|
+
if isinstance(item, dict):
|
|
80
|
+
if not all(isinstance(key, str) for key in item):
|
|
81
|
+
raise SeldonContractError("invalid_json_keys")
|
|
82
|
+
for child in item.values():
|
|
83
|
+
visit(child, depth + 1)
|
|
84
|
+
elif isinstance(item, list):
|
|
85
|
+
for child in item:
|
|
86
|
+
visit(child, depth + 1)
|
|
87
|
+
|
|
88
|
+
visit(value, 0)
|
|
89
|
+
try:
|
|
90
|
+
if len(canonical(value).encode()) > MAX_FILE_BYTES:
|
|
91
|
+
raise SeldonContractError("file_too_large")
|
|
92
|
+
except (ValueError, TypeError):
|
|
93
|
+
raise SeldonContractError("invalid_json") from None
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _request(call):
|
|
97
|
+
if not isinstance(call, dict) or set(call) - REQUEST_FIELDS - {"id"}:
|
|
98
|
+
raise SeldonContractError("unsupported_request_option")
|
|
99
|
+
if not isinstance(call.get("model"), str) or not call["model"]:
|
|
100
|
+
raise SeldonContractError("model_required")
|
|
101
|
+
messages = call.get("messages")
|
|
102
|
+
if not isinstance(messages, list) or not 1 <= len(messages) <= 32:
|
|
103
|
+
raise SeldonContractError("invalid_messages")
|
|
104
|
+
for message in messages:
|
|
105
|
+
if not isinstance(message, dict) or set(message) != {"role", "content"}:
|
|
106
|
+
raise SeldonContractError("invalid_message")
|
|
107
|
+
if message["role"] not in ROLES or not isinstance(message["content"], str):
|
|
108
|
+
raise SeldonContractError("invalid_message")
|
|
109
|
+
if len(message["content"]) > 262144:
|
|
110
|
+
raise SeldonContractError("message_too_large")
|
|
111
|
+
for name, neutral in (
|
|
112
|
+
("stream", False),
|
|
113
|
+
("n", 1),
|
|
114
|
+
("top_p", 1),
|
|
115
|
+
("frequency_penalty", 0),
|
|
116
|
+
("presence_penalty", 0),
|
|
117
|
+
):
|
|
118
|
+
if name in call and (
|
|
119
|
+
call[name] != neutral or isinstance(call[name], bool) != isinstance(neutral, bool)
|
|
120
|
+
):
|
|
121
|
+
raise SeldonContractError("unsupported_request_option")
|
|
122
|
+
for name in ("max_tokens", "max_completion_tokens"):
|
|
123
|
+
if (
|
|
124
|
+
name in call
|
|
125
|
+
and call[name] is not None
|
|
126
|
+
and (type(call[name]) is not int or call[name] <= 0)
|
|
127
|
+
):
|
|
128
|
+
raise SeldonContractError("invalid_output_limit")
|
|
129
|
+
if "max_tokens" in call and "max_completion_tokens" in call:
|
|
130
|
+
if call["max_tokens"] != call["max_completion_tokens"]:
|
|
131
|
+
raise SeldonContractError("conflicting_output_limits")
|
|
132
|
+
if "temperature" in call and call["temperature"] is not None:
|
|
133
|
+
value = call["temperature"]
|
|
134
|
+
if isinstance(value, bool) or not isinstance(value, (int, float)) or not 0 <= value <= 2:
|
|
135
|
+
raise SeldonContractError("invalid_temperature")
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def validate_source(source):
|
|
139
|
+
"""Validate the public source envelope without defaulting or rewriting its contents.
|
|
140
|
+
|
|
141
|
+
The server remains authoritative for evaluator, provider and runtime eligibility.
|
|
142
|
+
"""
|
|
143
|
+
bounded_json(source)
|
|
144
|
+
if isinstance(source, list):
|
|
145
|
+
calls = source
|
|
146
|
+
elif isinstance(source, dict) and "calls" in source:
|
|
147
|
+
allowed = {
|
|
148
|
+
"version",
|
|
149
|
+
"calls",
|
|
150
|
+
"inputs",
|
|
151
|
+
"input_schema",
|
|
152
|
+
"bindings",
|
|
153
|
+
"output_schema",
|
|
154
|
+
"evaluator",
|
|
155
|
+
"rubric",
|
|
156
|
+
"wire_mode",
|
|
157
|
+
"evaluation",
|
|
158
|
+
"evaluation_pending",
|
|
159
|
+
"template_syntax",
|
|
160
|
+
"samples",
|
|
161
|
+
"assumptions",
|
|
162
|
+
"preparation",
|
|
163
|
+
"output_token_policy",
|
|
164
|
+
"reference_binding",
|
|
165
|
+
}
|
|
166
|
+
if set(source) - allowed:
|
|
167
|
+
raise SeldonContractError("unsupported_source_option")
|
|
168
|
+
calls = source["calls"]
|
|
169
|
+
elif isinstance(source, dict) and source.get("kind") == "template":
|
|
170
|
+
allowed = {
|
|
171
|
+
"kind",
|
|
172
|
+
"model",
|
|
173
|
+
"messages",
|
|
174
|
+
"syntax",
|
|
175
|
+
"example_inputs",
|
|
176
|
+
"output_schema",
|
|
177
|
+
"response_format",
|
|
178
|
+
"temperature",
|
|
179
|
+
"max_tokens",
|
|
180
|
+
"samples",
|
|
181
|
+
"tools",
|
|
182
|
+
"tool_choice",
|
|
183
|
+
"parallel_tool_calls",
|
|
184
|
+
"evaluation",
|
|
185
|
+
}
|
|
186
|
+
if set(source) - allowed or source.get("syntax") not in {
|
|
187
|
+
"python_format",
|
|
188
|
+
"jinja_variables",
|
|
189
|
+
}:
|
|
190
|
+
raise SeldonContractError("invalid_template_source")
|
|
191
|
+
_request({key: value for key, value in source.items() if key in REQUEST_FIELDS})
|
|
192
|
+
return source
|
|
193
|
+
elif isinstance(source, dict):
|
|
194
|
+
_request(source)
|
|
195
|
+
return source
|
|
196
|
+
else:
|
|
197
|
+
raise SeldonContractError("invalid_source")
|
|
198
|
+
if not isinstance(calls, list) or not 1 <= len(calls) <= 3:
|
|
199
|
+
raise SeldonContractError("workflow_call_limit")
|
|
200
|
+
for call in calls:
|
|
201
|
+
_request(call)
|
|
202
|
+
return source
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def resolve_function(locator, lockfile=None):
|
|
206
|
+
if not isinstance(locator, str):
|
|
207
|
+
raise SeldonContractError("function_identity_required")
|
|
208
|
+
if locator.startswith("seldon/function/"):
|
|
209
|
+
locator = locator[len("seldon/function/") :]
|
|
210
|
+
try:
|
|
211
|
+
return str(UUID(locator))
|
|
212
|
+
except (ValueError, TypeError):
|
|
213
|
+
from .lockfile import locate
|
|
214
|
+
|
|
215
|
+
path = Path(lockfile) if lockfile else locate(None)
|
|
216
|
+
if path.exists():
|
|
217
|
+
value = read_json(path)
|
|
218
|
+
if isinstance(value, dict) and value.get("version") == "seldon-lock-v1":
|
|
219
|
+
entries = value.get("functions", {})
|
|
220
|
+
entry = entries.get(locator) if isinstance(entries, dict) else None
|
|
221
|
+
if isinstance(entry, dict):
|
|
222
|
+
try:
|
|
223
|
+
return str(UUID(entry.get("function_id", "")))
|
|
224
|
+
except (ValueError, TypeError, AttributeError):
|
|
225
|
+
pass
|
|
226
|
+
raise SeldonContractError("function_identity_required") from None
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
class Association:
|
|
230
|
+
def __init__(self, path):
|
|
231
|
+
self.path = Path(path)
|
|
232
|
+
|
|
233
|
+
def read(self):
|
|
234
|
+
if not self.path.exists():
|
|
235
|
+
return {}
|
|
236
|
+
value = read_json(self.path)
|
|
237
|
+
if not isinstance(value, dict) or value.get("version") != "seldon-operation-v1":
|
|
238
|
+
raise SeldonContractError("invalid_association")
|
|
239
|
+
return value
|
|
240
|
+
|
|
241
|
+
def save(self, values):
|
|
242
|
+
allowed = {
|
|
243
|
+
"source_file",
|
|
244
|
+
"source_digest",
|
|
245
|
+
"function_id",
|
|
246
|
+
"workspace_id",
|
|
247
|
+
"revision",
|
|
248
|
+
"run_id",
|
|
249
|
+
"revision_id",
|
|
250
|
+
"pending_create",
|
|
251
|
+
"command_id",
|
|
252
|
+
"cap_usd",
|
|
253
|
+
"base_url",
|
|
254
|
+
"evidence_digest",
|
|
255
|
+
"evidence_file",
|
|
256
|
+
"evaluation_command_id",
|
|
257
|
+
"evaluation_revision",
|
|
258
|
+
}
|
|
259
|
+
if set(values) - allowed:
|
|
260
|
+
raise SeldonContractError("invalid_association_field")
|
|
261
|
+
atomic_json(self.path, {"version": "seldon-operation-v1", **values})
|
|
262
|
+
|
|
263
|
+
def update(self, **values):
|
|
264
|
+
old = self.read()
|
|
265
|
+
old.pop("version", None)
|
|
266
|
+
self.save({**old, **values})
|
|
267
|
+
|
|
268
|
+
def require_source(self, source_digest):
|
|
269
|
+
state = self.read()
|
|
270
|
+
if state.get("source_digest") not in (None, source_digest):
|
|
271
|
+
raise SeldonContractError("source_changed_reimport_required")
|
|
272
|
+
return state
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def safe_code(value, default="service_error"):
|
|
276
|
+
return (
|
|
277
|
+
value
|
|
278
|
+
if isinstance(value, str) and re.fullmatch(r"[a-z][a-z0-9_]{0,79}", value)
|
|
279
|
+
else default
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def read_examples(path):
|
|
284
|
+
path = Path(path)
|
|
285
|
+
if path.suffix.lower() == ".jsonl":
|
|
286
|
+
try:
|
|
287
|
+
if path.stat().st_size > MAX_FILE_BYTES:
|
|
288
|
+
raise SeldonContractError("file_too_large")
|
|
289
|
+
examples = [
|
|
290
|
+
json.loads(line, parse_constant=_invalid_constant)
|
|
291
|
+
for line in path.read_text().splitlines()
|
|
292
|
+
if line.strip()
|
|
293
|
+
]
|
|
294
|
+
except (OSError, ValueError):
|
|
295
|
+
raise SeldonContractError("invalid_examples_file") from None
|
|
296
|
+
else:
|
|
297
|
+
examples = read_json(path)
|
|
298
|
+
if not isinstance(examples, list) or len(examples) > 400:
|
|
299
|
+
raise SeldonContractError("evaluation_examples_limit")
|
|
300
|
+
bounded_json(examples)
|
|
301
|
+
seen = set()
|
|
302
|
+
for index, example in enumerate(examples):
|
|
303
|
+
allowed = {
|
|
304
|
+
"input",
|
|
305
|
+
"output",
|
|
306
|
+
"expected",
|
|
307
|
+
"assertions",
|
|
308
|
+
"group",
|
|
309
|
+
"weight",
|
|
310
|
+
"role",
|
|
311
|
+
"value",
|
|
312
|
+
"input_origin",
|
|
313
|
+
"label_origin",
|
|
314
|
+
}
|
|
315
|
+
if (
|
|
316
|
+
not isinstance(example, dict)
|
|
317
|
+
or set(example) - allowed
|
|
318
|
+
or not isinstance(example.get("input"), dict)
|
|
319
|
+
):
|
|
320
|
+
raise SeldonContractError("invalid_example_row_" + str(index))
|
|
321
|
+
if example.get("input_origin", "uploaded") not in {"synthetic", "uploaded", "recorded"}:
|
|
322
|
+
raise SeldonContractError("invalid_example_input_origin")
|
|
323
|
+
if example.get("label_origin", "unknown") not in {"human", "reference", "judge", "unknown"}:
|
|
324
|
+
raise SeldonContractError("invalid_example_label_origin")
|
|
325
|
+
if example.get("role") not in {None, "training", "development", "confirmation"}:
|
|
326
|
+
raise SeldonContractError("invalid_example_role")
|
|
327
|
+
for name in ("weight", "value"):
|
|
328
|
+
number = example.get(name)
|
|
329
|
+
if number is not None and (
|
|
330
|
+
isinstance(number, bool)
|
|
331
|
+
or not isinstance(number, (int, float))
|
|
332
|
+
or number < 0
|
|
333
|
+
or (name == "weight" and number == 0)
|
|
334
|
+
):
|
|
335
|
+
raise SeldonContractError("invalid_example_" + name)
|
|
336
|
+
assertions = example.get("assertions", [])
|
|
337
|
+
if (
|
|
338
|
+
not isinstance(assertions, list)
|
|
339
|
+
or len(assertions) > 6
|
|
340
|
+
or any(
|
|
341
|
+
not isinstance(item, str) or not item.strip() or len(item) > 2048
|
|
342
|
+
for item in assertions
|
|
343
|
+
)
|
|
344
|
+
):
|
|
345
|
+
raise SeldonContractError("invalid_example_assertions")
|
|
346
|
+
identity = digest(example["input"])
|
|
347
|
+
if identity in seen:
|
|
348
|
+
raise SeldonContractError("duplicate_example_row_" + str(index))
|
|
349
|
+
seen.add(identity)
|
|
350
|
+
return examples
|