aid-runtime-python 0.1.94__py3-none-any.whl → 0.1.96__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 +7 -1
- aid_runtime/control.py +90 -2
- aid_runtime/decisions.py +38 -2
- aid_runtime/tracing.py +26 -6
- {aid_runtime_python-0.1.94.dist-info → aid_runtime_python-0.1.96.dist-info}/METADATA +1 -1
- {aid_runtime_python-0.1.94.dist-info → aid_runtime_python-0.1.96.dist-info}/RECORD +8 -8
- {aid_runtime_python-0.1.94.dist-info → aid_runtime_python-0.1.96.dist-info}/WHEEL +0 -0
- {aid_runtime_python-0.1.94.dist-info → aid_runtime_python-0.1.96.dist-info}/top_level.txt +0 -0
aid_runtime/__init__.py
CHANGED
|
@@ -60,6 +60,7 @@ from aid_runtime.control import (
|
|
|
60
60
|
map_step,
|
|
61
61
|
parallel,
|
|
62
62
|
reduce_step,
|
|
63
|
+
route,
|
|
63
64
|
)
|
|
64
65
|
from aid_runtime.cost import (
|
|
65
66
|
Budget,
|
|
@@ -71,6 +72,7 @@ from aid_runtime.cost import (
|
|
|
71
72
|
)
|
|
72
73
|
from aid_runtime.decisions import (
|
|
73
74
|
DEFAULT_TYPESAFE_BASE_URL,
|
|
75
|
+
JEV_INPUT_USD_PER_MTOK,
|
|
74
76
|
JEV_MOVING_ALIASES,
|
|
75
77
|
TYPESAFE_API_KEY_ENVS,
|
|
76
78
|
TYPESAFE_BASE_URL_ENV,
|
|
@@ -78,6 +80,7 @@ from aid_runtime.decisions import (
|
|
|
78
80
|
DecisionClient,
|
|
79
81
|
DecisionQuestion,
|
|
80
82
|
DecisionResponse,
|
|
83
|
+
decision_cost_usd,
|
|
81
84
|
decision_key,
|
|
82
85
|
is_reproducible_model,
|
|
83
86
|
)
|
|
@@ -294,7 +297,7 @@ from aid_runtime.types import (
|
|
|
294
297
|
)
|
|
295
298
|
|
|
296
299
|
#: Kept in sync with ``pyproject.toml`` by a test, so the two cannot drift.
|
|
297
|
-
__version__ = "0.1.
|
|
300
|
+
__version__ = "0.1.96"
|
|
298
301
|
|
|
299
302
|
__all__ = [
|
|
300
303
|
"ANTHROPIC_VERSION",
|
|
@@ -310,6 +313,7 @@ __all__ = [
|
|
|
310
313
|
"GEN_AI_OPERATION_CHAT",
|
|
311
314
|
"INDEX_KEY",
|
|
312
315
|
"ITEM_KEY",
|
|
316
|
+
"JEV_INPUT_USD_PER_MTOK",
|
|
313
317
|
"JEV_MOVING_ALIASES",
|
|
314
318
|
"LIMITER_KEY",
|
|
315
319
|
"MAX_STEP_DEPTH",
|
|
@@ -498,6 +502,7 @@ __all__ = [
|
|
|
498
502
|
"chunker_for",
|
|
499
503
|
"cite",
|
|
500
504
|
"create_store",
|
|
505
|
+
"decision_cost_usd",
|
|
501
506
|
"decision_key",
|
|
502
507
|
"default_bedrock_base_url",
|
|
503
508
|
"default_metrics",
|
|
@@ -539,6 +544,7 @@ __all__ = [
|
|
|
539
544
|
"resolve_uri",
|
|
540
545
|
"retrieval_metrics",
|
|
541
546
|
"retrieve_step",
|
|
547
|
+
"route",
|
|
542
548
|
"rrf_score",
|
|
543
549
|
"run_evaluation",
|
|
544
550
|
"run_structured",
|
aid_runtime/control.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""Composite pipeline steps: ``branch``, ``parallel``, ``map`` and ``reduce``.
|
|
1
|
+
"""Composite pipeline steps: ``branch``, ``route``, ``parallel``, ``map`` and ``reduce``.
|
|
2
2
|
|
|
3
3
|
:class:`~aid_runtime.pipeline.Pipeline` is a sequential reducer, and it stays one.
|
|
4
4
|
Each factory here returns an ordinary :data:`~aid_runtime.pipeline.Step`, so a
|
|
@@ -27,13 +27,18 @@ from __future__ import annotations
|
|
|
27
27
|
|
|
28
28
|
import asyncio
|
|
29
29
|
from collections.abc import Awaitable, Callable, Mapping, Sequence
|
|
30
|
-
from typing import Any, TypeVar
|
|
30
|
+
from typing import TYPE_CHECKING, Any, TypeVar
|
|
31
31
|
|
|
32
32
|
from aid_runtime.agents.policy import Principal, evaluate_policy, policy_source
|
|
33
33
|
from aid_runtime.concurrency import ConcurrencyLimiter, holds_slot
|
|
34
|
+
from aid_runtime.decisions import DecisionClient, DecisionQuestion
|
|
34
35
|
from aid_runtime.errors import PipelineError
|
|
35
36
|
from aid_runtime.pipeline import STEPS_KEY, Pipeline, PipelineContext
|
|
36
37
|
|
|
38
|
+
if TYPE_CHECKING:
|
|
39
|
+
from aid_runtime.agents.guardrails import DecisionAsker
|
|
40
|
+
from aid_runtime.agents.ir import DecisionDefinition
|
|
41
|
+
|
|
37
42
|
__all__ = [
|
|
38
43
|
"DEPTH_KEY",
|
|
39
44
|
"INDEX_KEY",
|
|
@@ -47,6 +52,7 @@ __all__ = [
|
|
|
47
52
|
"map_step",
|
|
48
53
|
"parallel",
|
|
49
54
|
"reduce_step",
|
|
55
|
+
"route",
|
|
50
56
|
]
|
|
51
57
|
|
|
52
58
|
#: Deepest composite nesting a pipeline may declare.
|
|
@@ -208,6 +214,88 @@ def branch(
|
|
|
208
214
|
return _named(_branch, name)
|
|
209
215
|
|
|
210
216
|
|
|
217
|
+
def route(
|
|
218
|
+
*,
|
|
219
|
+
decision: str,
|
|
220
|
+
state: str,
|
|
221
|
+
on: Mapping[str, Pipeline],
|
|
222
|
+
otherwise: Pipeline | None = None,
|
|
223
|
+
as_: str | None = None,
|
|
224
|
+
decisions: Mapping[str, DecisionDefinition] | None = None,
|
|
225
|
+
client_factory: Callable[[], DecisionAsker] | None = None,
|
|
226
|
+
max_depth: int = MAX_STEP_DEPTH,
|
|
227
|
+
name: str | None = None,
|
|
228
|
+
) -> CompositeStep:
|
|
229
|
+
"""Ask one ``choice`` decision which arm to run, then run that arm.
|
|
230
|
+
|
|
231
|
+
This is the one composite that asks a model a question, and it is the difference
|
|
232
|
+
between a ``branch`` and a ``route``: a branch's predicate is a policy over the
|
|
233
|
+
caller, so it can only answer who is asking. A route's question is asked about
|
|
234
|
+
``state`` -- the rendered text of what the pipeline has produced so far -- so it
|
|
235
|
+
can answer what the request *means*.
|
|
236
|
+
|
|
237
|
+
Every way the question can fail fails the run rather than picking an arm:
|
|
238
|
+
a decision with no definition in this runtime, a client that raises, an answer
|
|
239
|
+
with no choice, and a label no arm maps. A default is deliberately not a fourth
|
|
240
|
+
outcome; it is the arm an *unexpected* label runs, and the spec gate refuses a
|
|
241
|
+
default that a mapped set already covers.
|
|
242
|
+
|
|
243
|
+
``as_`` binds the label that was answered -- including one that fell through to
|
|
244
|
+
``otherwise`` -- so a later step can name the arm that ran rather than re-deriving
|
|
245
|
+
it. The client is built on first use, not when the pipeline is assembled: an app
|
|
246
|
+
with no TypeSafe key still boots as long as no request reaches a route.
|
|
247
|
+
"""
|
|
248
|
+
declared = dict(decisions or {})
|
|
249
|
+
factory = client_factory or DecisionClient
|
|
250
|
+
client: DecisionAsker | None = None
|
|
251
|
+
|
|
252
|
+
async def _route(context: PipelineContext) -> PipelineContext:
|
|
253
|
+
nonlocal client
|
|
254
|
+
nested = _enter(context, max_depth, f"route ({decision})")
|
|
255
|
+
definition = declared.get(decision)
|
|
256
|
+
if definition is None:
|
|
257
|
+
raise PipelineError(
|
|
258
|
+
f"route needs a definition for decision {decision!r}; the generated app "
|
|
259
|
+
f"carries one for every decision its routes name"
|
|
260
|
+
)
|
|
261
|
+
if client is None:
|
|
262
|
+
client = factory()
|
|
263
|
+
question = DecisionQuestion(
|
|
264
|
+
type="choice",
|
|
265
|
+
instructions=definition.instructions,
|
|
266
|
+
criteria=definition.criteria,
|
|
267
|
+
)
|
|
268
|
+
try:
|
|
269
|
+
response = await client.decide(
|
|
270
|
+
model=definition.model,
|
|
271
|
+
state=state,
|
|
272
|
+
questions={decision: question},
|
|
273
|
+
)
|
|
274
|
+
except Exception as error:
|
|
275
|
+
raise PipelineError(f"route decision {decision!r} could not answer: {error}") from error
|
|
276
|
+
answer = response.answers.get(decision)
|
|
277
|
+
label = None if answer is None else answer.choice
|
|
278
|
+
if label is None:
|
|
279
|
+
raise PipelineError(
|
|
280
|
+
f"route decision {decision!r} returned no choice, so no arm can be "
|
|
281
|
+
f"selected; a route does not guess which one was meant"
|
|
282
|
+
)
|
|
283
|
+
chosen = on.get(label)
|
|
284
|
+
if chosen is None:
|
|
285
|
+
if otherwise is None:
|
|
286
|
+
raise PipelineError(
|
|
287
|
+
f"route decision {decision!r} answered {label!r}, which no arm maps "
|
|
288
|
+
f"and no `default` catches"
|
|
289
|
+
)
|
|
290
|
+
chosen = otherwise
|
|
291
|
+
_, added = await _run_body(chosen, nested)
|
|
292
|
+
if as_ is not None:
|
|
293
|
+
added[as_] = label
|
|
294
|
+
return _escape(context.with_values(**added))
|
|
295
|
+
|
|
296
|
+
return _named(_route, name)
|
|
297
|
+
|
|
298
|
+
|
|
211
299
|
def parallel(
|
|
212
300
|
*,
|
|
213
301
|
branches: Sequence[Pipeline],
|
aid_runtime/decisions.py
CHANGED
|
@@ -38,9 +38,12 @@ from typing import Any, Literal
|
|
|
38
38
|
import httpx
|
|
39
39
|
|
|
40
40
|
from aid_runtime.errors import ConfigurationError, ProviderError, ProviderResponseError
|
|
41
|
+
from aid_runtime.tracing import llm_span, record_attributes, record_cost, record_usage
|
|
42
|
+
from aid_runtime.types import TokenUsage
|
|
41
43
|
|
|
42
44
|
__all__ = [
|
|
43
45
|
"DEFAULT_TYPESAFE_BASE_URL",
|
|
46
|
+
"JEV_INPUT_USD_PER_MTOK",
|
|
44
47
|
"JEV_MOVING_ALIASES",
|
|
45
48
|
"TYPESAFE_API_KEY_ENVS",
|
|
46
49
|
"TYPESAFE_BASE_URL_ENV",
|
|
@@ -48,6 +51,7 @@ __all__ = [
|
|
|
48
51
|
"DecisionClient",
|
|
49
52
|
"DecisionQuestion",
|
|
50
53
|
"DecisionResponse",
|
|
54
|
+
"decision_cost_usd",
|
|
51
55
|
"decision_key",
|
|
52
56
|
"is_reproducible_model",
|
|
53
57
|
]
|
|
@@ -64,6 +68,11 @@ TYPESAFE_BASE_URL_ENV = "TYPESAFE_BASE_URL"
|
|
|
64
68
|
#: ``reproducible=True``.
|
|
65
69
|
JEV_MOVING_ALIASES = frozenset({"jev-latest", "jev-preview"})
|
|
66
70
|
|
|
71
|
+
#: Jev bills input tokens at $42 per billion ($0.042 per million) and states that
|
|
72
|
+
#: output tokens are free. A call therefore needs an input row only, and a
|
|
73
|
+
#: decision run must never be reported as unpriceable.
|
|
74
|
+
JEV_INPUT_USD_PER_MTOK = 0.042
|
|
75
|
+
|
|
67
76
|
QuestionType = Literal["choice", "score", "noul"]
|
|
68
77
|
|
|
69
78
|
_QUESTION_TYPES: frozenset[str] = frozenset({"choice", "score", "noul"})
|
|
@@ -93,6 +102,19 @@ def is_reproducible_model(model: str) -> bool:
|
|
|
93
102
|
return model not in JEV_MOVING_ALIASES
|
|
94
103
|
|
|
95
104
|
|
|
105
|
+
def decision_cost_usd(input_tokens: int | None) -> float:
|
|
106
|
+
"""Price one decision call from its input tokens alone.
|
|
107
|
+
|
|
108
|
+
Output tokens are free by contract, so they are deliberately not part of the
|
|
109
|
+
arithmetic: a caller that passed a total would overcharge the run. ``None``
|
|
110
|
+
means the vendor reported no usage; :meth:`DecisionClient.decide` then records
|
|
111
|
+
no cost at all, so the call stays unknown rather than free.
|
|
112
|
+
"""
|
|
113
|
+
if input_tokens is None:
|
|
114
|
+
return 0.0
|
|
115
|
+
return (input_tokens / 1_000_000) * JEV_INPUT_USD_PER_MTOK
|
|
116
|
+
|
|
117
|
+
|
|
96
118
|
@dataclass(frozen=True, slots=True)
|
|
97
119
|
class DecisionQuestion:
|
|
98
120
|
"""One typed question.
|
|
@@ -228,8 +250,22 @@ class DecisionClient:
|
|
|
228
250
|
"state": state,
|
|
229
251
|
"questions": {key: question.as_payload() for key, question in questions.items()},
|
|
230
252
|
}
|
|
231
|
-
|
|
232
|
-
|
|
253
|
+
# A decision is billed like any other model call, so it gets the same
|
|
254
|
+
# span: `aid cost` and the eval budget read `aid.cost.usd` off it.
|
|
255
|
+
with llm_span(model=model, provider=self._name, operation="decision") as span:
|
|
256
|
+
body = await self._post(payload)
|
|
257
|
+
response = self._parse(body, questions)
|
|
258
|
+
if response.input_tokens is not None:
|
|
259
|
+
usage = TokenUsage(
|
|
260
|
+
input_tokens=response.input_tokens,
|
|
261
|
+
output_tokens=response.output_tokens or 0,
|
|
262
|
+
)
|
|
263
|
+
record_usage(span, usage)
|
|
264
|
+
record_cost(span, decision_cost_usd(usage.input_tokens))
|
|
265
|
+
# The vendor echoes the version that actually answered, which is the
|
|
266
|
+
# only record of what a moving alias resolved to.
|
|
267
|
+
record_attributes(span, response_model=response.model)
|
|
268
|
+
return response
|
|
233
269
|
|
|
234
270
|
async def close(self) -> None:
|
|
235
271
|
if self._closed:
|
aid_runtime/tracing.py
CHANGED
|
@@ -14,8 +14,8 @@ that must not decide a host application's telemetry setup for it.
|
|
|
14
14
|
from __future__ import annotations
|
|
15
15
|
|
|
16
16
|
import contextlib
|
|
17
|
-
from collections.abc import Iterator
|
|
18
|
-
from typing import
|
|
17
|
+
from collections.abc import Iterator, Sequence
|
|
18
|
+
from typing import Any, Protocol
|
|
19
19
|
|
|
20
20
|
from opentelemetry import trace
|
|
21
21
|
from opentelemetry.trace import Span, SpanKind, Status, StatusCode
|
|
@@ -23,12 +23,10 @@ from opentelemetry.trace import Span, SpanKind, Status, StatusCode
|
|
|
23
23
|
from aid_runtime.local_trace import finish_span, wrap_span
|
|
24
24
|
from aid_runtime.types import TokenUsage
|
|
25
25
|
|
|
26
|
-
if TYPE_CHECKING:
|
|
27
|
-
from aid_runtime.agents.guardrails import GuardrailRecord
|
|
28
|
-
|
|
29
26
|
__all__ = [
|
|
30
27
|
"ATTRIBUTES",
|
|
31
28
|
"GEN_AI_OPERATION_CHAT",
|
|
29
|
+
"GuardrailEvent",
|
|
32
30
|
"agent_span",
|
|
33
31
|
"aid_attributes",
|
|
34
32
|
"get_tracer",
|
|
@@ -226,7 +224,29 @@ def record_hitl(
|
|
|
226
224
|
span.add_event("aid.hitl", attributes)
|
|
227
225
|
|
|
228
226
|
|
|
229
|
-
|
|
227
|
+
class GuardrailEvent(Protocol):
|
|
228
|
+
"""The fields :func:`record_guardrail` reads from a fired guardrail.
|
|
229
|
+
|
|
230
|
+
Structural on purpose. Importing ``GuardrailRecord`` would close a module cycle --
|
|
231
|
+
``guardrails`` imports the decision client, which imports this module, which would
|
|
232
|
+
import ``guardrails`` again -- and a protocol keeps the annotation honest without
|
|
233
|
+
the import.
|
|
234
|
+
"""
|
|
235
|
+
|
|
236
|
+
@property
|
|
237
|
+
def guardrail(self) -> str: ...
|
|
238
|
+
|
|
239
|
+
@property
|
|
240
|
+
def stage(self) -> str: ...
|
|
241
|
+
|
|
242
|
+
@property
|
|
243
|
+
def action(self) -> str: ...
|
|
244
|
+
|
|
245
|
+
@property
|
|
246
|
+
def policies(self) -> Sequence[str]: ...
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def record_guardrail(span: Span, record: GuardrailEvent) -> None:
|
|
230
250
|
"""Record one guardrail hit as an ``aid.guardrail`` span event."""
|
|
231
251
|
span.add_event(
|
|
232
252
|
"aid.guardrail",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: aid-runtime-python
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.96
|
|
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
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
aid_runtime/__init__.py,sha256=
|
|
1
|
+
aid_runtime/__init__.py,sha256=uwsi41AfQfY4kN68MjH06XNsfD4Ruj6vY28mdrFRDD4,12527
|
|
2
2
|
aid_runtime/cache.py,sha256=jhw_EBd9dfWEUzYeibOvIkVBqqiLUjqF5WKEPz9GkhE,6836
|
|
3
3
|
aid_runtime/concurrency.py,sha256=z0tFQHw35ttFLKfazGBjCMPOLQOnUotE_OPpvR1piH4,2829
|
|
4
|
-
aid_runtime/control.py,sha256=
|
|
4
|
+
aid_runtime/control.py,sha256=y6wPn0fqYJv0nVSsjzpzfiaq1Y7zBoMxCpdrEuqkfvg,17529
|
|
5
5
|
aid_runtime/cost.py,sha256=Hc7X6mtkeanL9eJ1lEZZCQ9LxZurhL1it0gm3s9LpQE,5465
|
|
6
|
-
aid_runtime/decisions.py,sha256=
|
|
6
|
+
aid_runtime/decisions.py,sha256=1V0d9EttfwTuh8jgtC5X3wQDTZrl20ZREcs6VqIdcHY,15662
|
|
7
7
|
aid_runtime/errors.py,sha256=TKkZyZZcFTl5yxNKlA8jUF4ORpYHLGFEvc6r9FK4X70,12572
|
|
8
8
|
aid_runtime/gateway.py,sha256=p0qsaQUAYrwSzlWwpUi1wY6pdOeU98QjsWLzq8045Fo,18267
|
|
9
9
|
aid_runtime/local_trace.py,sha256=eg5LDsg1VfNmJNqs8KjtdzTjSqg7SWTXunLwg-b3IcM,7870
|
|
@@ -13,7 +13,7 @@ aid_runtime/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
|
13
13
|
aid_runtime/steps.py,sha256=44v8-ebN77VGZePmfhBYi0KORFBrH2GWFSzlfczaJhs,12047
|
|
14
14
|
aid_runtime/structured.py,sha256=Uga-VoGJN56lUtr-M3nmUjAKGF32cAAk-p7WceUgPzg,5061
|
|
15
15
|
aid_runtime/tools.py,sha256=7LeaPI5aJioNe3vexUv94tvIRpOwxj1Ujt723qvO3UI,9906
|
|
16
|
-
aid_runtime/tracing.py,sha256=
|
|
16
|
+
aid_runtime/tracing.py,sha256=q7v1GQ0FaYOq4uWfGMhloXwQb3fXjxE0ixi6Yij9OJQ,9624
|
|
17
17
|
aid_runtime/types.py,sha256=cVYgmacDIhStQT5dX_X67-q57sPBBaWC5nybtGVqVsw,10389
|
|
18
18
|
aid_runtime/agents/__init__.py,sha256=Qkcjm0YokmObQWRB3G_xFd_LI4sku_GajDbE_7TzyF8,2488
|
|
19
19
|
aid_runtime/agents/chat_model.py,sha256=fZdCmSpoECuBVGZE1s8NmT2erT33RhDuLh3yJM6kAJE,14808
|
|
@@ -51,7 +51,7 @@ aid_runtime/retrieval/rerank.py,sha256=L4ICBi9YHDzhnKVtImKh5Wzre1HDzS8at-AKDkXBJ
|
|
|
51
51
|
aid_runtime/retrieval/retriever.py,sha256=hVLP0xOLKioYsa-aS1akyJl5p-WbpVjm8OfCw26m4CE,13941
|
|
52
52
|
aid_runtime/retrieval/stores.py,sha256=VEi68AaMHcQjdAbWOHflp3jCl1_1db71rIzZjTouLlo,5857
|
|
53
53
|
aid_runtime/retrieval/types.py,sha256=8vKP21rmE9LglnB5gtv0dLBIkXzaPqdG3-GS98K0gpc,13498
|
|
54
|
-
aid_runtime_python-0.1.
|
|
55
|
-
aid_runtime_python-0.1.
|
|
56
|
-
aid_runtime_python-0.1.
|
|
57
|
-
aid_runtime_python-0.1.
|
|
54
|
+
aid_runtime_python-0.1.96.dist-info/METADATA,sha256=ezB_vP8FTKeo4AlhZQI5f5eXj_lrDFRBFBlWCf3XWnw,17005
|
|
55
|
+
aid_runtime_python-0.1.96.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
56
|
+
aid_runtime_python-0.1.96.dist-info/top_level.txt,sha256=QvODgNVRDDMmLcTnoIQ4jYnhqf-N4b9sHWEmaUUxp44,12
|
|
57
|
+
aid_runtime_python-0.1.96.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|