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/local.py ADDED
@@ -0,0 +1,69 @@
1
+ """Typed adapter over the public Runner; it owns eligibility and local fallback."""
2
+
3
+ import asyncio
4
+ import time
5
+ import weakref
6
+
7
+ from .errors import SeldonConfigError, SeldonError, SeldonOutputValidationError
8
+ from .files import safe_code
9
+ from .runtime import CallInfo, options_for, resolve_identity
10
+
11
+
12
+ async def invoke_local(fn, inputs):
13
+ started = time.monotonic()
14
+ options = options_for(fn)
15
+ identity, reason = resolve_identity(fn)
16
+ if reason:
17
+ raise SeldonConfigError("registered_function_required_for_local_execution")
18
+ try:
19
+ from seldon_runtime.runner import Runner
20
+ except ImportError:
21
+ raise SeldonConfigError("install_seldon_runtime_gliner_extra") from None
22
+ loop = asyncio.get_running_loop()
23
+ if not hasattr(fn, "local_runners"):
24
+ fn.local_runners = weakref.WeakKeyDictionary()
25
+ runner = fn.local_runners.get(loop)
26
+ if runner is None:
27
+ # load() includes eligibility, integrity and conformance checks. Never opt out here.
28
+ runner = await Runner.load(
29
+ identity,
30
+ options["api_key"],
31
+ base_url=options["base_url"].removesuffix("/v1"),
32
+ fallback=fn.options.get("local_fallback", "seldon"),
33
+ telemetry=fn.options.get("telemetry", True),
34
+ )
35
+ fn.local_runners[loop] = runner
36
+ info = CallInfo(path="local", function_id=identity)
37
+ try:
38
+ result = await runner(inputs)
39
+ info.revision_id = runner.status()["revision_id"]
40
+ info.server_fallback_used = result["fallback_used"]
41
+ info.reason = result["fallback_reason"]
42
+ try:
43
+ output = fn.output.validate_python(result["output"], strict=True)
44
+ except ValueError:
45
+ raise SeldonOutputValidationError("invalid_local_output") from None
46
+ return output, info
47
+ except SeldonError:
48
+ raise
49
+ except Exception as exc:
50
+ raise SeldonError(safe_code(str(exc), "local_execution_unavailable")) from None
51
+ finally:
52
+ if not fn.is_async:
53
+ await runner.aclose()
54
+ fn.local_runners.pop(loop, None)
55
+ info.latency_ms = (time.monotonic() - started) * 1000
56
+ hook = options.get("on_call")
57
+ if hook:
58
+ try:
59
+ hook(info)
60
+ except Exception:
61
+ import logging
62
+
63
+ logging.getLogger(__name__).warning("call_metadata_hook_failed")
64
+
65
+
66
+ async def close_local(fn):
67
+ runner = getattr(fn, "local_runners", {}).pop(asyncio.get_running_loop(), None)
68
+ if runner is not None:
69
+ await runner.aclose()
seldon_sdk/lockfile.py ADDED
@@ -0,0 +1,42 @@
1
+ """Explicit identity mapping; local files never contain API credentials."""
2
+
3
+ import os
4
+ import re
5
+ from pathlib import Path
6
+
7
+ from .errors import SeldonConfigError
8
+ from .files import atomic_json, read_json, resolve_function
9
+
10
+
11
+ def locate(module_file):
12
+ override = os.environ.get("SELDON_LOCKFILE")
13
+ if override:
14
+ return Path(override)
15
+ start = Path(module_file).resolve().parent if module_file else Path.cwd()
16
+ for parent in (start, *start.parents):
17
+ candidate = parent / "seldon.lock.json"
18
+ if candidate.exists():
19
+ return candidate
20
+ return start / "seldon.lock.json"
21
+
22
+
23
+ def read(path):
24
+ try:
25
+ value = read_json(path)
26
+ if value.get("version") != "seldon-lock-v1" or not isinstance(value.get("functions"), dict):
27
+ raise ValueError
28
+ for item in value["functions"].values():
29
+ resolve_function(item["function_id"])
30
+ if not isinstance(item["contract_digest"], str) or not re.fullmatch(
31
+ r"[0-9a-f]{64}", item["contract_digest"]
32
+ ):
33
+ raise ValueError
34
+ return value
35
+ except Exception:
36
+ raise SeldonConfigError("invalid_lockfile") from None
37
+
38
+
39
+ def set_entry(path, key, entry):
40
+ value = read(path) if Path(path).exists() else {"version": "seldon-lock-v1", "functions": {}}
41
+ value["functions"][key] = entry
42
+ atomic_json(path, value)
@@ -0,0 +1,532 @@
1
+ """Explicit management operations with durable request identities and no mutation retries."""
2
+
3
+ import math
4
+ import time
5
+ from decimal import Decimal, InvalidOperation
6
+ from pathlib import Path
7
+ from uuid import UUID, uuid4
8
+
9
+ import httpx
10
+
11
+ from .errors import SeldonAuthError, SeldonContractError, SeldonError, SeldonSpendError
12
+ from .files import Association, digest, read_json, resolve_function, safe_code, validate_source
13
+
14
+ FUNCTIONS = "/v1/seldon/functions"
15
+
16
+
17
+ class Management:
18
+ def __init__(self, login, *, association=".seldon/operation.json", http=None):
19
+ self.login = login
20
+ self.origin = login.origin
21
+ self.http = http or login.http
22
+ self.association = Association(association)
23
+
24
+ def request(self, method, path, body=None, *, params=None):
25
+ try:
26
+ response = self.http.request(
27
+ method,
28
+ self.origin + path,
29
+ json=body,
30
+ params=params,
31
+ headers={"Authorization": "Bearer " + self.login.access_token()},
32
+ timeout=30,
33
+ follow_redirects=False,
34
+ )
35
+ except httpx.HTTPError:
36
+ raise SeldonError(
37
+ "request_outcome_unknown" if method != "GET" else "service_unavailable"
38
+ ) from None
39
+ try:
40
+ data = response.json() if response.content else {}
41
+ except ValueError:
42
+ raise SeldonError("invalid_service_response", status=response.status_code) from None
43
+ if response.status_code >= 300:
44
+ envelope = data.get("detail", data) if isinstance(data, dict) else {}
45
+ envelope = envelope if isinstance(envelope, dict) else {}
46
+ code = safe_code(envelope.get("error"))
47
+ error = (
48
+ SeldonAuthError
49
+ if response.status_code in (401, 403)
50
+ else SeldonSpendError
51
+ if response.status_code == 402
52
+ else SeldonError
53
+ )
54
+ raise error(code, status=response.status_code)
55
+ if not isinstance(data, dict):
56
+ raise SeldonError("invalid_service_response")
57
+ return data
58
+
59
+ def detail(self, function, *, run_id=None):
60
+ function = resolve_function(function)
61
+ params = {"run_id": str(UUID(run_id))} if run_id else None
62
+ detail = self.request("GET", FUNCTIONS + "/" + function, params=params)
63
+ state = self.association.read()
64
+ if state.get("function_id") == function:
65
+ if state.get("base_url") not in (None, self.origin):
66
+ raise SeldonAuthError("association_host_mismatch")
67
+ if state.get("workspace_id") not in (None, str(detail.get("workspace_id"))):
68
+ raise SeldonAuthError("association_workspace_mismatch")
69
+ return detail
70
+
71
+ def import_source(self, path, *, function=None, name="Selected operation", offline=False):
72
+ source = validate_source(read_json(path))
73
+ source_digest = digest(source)
74
+ state = self.association.read()
75
+ preview = {
76
+ "source_file": str(path),
77
+ "fields": sorted(source) if isinstance(source, dict) else ["calls"],
78
+ "source_digest": source_digest,
79
+ "destination": self.origin + FUNCTIONS,
80
+ }
81
+ if offline:
82
+ return {
83
+ **preview,
84
+ "status": "validated_offline",
85
+ "next_action": "dashboard_import_or_login",
86
+ }
87
+ try:
88
+ self.login.access_token()
89
+ except SeldonAuthError as exc:
90
+ if exc.code == "login_required":
91
+ return {
92
+ **preview,
93
+ "status": "validated_offline",
94
+ "next_action": "dashboard_import_or_login",
95
+ }
96
+ raise
97
+ if state.get("pending_create") and not function:
98
+ raise SeldonContractError("uncertain_create_reconcile_function_list")
99
+ body = {"name": name, "source": source}
100
+ if function:
101
+ function = resolve_function(function)
102
+ detail = self.detail(function)
103
+ if state.get("function_id") == function and state.get("source_digest") == source_digest:
104
+ if state.get("revision") == detail.get("revision"):
105
+ return {
106
+ **preview,
107
+ "function_id": function,
108
+ "status": "unchanged",
109
+ "contract_digest": detail.get("selected_contract_digest"),
110
+ }
111
+ body["expected_revision"] = detail["revision"]
112
+ result = self.request("PATCH", FUNCTIONS + "/" + function, body)
113
+ else:
114
+ self.association.update(
115
+ source_file=str(Path(path).resolve()),
116
+ source_digest=source_digest,
117
+ pending_create=True,
118
+ base_url=self.origin,
119
+ )
120
+ try:
121
+ result = self.request("POST", FUNCTIONS, body)
122
+ except SeldonError as exc:
123
+ if exc.status is not None and 400 <= exc.status < 500:
124
+ self.association.update(pending_create=False)
125
+ raise
126
+ identity = resolve_function(str(result["id"]))
127
+ self.association.save(
128
+ {
129
+ "source_file": str(Path(path).resolve()),
130
+ "source_digest": source_digest,
131
+ "function_id": identity,
132
+ "workspace_id": str(result["workspace_id"]),
133
+ "revision": result["revision"],
134
+ "pending_create": False,
135
+ "base_url": self.origin,
136
+ }
137
+ )
138
+ return {
139
+ **preview,
140
+ "function_id": identity,
141
+ "revision": result["revision"],
142
+ "status": "imported",
143
+ "contract_digest": result.get("selected_contract_digest"),
144
+ "preview_issues": [
145
+ {"code": safe_code(item.get("code"))} for item in result.get("preview_issues", [])
146
+ ],
147
+ }
148
+
149
+ def _state(self, function):
150
+ state = self.association.read()
151
+ if state.get("function_id") == function:
152
+ path = state.get("source_file")
153
+ if path:
154
+ self.association.require_source(digest(validate_source(read_json(path))))
155
+ if state.get("evidence_file"):
156
+ from .files import read_examples
157
+
158
+ if digest(read_examples(state["evidence_file"])) != state.get("evidence_digest"):
159
+ raise SeldonContractError("evidence_changed_import_required")
160
+ return state
161
+
162
+ def status(self, function=None):
163
+ if function is None:
164
+ from .lockfile import locate, read
165
+
166
+ path = locate(None)
167
+ if path.exists():
168
+ rows = []
169
+ for key, entry in sorted(read(path)["functions"].items()):
170
+ row = self.status(entry["function_id"])
171
+ detail = self.detail(entry["function_id"])
172
+ active = next(
173
+ (
174
+ item
175
+ for item in detail.get("revisions", [])
176
+ if str(item["id"]) == str(detail.get("active_revision"))
177
+ ),
178
+ None,
179
+ )
180
+ row["key"] = key
181
+ row["local_state"] = (
182
+ "pending"
183
+ if active is None
184
+ else "active"
185
+ if active.get("source_digest") == entry["contract_digest"]
186
+ else "stale_lock"
187
+ )
188
+ rows.append(row)
189
+ return {"items": rows}
190
+ return {
191
+ "items": [
192
+ self._summary(item) for item in self.request("GET", FUNCTIONS).get("items", [])
193
+ ]
194
+ }
195
+ function = resolve_function(function)
196
+ detail = self.detail(function)
197
+ result = self._summary(detail)
198
+ result["usage"] = self.request("GET", f"{FUNCTIONS}/{function}/usage", params={"days": 7})
199
+ state = self.association.read()
200
+ if state.get("function_id") == function:
201
+ result["associated_run_id"] = state.get("run_id")
202
+ result["local_state"] = (
203
+ "registered" if state.get("revision") == detail["revision"] else "stale_lock"
204
+ )
205
+ return result
206
+
207
+ @staticmethod
208
+ def _summary(detail):
209
+ return {
210
+ key: detail.get(key)
211
+ for key in (
212
+ "id",
213
+ "revision",
214
+ "active_revision",
215
+ "status",
216
+ "disabled",
217
+ "requests_7d",
218
+ "serving_state",
219
+ )
220
+ }
221
+
222
+ def report(self, function, *, run_id=None):
223
+ function = resolve_function(function)
224
+ state = self.association.read()
225
+ if run_id is None and state.get("function_id") == function:
226
+ run_id = state.get("run_id")
227
+ detail = self.detail(function, run_id=run_id)
228
+ return {
229
+ "function_id": detail["id"],
230
+ "workspace_id": detail["workspace_id"],
231
+ "revision": detail["revision"],
232
+ "run_id": run_id,
233
+ "alternatives": detail.get("alternatives", []),
234
+ "selected_run_id": detail.get("selected_run_id"),
235
+ "selected_contract_digest": detail.get("selected_contract_digest"),
236
+ "diagnostics": detail.get("diagnostics", {}),
237
+ "serving_state": detail.get("serving_state"),
238
+ "runs": [
239
+ run for run in detail.get("runs", []) if run_id is None or str(run["id"]) == run_id
240
+ ],
241
+ "inspection_url": self.origin
242
+ + FUNCTIONS
243
+ + "/"
244
+ + function
245
+ + ("?run_id=" + run_id if run_id else ""),
246
+ }
247
+
248
+ def optimize(
249
+ self,
250
+ function,
251
+ *,
252
+ cap,
253
+ objective="cost",
254
+ max_p95_ms=None,
255
+ authorize=False,
256
+ wait=False,
257
+ timeout=600,
258
+ new_run=False,
259
+ source_run=None,
260
+ sleep=time.sleep,
261
+ clock=time.monotonic,
262
+ ):
263
+ function = resolve_function(function)
264
+ try:
265
+ budget = Decimal(str(cap))
266
+ if not budget.is_finite() or not 0 < budget <= 100:
267
+ raise ValueError
268
+ except (InvalidOperation, ValueError):
269
+ raise SeldonContractError("invalid_cap") from None
270
+ if not math.isfinite(timeout) or timeout < 0:
271
+ raise SeldonContractError("invalid_timeout")
272
+ detail = self.detail(function)
273
+ state = self._state(function)
274
+ run_id = (
275
+ state.get("run_id") if state.get("function_id") == function and not new_run else None
276
+ )
277
+ run = next((run for run in detail.get("runs", []) if str(run["id"]) == run_id), None)
278
+ if run_id and run is None:
279
+ raise SeldonContractError("associated_run_unavailable")
280
+ summary = {
281
+ "function_id": function,
282
+ "authorized_cap_usd": str(budget),
283
+ "run_id": run_id,
284
+ "quote": run.get("quote") if run else None,
285
+ }
286
+ if not authorize:
287
+ return {**summary, "status": "confirmation_required"}, 3
288
+ if run and Decimal(str(run["cap"])) != budget:
289
+ raise SeldonSpendError("run_cap_changed_requires_new_run")
290
+ if run is None:
291
+ command = (
292
+ state.get("command_id")
293
+ if not new_run and state.get("function_id") == function
294
+ else None
295
+ )
296
+ if not command:
297
+ command = str(uuid4())
298
+ self.association.update(
299
+ function_id=function,
300
+ workspace_id=str(detail["workspace_id"]),
301
+ base_url=self.origin,
302
+ command_id=command,
303
+ cap_usd=str(budget),
304
+ run_id=None,
305
+ )
306
+ policy = {"objective": objective}
307
+ if max_p95_ms is not None:
308
+ if not math.isfinite(max_p95_ms) or max_p95_ms <= 0:
309
+ raise SeldonContractError("invalid_latency_target")
310
+ policy["max_p95_ms"] = max_p95_ms
311
+ detail = self.request(
312
+ "POST",
313
+ f"{FUNCTIONS}/{function}/runs",
314
+ {
315
+ "command_id": command,
316
+ "expected_revision": detail["revision"],
317
+ "cap_usd": str(budget),
318
+ "policy": policy,
319
+ **({"source_run_id": str(UUID(source_run))} if source_run else {}),
320
+ },
321
+ )
322
+ runs = detail.get("runs", [])
323
+ if not runs:
324
+ raise SeldonError("run_identity_unavailable")
325
+ run = next((item for item in runs if str(item.get("command_id")) == command), None)
326
+ if run is None:
327
+ raise SeldonError("run_identity_unavailable")
328
+ run_id = str(run["id"])
329
+ self.association.update(run_id=run_id, revision=detail["revision"])
330
+ deadline = clock() + timeout
331
+ submitted = False
332
+ while True:
333
+ if run["state"] == "awaiting_review":
334
+ quote_id = (run.get("quote") or {}).get("id")
335
+ if not quote_id:
336
+ raise SeldonError("quote_unavailable")
337
+ detail = self.request(
338
+ "POST",
339
+ f"{FUNCTIONS}/{function}/runs/{run_id}/optimize",
340
+ {"quote_id": quote_id, "expected_revision": detail["revision"]},
341
+ )
342
+ submitted = True
343
+ run = next(item for item in detail["runs"] if str(item["id"]) == run_id)
344
+ result = {
345
+ "function_id": function,
346
+ "run_id": run_id,
347
+ "state": run["state"],
348
+ "outcome": run.get("outcome"),
349
+ "spent": run.get("spent"),
350
+ "held": run.get("held"),
351
+ "cap": run.get("cap"),
352
+ "optimize_submitted": submitted,
353
+ }
354
+ if (
355
+ run.get("ended_at")
356
+ or run["state"] in {"completed", "failed", "cancelled"}
357
+ or not wait
358
+ ):
359
+ return result, 0
360
+ if clock() >= deadline:
361
+ return {**result, "status": "wait_timeout", "next_action": "resume_same_run"}, 4
362
+ sleep(min(2, max(0, deadline - clock())))
363
+ detail = self.detail(function, run_id=run_id)
364
+ run = next((item for item in detail["runs"] if str(item["id"]) == run_id), None)
365
+ if run is None:
366
+ raise SeldonError("associated_run_unavailable")
367
+
368
+ def cancel(self, function, run_id):
369
+ return self._summary(
370
+ self.request(
371
+ "POST", f"{FUNCTIONS}/{resolve_function(function)}/runs/{UUID(run_id)}/cancel"
372
+ )
373
+ )
374
+
375
+ def activate(
376
+ self,
377
+ function,
378
+ revision,
379
+ *,
380
+ publish=False,
381
+ alternative=None,
382
+ confirm,
383
+ interactive,
384
+ managed_fallback_approved=False,
385
+ ):
386
+ if not interactive:
387
+ raise SeldonAuthError("interactive_owner_confirmation_required")
388
+ self.login.access_token()
389
+ function = resolve_function(function)
390
+ state = self.association.read()
391
+ selected_run = state.get("run_id") if state.get("function_id") == function else None
392
+ detail = self.detail(function, run_id=selected_run)
393
+ if alternative is not None:
394
+ choices = detail.get("alternatives", [])
395
+ if revision is not None or not 1 <= alternative <= len(choices):
396
+ raise SeldonContractError("alternative_unavailable")
397
+ revision = choices[alternative - 1].get("revision_id")
398
+ if revision is None:
399
+ raise SeldonContractError("alternative_not_deployable")
400
+ revision = str(UUID(revision))
401
+ selected = next(
402
+ (item for item in detail.get("revisions", []) if str(item["id"]) == revision), None
403
+ )
404
+ if selected is None:
405
+ raise SeldonContractError("revision_unavailable")
406
+ warnings = selected.get("metrics", {}).get("report", {}).get("warnings", [])
407
+ summary = {
408
+ "function_id": function,
409
+ "revision_id": revision,
410
+ "metrics": selected.get("metrics", {}),
411
+ "warnings": warnings,
412
+ "action": "publish" if publish else "activate",
413
+ "managed_fallback_approved": managed_fallback_approved,
414
+ }
415
+ if not confirm(summary):
416
+ return {"status": "confirmation_required"}, 3
417
+ if publish:
418
+ result = self.request(
419
+ "POST",
420
+ f"{FUNCTIONS}/{function}/revisions/{revision}/publish",
421
+ {"managed_fallback_approved": managed_fallback_approved},
422
+ )
423
+ else:
424
+ result = self.request(
425
+ "POST",
426
+ f"{FUNCTIONS}/{function}/activate",
427
+ {
428
+ "revision_id": revision,
429
+ "expected_active_revision": detail.get("active_revision"),
430
+ "managed_fallback_approved": managed_fallback_approved,
431
+ },
432
+ )
433
+ self.association.update(revision_id=revision)
434
+ return self._summary(result), 0
435
+
436
+ def serve_reference(self, function, *, acknowledge_deadline_risk=False, confirm, interactive):
437
+ if not interactive:
438
+ raise SeldonAuthError("interactive_owner_confirmation_required")
439
+ identity = resolve_function(function)
440
+ detail = self.detail(identity)
441
+ readiness = self.request("GET", f"{FUNCTIONS}/{identity}/reference-readiness")
442
+ if not confirm(
443
+ {
444
+ "action": "serve_reference",
445
+ "function_id": identity,
446
+ "readiness": readiness,
447
+ "inference_billed": True,
448
+ "acknowledge_deadline_risk": acknowledge_deadline_risk,
449
+ }
450
+ ):
451
+ return {"status": "confirmation_required"}, 3
452
+ result = self.request(
453
+ "POST",
454
+ f"{FUNCTIONS}/{identity}/serve-reference",
455
+ {
456
+ "expected_revision": detail["revision"],
457
+ "expected_active_revision": detail.get("active_revision"),
458
+ "acknowledge_deadline_risk": acknowledge_deadline_risk,
459
+ },
460
+ )
461
+ current = self.detail(identity)
462
+ self.association.update(
463
+ function_id=identity,
464
+ revision_id=result["revision_id"],
465
+ run_id=result["run_id"],
466
+ revision=current["revision"],
467
+ workspace_id=str(current["workspace_id"]),
468
+ base_url=self.origin,
469
+ )
470
+ return {**result, "function": self._summary(current)}, 0
471
+
472
+ def import_examples(self, function, path, *, replace=False):
473
+ from .files import read_examples
474
+
475
+ function = resolve_function(function)
476
+ examples = read_examples(path)
477
+ if not replace:
478
+ return {
479
+ "function_id": function,
480
+ "rows": len(examples),
481
+ "mode": "replace",
482
+ "status": "confirmation_required",
483
+ }, 3
484
+ detail = self.detail(function)
485
+ state = self.association.read()
486
+ run_id = state.get("run_id") if state.get("function_id") == function else None
487
+ if run_id:
488
+ evidence_digest = digest(examples)
489
+ same = state.get("evidence_digest") == evidence_digest
490
+ command = state.get("evaluation_command_id") if same else None
491
+ revision = state.get("evaluation_revision") if command else detail["revision"]
492
+ command = command or str(uuid4())
493
+ self.association.update(
494
+ evidence_digest=evidence_digest,
495
+ evidence_file=str(Path(path).resolve()),
496
+ evaluation_command_id=command,
497
+ evaluation_revision=revision,
498
+ )
499
+ result = self.request(
500
+ "POST",
501
+ f"{FUNCTIONS}/{function}/evaluation/examples",
502
+ {
503
+ "command_id": command,
504
+ "run_id": run_id,
505
+ "expected_revision": revision,
506
+ "examples": examples,
507
+ },
508
+ )
509
+ return {
510
+ "function_id": function,
511
+ "run_id": run_id,
512
+ "evaluation_command_id": result["id"],
513
+ "state": result["state"],
514
+ }, 0
515
+ source = detail["source"]
516
+ if source.get("version") != "function-spec-v1":
517
+ raise SeldonContractError("draft_evaluation_requires_full_source")
518
+ source = {
519
+ **source,
520
+ "evaluation": {**(source.get("evaluation") or {}), "examples": examples},
521
+ }
522
+ result = self.request(
523
+ "PATCH",
524
+ f"{FUNCTIONS}/{function}",
525
+ {"name": detail["name"], "expected_revision": detail["revision"], "source": source},
526
+ )
527
+ self.association.update(
528
+ evidence_digest=digest(examples),
529
+ evidence_file=str(Path(path).resolve()),
530
+ revision=result["revision"],
531
+ )
532
+ return {"function_id": function, "revision": result["revision"], "rows": len(examples)}, 0