aid-runtime-python 0.1.87__py3-none-any.whl → 0.1.89__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.
- aid_runtime/__init__.py +23 -1
- aid_runtime/decisions.py +374 -0
- {aid_runtime_python-0.1.87.dist-info → aid_runtime_python-0.1.89.dist-info}/METADATA +33 -2
- {aid_runtime_python-0.1.87.dist-info → aid_runtime_python-0.1.89.dist-info}/RECORD +6 -5
- {aid_runtime_python-0.1.87.dist-info → aid_runtime_python-0.1.89.dist-info}/WHEEL +0 -0
- {aid_runtime_python-0.1.87.dist-info → aid_runtime_python-0.1.89.dist-info}/top_level.txt +0 -0
aid_runtime/__init__.py
CHANGED
|
@@ -69,6 +69,18 @@ from aid_runtime.cost import (
|
|
|
69
69
|
estimate_cost_usd,
|
|
70
70
|
estimate_prompt_cost_usd,
|
|
71
71
|
)
|
|
72
|
+
from aid_runtime.decisions import (
|
|
73
|
+
DEFAULT_TYPESAFE_BASE_URL,
|
|
74
|
+
JEV_MOVING_ALIASES,
|
|
75
|
+
TYPESAFE_API_KEY_ENVS,
|
|
76
|
+
TYPESAFE_BASE_URL_ENV,
|
|
77
|
+
DecisionAnswer,
|
|
78
|
+
DecisionClient,
|
|
79
|
+
DecisionQuestion,
|
|
80
|
+
DecisionResponse,
|
|
81
|
+
decision_key,
|
|
82
|
+
is_reproducible_model,
|
|
83
|
+
)
|
|
72
84
|
from aid_runtime.errors import (
|
|
73
85
|
AgentBudgetDegradedError,
|
|
74
86
|
AgentHandoffUnsupportedError,
|
|
@@ -282,7 +294,7 @@ from aid_runtime.types import (
|
|
|
282
294
|
)
|
|
283
295
|
|
|
284
296
|
#: Kept in sync with ``pyproject.toml`` by a test, so the two cannot drift.
|
|
285
|
-
__version__ = "0.1.
|
|
297
|
+
__version__ = "0.1.89"
|
|
286
298
|
|
|
287
299
|
__all__ = [
|
|
288
300
|
"ANTHROPIC_VERSION",
|
|
@@ -291,12 +303,14 @@ __all__ = [
|
|
|
291
303
|
"DEFAULT_ANTHROPIC_BASE_URL",
|
|
292
304
|
"DEFAULT_BASE_URL",
|
|
293
305
|
"DEFAULT_CANDIDATE_FACTOR",
|
|
306
|
+
"DEFAULT_TYPESAFE_BASE_URL",
|
|
294
307
|
"DEFAULT_VERTEX_LOCATION",
|
|
295
308
|
"DEPTH_KEY",
|
|
296
309
|
"EMITTED_KEY",
|
|
297
310
|
"GEN_AI_OPERATION_CHAT",
|
|
298
311
|
"INDEX_KEY",
|
|
299
312
|
"ITEM_KEY",
|
|
313
|
+
"JEV_MOVING_ALIASES",
|
|
300
314
|
"LIMITER_KEY",
|
|
301
315
|
"MAX_STEP_DEPTH",
|
|
302
316
|
"METADATA_TOKEN_URL",
|
|
@@ -305,6 +319,8 @@ __all__ = [
|
|
|
305
319
|
"RESERVED_KEYS",
|
|
306
320
|
"STEPS_KEY",
|
|
307
321
|
"TRACE_DIR_ENV",
|
|
322
|
+
"TYPESAFE_API_KEY_ENVS",
|
|
323
|
+
"TYPESAFE_BASE_URL_ENV",
|
|
308
324
|
"VERTEX_TOKEN_ENV",
|
|
309
325
|
"AgentBudgetDegradedError",
|
|
310
326
|
"AgentCheckpoint",
|
|
@@ -338,6 +354,10 @@ __all__ = [
|
|
|
338
354
|
"CostProfile",
|
|
339
355
|
"Dataset",
|
|
340
356
|
"DatasetRun",
|
|
357
|
+
"DecisionAnswer",
|
|
358
|
+
"DecisionClient",
|
|
359
|
+
"DecisionQuestion",
|
|
360
|
+
"DecisionResponse",
|
|
341
361
|
"DeepAgentsEngineError",
|
|
342
362
|
"DeepAgentsMaxStepsError",
|
|
343
363
|
"DeepAgentsUnavailableError",
|
|
@@ -478,6 +498,7 @@ __all__ = [
|
|
|
478
498
|
"chunker_for",
|
|
479
499
|
"cite",
|
|
480
500
|
"create_store",
|
|
501
|
+
"decision_key",
|
|
481
502
|
"default_bedrock_base_url",
|
|
482
503
|
"default_metrics",
|
|
483
504
|
"default_vertex_base_url",
|
|
@@ -494,6 +515,7 @@ __all__ = [
|
|
|
494
515
|
"gateway_judge",
|
|
495
516
|
"generate_step",
|
|
496
517
|
"get_tracer",
|
|
518
|
+
"is_reproducible_model",
|
|
497
519
|
"lexical_ranking",
|
|
498
520
|
"lexical_score",
|
|
499
521
|
"llm_span",
|
aid_runtime/decisions.py
ADDED
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
"""The TypeSafe AI (Jev) decision client.
|
|
2
|
+
|
|
3
|
+
Jev is a *System One* model, not a chat model: it takes a state and typed
|
|
4
|
+
questions and returns typed answers. It has no ``/v1/chat/completions``
|
|
5
|
+
surface, no free-text output and no tool calls, so it is deliberately not a
|
|
6
|
+
member of :mod:`aid_runtime.providers`. Putting it there would let a pipeline
|
|
7
|
+
select it as the model that writes an answer, which it cannot do.
|
|
8
|
+
|
|
9
|
+
The four primitives map to the places a pipeline already needs a judgment:
|
|
10
|
+
|
|
11
|
+
``choice``
|
|
12
|
+
one label out of a list -- routing, classification.
|
|
13
|
+
``score``
|
|
14
|
+
a position on an ordered rubric -- eval scoring.
|
|
15
|
+
``noul``
|
|
16
|
+
a probability in ``[0, 1]`` -- a cheap pre-filter.
|
|
17
|
+
|
|
18
|
+
Two facts drive the shape of this module:
|
|
19
|
+
|
|
20
|
+
**``noul`` answers carry no ``confidence`` at all.** ``choice`` and ``score``
|
|
21
|
+
answers do. A caller that branches on confidence must therefore refuse a
|
|
22
|
+
``noul`` decision rather than defaulting the missing value to zero, which
|
|
23
|
+
would silently disable the branch.
|
|
24
|
+
|
|
25
|
+
**A moving model alias cannot be used where a result must be reproducible.**
|
|
26
|
+
``jev-latest`` is legal for interactive routing and refused when
|
|
27
|
+
``reproducible=True``, which is what the eval harness passes.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
from __future__ import annotations
|
|
31
|
+
|
|
32
|
+
import asyncio
|
|
33
|
+
import os
|
|
34
|
+
from collections.abc import Mapping, Sequence
|
|
35
|
+
from dataclasses import dataclass
|
|
36
|
+
from typing import Any, Literal
|
|
37
|
+
|
|
38
|
+
import httpx
|
|
39
|
+
|
|
40
|
+
from aid_runtime.errors import ConfigurationError, ProviderError, ProviderResponseError
|
|
41
|
+
|
|
42
|
+
__all__ = [
|
|
43
|
+
"DEFAULT_TYPESAFE_BASE_URL",
|
|
44
|
+
"JEV_MOVING_ALIASES",
|
|
45
|
+
"TYPESAFE_API_KEY_ENVS",
|
|
46
|
+
"TYPESAFE_BASE_URL_ENV",
|
|
47
|
+
"DecisionAnswer",
|
|
48
|
+
"DecisionClient",
|
|
49
|
+
"DecisionQuestion",
|
|
50
|
+
"DecisionResponse",
|
|
51
|
+
"decision_key",
|
|
52
|
+
"is_reproducible_model",
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
DEFAULT_TYPESAFE_BASE_URL = "https://api.typesafe.ai"
|
|
56
|
+
|
|
57
|
+
#: The first-party SDK reads ``TYPESAFE_API_KEY``; Vercel's provider reads
|
|
58
|
+
#: ``TYPESAFE_AI_API_KEY``. Accept both, first-party name first.
|
|
59
|
+
TYPESAFE_API_KEY_ENVS = ("TYPESAFE_API_KEY", "TYPESAFE_AI_API_KEY")
|
|
60
|
+
|
|
61
|
+
TYPESAFE_BASE_URL_ENV = "TYPESAFE_BASE_URL"
|
|
62
|
+
|
|
63
|
+
#: Aliases that move to a new model version. Legal interactively, refused under
|
|
64
|
+
#: ``reproducible=True``.
|
|
65
|
+
JEV_MOVING_ALIASES = frozenset({"jev-latest", "jev-preview"})
|
|
66
|
+
|
|
67
|
+
QuestionType = Literal["choice", "score", "noul"]
|
|
68
|
+
|
|
69
|
+
_QUESTION_TYPES: frozenset[str] = frozenset({"choice", "score", "noul"})
|
|
70
|
+
|
|
71
|
+
#: 429 and 529 are the retryable classes the vendor names; both may carry
|
|
72
|
+
#: ``retry-after``. The rest are caller errors and are not retried.
|
|
73
|
+
_RETRYABLE_STATUS = frozenset({429, 529})
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def decision_key(env: Mapping[str, str] | None = None) -> str | None:
|
|
77
|
+
"""Return the first configured TypeSafe key, or ``None``.
|
|
78
|
+
|
|
79
|
+
Both the first-party name and Vercel's spelling are accepted. An empty
|
|
80
|
+
string counts as unset, so a workflow that exports a missing secret does
|
|
81
|
+
not authenticate with ``""``.
|
|
82
|
+
"""
|
|
83
|
+
source = os.environ if env is None else env
|
|
84
|
+
for name in TYPESAFE_API_KEY_ENVS:
|
|
85
|
+
value = source.get(name)
|
|
86
|
+
if value is not None and value != "":
|
|
87
|
+
return value
|
|
88
|
+
return None
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def is_reproducible_model(model: str) -> bool:
|
|
92
|
+
"""Whether ``model`` is pinned to one version rather than a moving alias."""
|
|
93
|
+
return model not in JEV_MOVING_ALIASES
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@dataclass(frozen=True, slots=True)
|
|
97
|
+
class DecisionQuestion:
|
|
98
|
+
"""One typed question.
|
|
99
|
+
|
|
100
|
+
``criteria`` is required by ``choice`` and ``score`` and forbidden by
|
|
101
|
+
``noul``, because a probability has nothing to choose between. ``score``
|
|
102
|
+
criteria are ordered: the position is the score.
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
type: QuestionType
|
|
106
|
+
instructions: str
|
|
107
|
+
criteria: tuple[str, ...] | None = None
|
|
108
|
+
|
|
109
|
+
def __post_init__(self) -> None:
|
|
110
|
+
if self.type not in _QUESTION_TYPES:
|
|
111
|
+
raise ConfigurationError(
|
|
112
|
+
f"unknown decision question type {self.type!r}; expected one of "
|
|
113
|
+
f"{', '.join(sorted(_QUESTION_TYPES))}"
|
|
114
|
+
)
|
|
115
|
+
if self.instructions.strip() == "":
|
|
116
|
+
raise ConfigurationError("a decision question needs instructions")
|
|
117
|
+
if self.type == "noul":
|
|
118
|
+
if self.criteria is not None:
|
|
119
|
+
raise ConfigurationError("a noul question takes no criteria")
|
|
120
|
+
return
|
|
121
|
+
if not self.criteria:
|
|
122
|
+
raise ConfigurationError(f"a {self.type} question needs at least one criterion")
|
|
123
|
+
if self.type == "score" and not 2 <= len(self.criteria) <= 10:
|
|
124
|
+
raise ConfigurationError(
|
|
125
|
+
"a score question needs between 2 and 10 ordered criteria; "
|
|
126
|
+
f"got {len(self.criteria)}"
|
|
127
|
+
)
|
|
128
|
+
if self.type == "choice" and len(self.criteria) > 255:
|
|
129
|
+
raise ConfigurationError("a choice question takes at most 255 criteria")
|
|
130
|
+
|
|
131
|
+
def as_payload(self) -> dict[str, Any]:
|
|
132
|
+
payload: dict[str, Any] = {"type": self.type, "instructions": self.instructions}
|
|
133
|
+
if self.criteria is not None:
|
|
134
|
+
payload["criteria"] = list(self.criteria)
|
|
135
|
+
return payload
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
@dataclass(frozen=True, slots=True)
|
|
139
|
+
class DecisionAnswer:
|
|
140
|
+
"""One typed answer, with every field the question type can produce."""
|
|
141
|
+
|
|
142
|
+
type: QuestionType
|
|
143
|
+
choice: str | None = None
|
|
144
|
+
score: float | None = None
|
|
145
|
+
noul: float | None = None
|
|
146
|
+
confidence: float | None = None
|
|
147
|
+
probabilities: Mapping[str, float] | None = None
|
|
148
|
+
legend: str | None = None
|
|
149
|
+
raw: Mapping[str, Any] | None = None
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
@dataclass(frozen=True, slots=True)
|
|
153
|
+
class DecisionResponse:
|
|
154
|
+
"""``model`` is echoed by the vendor, so a trace records the version that
|
|
155
|
+
actually answered -- the only way to explain a score that moved."""
|
|
156
|
+
|
|
157
|
+
model: str
|
|
158
|
+
answers: Mapping[str, DecisionAnswer]
|
|
159
|
+
input_tokens: int | None = None
|
|
160
|
+
output_tokens: int | None = None
|
|
161
|
+
raw: Mapping[str, Any] | None = None
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class DecisionClient:
|
|
165
|
+
"""Calls ``POST /v1/systemone`` and maps the reply to typed answers."""
|
|
166
|
+
|
|
167
|
+
def __init__(
|
|
168
|
+
self,
|
|
169
|
+
*,
|
|
170
|
+
api_key: str | None = None,
|
|
171
|
+
base_url: str | None = None,
|
|
172
|
+
name: str = "typesafe",
|
|
173
|
+
timeout_seconds: float = 60.0,
|
|
174
|
+
max_attempts: int = 3,
|
|
175
|
+
backoff_seconds: float = 0.5,
|
|
176
|
+
client: httpx.AsyncClient | None = None,
|
|
177
|
+
) -> None:
|
|
178
|
+
resolved_key = api_key if api_key is not None else decision_key()
|
|
179
|
+
if resolved_key is None or resolved_key == "":
|
|
180
|
+
raise ConfigurationError(
|
|
181
|
+
"TypeSafe AI needs an API key; set TYPESAFE_API_KEY (or TYPESAFE_AI_API_KEY)"
|
|
182
|
+
)
|
|
183
|
+
resolved_base = (
|
|
184
|
+
base_url or os.environ.get(TYPESAFE_BASE_URL_ENV) or DEFAULT_TYPESAFE_BASE_URL
|
|
185
|
+
)
|
|
186
|
+
if not resolved_base.startswith(("http://", "https://")):
|
|
187
|
+
raise ConfigurationError(
|
|
188
|
+
f"base_url must be an absolute http(s) URL, got {resolved_base!r}"
|
|
189
|
+
)
|
|
190
|
+
if max_attempts < 1:
|
|
191
|
+
raise ConfigurationError("max_attempts must be at least 1")
|
|
192
|
+
self._name = name
|
|
193
|
+
self._base_url = resolved_base.rstrip("/")
|
|
194
|
+
self._api_key = resolved_key
|
|
195
|
+
self._timeout = timeout_seconds
|
|
196
|
+
self._max_attempts = max_attempts
|
|
197
|
+
self._backoff = backoff_seconds
|
|
198
|
+
self._owns_client = client is None
|
|
199
|
+
self._client = client or httpx.AsyncClient(timeout=timeout_seconds)
|
|
200
|
+
self._closed = False
|
|
201
|
+
|
|
202
|
+
@property
|
|
203
|
+
def name(self) -> str:
|
|
204
|
+
return self._name
|
|
205
|
+
|
|
206
|
+
async def decide(
|
|
207
|
+
self,
|
|
208
|
+
*,
|
|
209
|
+
model: str,
|
|
210
|
+
state: str | Mapping[str, Any] | Sequence[Any],
|
|
211
|
+
questions: Mapping[str, DecisionQuestion],
|
|
212
|
+
reproducible: bool = False,
|
|
213
|
+
) -> DecisionResponse:
|
|
214
|
+
"""Ask ``questions`` about ``state``.
|
|
215
|
+
|
|
216
|
+
``reproducible=True`` refuses a moving alias, because two runs of an
|
|
217
|
+
eval would otherwise score two different models under one name.
|
|
218
|
+
"""
|
|
219
|
+
if not questions:
|
|
220
|
+
raise ConfigurationError("a decision needs at least one question")
|
|
221
|
+
if reproducible and not is_reproducible_model(model):
|
|
222
|
+
raise ConfigurationError(
|
|
223
|
+
f"{model!r} is a moving alias and cannot be used where the result must be "
|
|
224
|
+
"reproducible; pin a version such as 'jev-1.14.0'"
|
|
225
|
+
)
|
|
226
|
+
payload: dict[str, Any] = {
|
|
227
|
+
"model": model,
|
|
228
|
+
"state": state,
|
|
229
|
+
"questions": {key: question.as_payload() for key, question in questions.items()},
|
|
230
|
+
}
|
|
231
|
+
body = await self._post(payload)
|
|
232
|
+
return self._parse(body, questions)
|
|
233
|
+
|
|
234
|
+
async def close(self) -> None:
|
|
235
|
+
if self._closed:
|
|
236
|
+
return
|
|
237
|
+
self._closed = True
|
|
238
|
+
if self._owns_client:
|
|
239
|
+
await self._client.aclose()
|
|
240
|
+
|
|
241
|
+
async def __aenter__(self) -> DecisionClient:
|
|
242
|
+
return self
|
|
243
|
+
|
|
244
|
+
async def __aexit__(self, *_exc: object) -> None:
|
|
245
|
+
await self.close()
|
|
246
|
+
|
|
247
|
+
def _headers(self) -> dict[str, str]:
|
|
248
|
+
return {
|
|
249
|
+
"authorization": f"Bearer {self._api_key}",
|
|
250
|
+
"content-type": "application/json",
|
|
251
|
+
"accept": "application/json",
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async def _post(self, payload: Mapping[str, Any]) -> Mapping[str, Any]:
|
|
255
|
+
url = f"{self._base_url}/v1/systemone"
|
|
256
|
+
attempt = 0
|
|
257
|
+
while True:
|
|
258
|
+
attempt += 1
|
|
259
|
+
try:
|
|
260
|
+
response = await self._client.post(url, json=payload, headers=self._headers())
|
|
261
|
+
except httpx.HTTPError as error:
|
|
262
|
+
if attempt >= self._max_attempts:
|
|
263
|
+
raise ProviderError(f"{self._name} request failed: {error}") from error
|
|
264
|
+
await asyncio.sleep(self._backoff * attempt)
|
|
265
|
+
continue
|
|
266
|
+
|
|
267
|
+
if response.status_code in _RETRYABLE_STATUS and attempt < self._max_attempts:
|
|
268
|
+
await asyncio.sleep(self._retry_delay(response, attempt))
|
|
269
|
+
continue
|
|
270
|
+
if response.status_code >= 400:
|
|
271
|
+
raise ProviderError(
|
|
272
|
+
f"{self._name} returned {response.status_code}: {response.text[:500]}"
|
|
273
|
+
)
|
|
274
|
+
try:
|
|
275
|
+
parsed = response.json()
|
|
276
|
+
except ValueError as error:
|
|
277
|
+
raise ProviderResponseError(f"{self._name} returned non-JSON: {error}") from error
|
|
278
|
+
if not isinstance(parsed, dict):
|
|
279
|
+
raise ProviderResponseError(f"{self._name} returned a non-object body")
|
|
280
|
+
return parsed
|
|
281
|
+
|
|
282
|
+
def _retry_delay(self, response: httpx.Response, attempt: int) -> float:
|
|
283
|
+
raw = response.headers.get("retry-after")
|
|
284
|
+
if raw is not None:
|
|
285
|
+
try:
|
|
286
|
+
return max(0.0, float(raw))
|
|
287
|
+
except ValueError:
|
|
288
|
+
pass
|
|
289
|
+
return self._backoff * attempt
|
|
290
|
+
|
|
291
|
+
def _parse(
|
|
292
|
+
self, body: Mapping[str, Any], questions: Mapping[str, DecisionQuestion]
|
|
293
|
+
) -> DecisionResponse:
|
|
294
|
+
model = body.get("model")
|
|
295
|
+
answers = body.get("answers")
|
|
296
|
+
if not isinstance(model, str) or not isinstance(answers, dict):
|
|
297
|
+
raise ProviderResponseError(f"{self._name} response is missing 'model' or 'answers'")
|
|
298
|
+
parsed: dict[str, DecisionAnswer] = {}
|
|
299
|
+
for key, question in questions.items():
|
|
300
|
+
raw = answers.get(key)
|
|
301
|
+
if not isinstance(raw, dict):
|
|
302
|
+
raise ProviderResponseError(
|
|
303
|
+
f"{self._name} response is missing an answer for question {key!r}"
|
|
304
|
+
)
|
|
305
|
+
parsed[key] = _answer(question.type, raw)
|
|
306
|
+
usage = body.get("usage")
|
|
307
|
+
return DecisionResponse(
|
|
308
|
+
model=model,
|
|
309
|
+
answers=parsed,
|
|
310
|
+
input_tokens=_int_or_none(_field(usage, "input_tokens")),
|
|
311
|
+
output_tokens=_int_or_none(_field(usage, "output_tokens")),
|
|
312
|
+
raw=body,
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def _field(value: Any, key: str) -> Any:
|
|
317
|
+
return value.get(key) if isinstance(value, Mapping) else None
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def _int_or_none(value: Any) -> int | None:
|
|
321
|
+
return value if isinstance(value, int) and not isinstance(value, bool) else None
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def _float_or_none(value: Any) -> float | None:
|
|
325
|
+
return float(value) if isinstance(value, (int, float)) and not isinstance(value, bool) else None
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def _answer(kind: QuestionType, raw: Mapping[str, Any]) -> DecisionAnswer:
|
|
329
|
+
"""Map one answer, refusing a shape the question type cannot produce.
|
|
330
|
+
|
|
331
|
+
The confidence rule is the important one: a ``noul`` answer has no
|
|
332
|
+
``confidence`` field, so inventing one here would make a guardrail that
|
|
333
|
+
branches on confidence look satisfiable when it never is.
|
|
334
|
+
"""
|
|
335
|
+
if kind == "noul":
|
|
336
|
+
noul = _float_or_none(raw.get("noul"))
|
|
337
|
+
if noul is None:
|
|
338
|
+
raise ProviderResponseError("a noul answer must carry 'noul'")
|
|
339
|
+
if "confidence" in raw:
|
|
340
|
+
raise ProviderResponseError("a noul answer must not carry 'confidence'")
|
|
341
|
+
return DecisionAnswer(type=kind, noul=noul, raw=raw)
|
|
342
|
+
|
|
343
|
+
confidence = _float_or_none(raw.get("confidence"))
|
|
344
|
+
if confidence is None:
|
|
345
|
+
raise ProviderResponseError(f"a {kind} answer must carry 'confidence'")
|
|
346
|
+
probabilities = raw.get("probabilities")
|
|
347
|
+
mapped = (
|
|
348
|
+
{str(key): float(value) for key, value in probabilities.items()}
|
|
349
|
+
if isinstance(probabilities, Mapping)
|
|
350
|
+
else None
|
|
351
|
+
)
|
|
352
|
+
if kind == "choice":
|
|
353
|
+
choice = raw.get("choice")
|
|
354
|
+
if not isinstance(choice, str):
|
|
355
|
+
raise ProviderResponseError("a choice answer must carry 'choice'")
|
|
356
|
+
return DecisionAnswer(
|
|
357
|
+
type=kind,
|
|
358
|
+
choice=choice,
|
|
359
|
+
confidence=confidence,
|
|
360
|
+
probabilities=mapped,
|
|
361
|
+
raw=raw,
|
|
362
|
+
)
|
|
363
|
+
score = _float_or_none(raw.get("score"))
|
|
364
|
+
if score is None:
|
|
365
|
+
raise ProviderResponseError("a score answer must carry 'score'")
|
|
366
|
+
legend = raw.get("legend")
|
|
367
|
+
return DecisionAnswer(
|
|
368
|
+
type=kind,
|
|
369
|
+
score=score,
|
|
370
|
+
confidence=confidence,
|
|
371
|
+
probabilities=mapped,
|
|
372
|
+
legend=legend if isinstance(legend, str) else None,
|
|
373
|
+
raw=raw,
|
|
374
|
+
)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: aid-runtime-python
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.89
|
|
4
4
|
Summary: The AID Framework Python runtime: model gateway with pinning and cost budgets, prompt registry, tool executor, OTel GenAI tracing, plain-async pipeline, and an eval harness that gates on statistical regression against a stored baseline.
|
|
5
5
|
Author: The AID Framework authors
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -96,6 +96,7 @@ result.text, result.usage, result.cost_usd, result.alias, result.attempts, resul
|
|
|
96
96
|
| --- | --- |
|
|
97
97
|
| `gateway.py` | `ModelGateway` — alias resolution, capability negotiation, ordered fallback, cost accounting, GenAI spans |
|
|
98
98
|
| `providers/` | The `Provider` protocol, one HTTP adapter (`OpenAICompatibleProvider`), one deterministic fake (`FakeProvider`) |
|
|
99
|
+
| `decisions.py` | `DecisionClient` — TypeSafe AI (Jev) typed decisions: `choice`, `score`, and `noul` answers |
|
|
99
100
|
| `prompts.py` | `PromptTemplate` / `PromptRegistry` — versioned prompts, typed rendering, few-shot assembly |
|
|
100
101
|
| `structured.py` | `build_agent` — structured output via Pydantic AI only |
|
|
101
102
|
| `tools.py` | `Tool` / `ToolExecutor` — declared definitions, argument validation, structured results |
|
|
@@ -107,7 +108,7 @@ result.text, result.usage, result.cost_usd, result.alias, result.attempts, resul
|
|
|
107
108
|
| `cost.py` | `Budget`, `CostLedger`, `estimate_cost_usd`, `estimate_tokens` |
|
|
108
109
|
| `types.py` | The request/response/usage/spec models everything else is typed against |
|
|
109
110
|
|
|
110
|
-
|
|
111
|
+
249 names are exported. The layering is acyclic and one-directional: `errors` and `types` import nothing else in
|
|
111
112
|
the package, `gateway` is imported only by the package `__init__` and the eval runner, and no core module imports
|
|
112
113
|
a higher layer back.
|
|
113
114
|
|
|
@@ -128,6 +129,36 @@ alias is an error** (`UnknownModelAliasError`) rather than a silent fall-through
|
|
|
128
129
|
`estimate_usage` / `estimate_cost_usd` price a request from a model's `CostProfile` when a caller wants to check
|
|
129
130
|
before spending.
|
|
130
131
|
|
|
132
|
+
## Decisions are not chat
|
|
133
|
+
|
|
134
|
+
`DecisionClient` calls TypeSafe AI's Jev (`POST /v1/systemone`), which takes a state plus typed questions and
|
|
135
|
+
returns typed answers instead of prose. It is deliberately not in `providers/`: every provider there maps messages
|
|
136
|
+
to text, and Jev has no text surface, so a pipeline cannot select it as the model that writes an answer.
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
from aid_runtime import DecisionClient, DecisionQuestion
|
|
140
|
+
|
|
141
|
+
async with DecisionClient() as client:
|
|
142
|
+
response = await client.decide(
|
|
143
|
+
model="jev-1.14.0",
|
|
144
|
+
state={"message": message},
|
|
145
|
+
questions={
|
|
146
|
+
"triage": DecisionQuestion(
|
|
147
|
+
type="choice",
|
|
148
|
+
instructions="Which team should handle this",
|
|
149
|
+
criteria=("billing", "technical"),
|
|
150
|
+
)
|
|
151
|
+
},
|
|
152
|
+
)
|
|
153
|
+
response.answers["triage"].choice
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
The key is `TYPESAFE_API_KEY` (Vercel's spelling, `TYPESAFE_AI_API_KEY`, is accepted too); `TYPESAFE_BASE_URL`
|
|
157
|
+
overrides the host. Two rules matter more than they look: a `noul` answer carries `noul` and **no confidence
|
|
158
|
+
field at all**, so a branch on confidence must refuse it rather than default to zero; and
|
|
159
|
+
`decide(..., reproducible=True)` refuses the moving aliases `jev-latest` and `jev-preview`, which is what eval
|
|
160
|
+
scoring needs. Only 429/529 are retried, honouring `retry-after`.
|
|
161
|
+
|
|
131
162
|
## The eval gate measures statistics, not equality
|
|
132
163
|
|
|
133
164
|
A model is **expected** to vary between runs, so a metric that compares two runs for equality is either always
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
aid_runtime/__init__.py,sha256=
|
|
1
|
+
aid_runtime/__init__.py,sha256=Tzosw1nOeH95sTHYpIOqch3teztOQUutYCGnR5V-ohQ,12397
|
|
2
2
|
aid_runtime/cache.py,sha256=jhw_EBd9dfWEUzYeibOvIkVBqqiLUjqF5WKEPz9GkhE,6836
|
|
3
3
|
aid_runtime/concurrency.py,sha256=z0tFQHw35ttFLKfazGBjCMPOLQOnUotE_OPpvR1piH4,2829
|
|
4
4
|
aid_runtime/control.py,sha256=u4U97QFdYSSjOPY3D6f1-GZhkGhygEdR-apdgw_jJok,13797
|
|
5
5
|
aid_runtime/cost.py,sha256=Hc7X6mtkeanL9eJ1lEZZCQ9LxZurhL1it0gm3s9LpQE,5465
|
|
6
|
+
aid_runtime/decisions.py,sha256=6rkpFODb0PAQ9cqtKuz5ZRA19C3Ljwz1aGWKcovVSc4,13859
|
|
6
7
|
aid_runtime/errors.py,sha256=TKkZyZZcFTl5yxNKlA8jUF4ORpYHLGFEvc6r9FK4X70,12572
|
|
7
8
|
aid_runtime/gateway.py,sha256=p0qsaQUAYrwSzlWwpUi1wY6pdOeU98QjsWLzq8045Fo,18267
|
|
8
9
|
aid_runtime/local_trace.py,sha256=eg5LDsg1VfNmJNqs8KjtdzTjSqg7SWTXunLwg-b3IcM,7870
|
|
@@ -50,7 +51,7 @@ aid_runtime/retrieval/rerank.py,sha256=L4ICBi9YHDzhnKVtImKh5Wzre1HDzS8at-AKDkXBJ
|
|
|
50
51
|
aid_runtime/retrieval/retriever.py,sha256=hVLP0xOLKioYsa-aS1akyJl5p-WbpVjm8OfCw26m4CE,13941
|
|
51
52
|
aid_runtime/retrieval/stores.py,sha256=VEi68AaMHcQjdAbWOHflp3jCl1_1db71rIzZjTouLlo,5857
|
|
52
53
|
aid_runtime/retrieval/types.py,sha256=8vKP21rmE9LglnB5gtv0dLBIkXzaPqdG3-GS98K0gpc,13498
|
|
53
|
-
aid_runtime_python-0.1.
|
|
54
|
-
aid_runtime_python-0.1.
|
|
55
|
-
aid_runtime_python-0.1.
|
|
56
|
-
aid_runtime_python-0.1.
|
|
54
|
+
aid_runtime_python-0.1.89.dist-info/METADATA,sha256=XtGshSd4yt8JRa08CXHHWlHaOGfDjWr00uCfX1DrwLI,17005
|
|
55
|
+
aid_runtime_python-0.1.89.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
56
|
+
aid_runtime_python-0.1.89.dist-info/top_level.txt,sha256=QvODgNVRDDMmLcTnoIQ4jYnhqf-N4b9sHWEmaUUxp44,12
|
|
57
|
+
aid_runtime_python-0.1.89.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|