aid-runtime-python 0.1.89__tar.gz → 0.1.91__tar.gz
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_python-0.1.89 → aid_runtime_python-0.1.91}/PKG-INFO +1 -1
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/pyproject.toml +1 -1
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/__init__.py +1 -1
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/agents/__init__.py +4 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/agents/guardrails.py +185 -4
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/agents/ir.py +25 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/evals/__init__.py +2 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/evals/metrics.py +90 -1
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime_python.egg-info/PKG-INFO +1 -1
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime_python.egg-info/SOURCES.txt +1 -0
- aid_runtime_python-0.1.91/tests/test_evals_decision.py +150 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_guardrails.py +172 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/README.md +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/setup.cfg +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/agents/chat_model.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/agents/deep_agents.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/agents/engine.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/agents/loop.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/agents/memory.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/agents/policy.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/agents/state.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/cache.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/concurrency.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/control.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/cost.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/decisions.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/errors.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/evals/dataset.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/evals/gate.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/evals/predicate.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/evals/runner.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/gateway.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/local_trace.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/pipeline.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/prompts.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/providers/__init__.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/providers/anthropic.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/providers/base.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/providers/bedrock.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/providers/fake.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/providers/http.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/providers/vertex.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/py.typed +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/__init__.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/chunking.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/citations.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/connectors.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/embedding.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/gate.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/hybrid.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/metrics.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/pgvector.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/rerank.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/retriever.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/stores.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/types.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/steps.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/structured.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/tools.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/tracing.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/types.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime_python.egg-info/dependency_links.txt +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime_python.egg-info/requires.txt +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime_python.egg-info/top_level.txt +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_agents_budget.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_agents_deep_agents.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_agents_hitl.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_agents_loop.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_agents_memory.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_anthropic_provider.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_bedrock_provider.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_cache.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_concurrency.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_control.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_cost.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_dataset.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_decisions.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_evals.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_evals_assert_gate.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_evals_gate.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_evals_predicate.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_gateway.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_http_provider.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_local_trace.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_package.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_pipeline.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_prompts.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_retrieval_chunking.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_retrieval_citations.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_retrieval_connectors.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_retrieval_embedding.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_retrieval_gate.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_retrieval_hybrid.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_retrieval_metrics.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_retrieval_rerank.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_retrieval_stores.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_retrieval_types.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_steps.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_streaming.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_structured.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_tool_permissions.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_tools.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_tracing.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_types.py +0 -0
- {aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/tests/test_vertex_provider.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: aid-runtime-python
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.91
|
|
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
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "aid-runtime-python"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.91"
|
|
8
8
|
description = "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."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.11"
|
|
@@ -19,6 +19,7 @@ from aid_runtime.agents.guardrails import (
|
|
|
19
19
|
DETECTORS,
|
|
20
20
|
GUARDRAIL_ACTIONS,
|
|
21
21
|
GUARDRAIL_STAGES,
|
|
22
|
+
DecisionAsker,
|
|
22
23
|
Detection,
|
|
23
24
|
GuardrailEngine,
|
|
24
25
|
GuardrailEscalation,
|
|
@@ -28,6 +29,7 @@ from aid_runtime.agents.guardrails import (
|
|
|
28
29
|
)
|
|
29
30
|
from aid_runtime.agents.ir import (
|
|
30
31
|
AgentDefinition,
|
|
32
|
+
DecisionDefinition,
|
|
31
33
|
GuardrailDefinition,
|
|
32
34
|
HumanApproval,
|
|
33
35
|
MemoryDefinition,
|
|
@@ -65,6 +67,8 @@ __all__ = [
|
|
|
65
67
|
"AgentCheckpoint",
|
|
66
68
|
"AgentDefinition",
|
|
67
69
|
"AgentRuntime",
|
|
70
|
+
"DecisionAsker",
|
|
71
|
+
"DecisionDefinition",
|
|
68
72
|
"Detection",
|
|
69
73
|
"FileMemoryStore",
|
|
70
74
|
"GuardrailDefinition",
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/agents/guardrails.py
RENAMED
|
@@ -43,13 +43,14 @@ detector matched, and the run carried on because the spec said the run may.
|
|
|
43
43
|
from __future__ import annotations
|
|
44
44
|
|
|
45
45
|
import re
|
|
46
|
-
from collections.abc import Mapping, Sequence
|
|
46
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
47
47
|
from dataclasses import dataclass, field
|
|
48
|
-
from typing import Any, Literal
|
|
48
|
+
from typing import Any, Literal, Protocol, cast
|
|
49
49
|
|
|
50
50
|
from pydantic import BaseModel, ConfigDict
|
|
51
51
|
|
|
52
|
-
from aid_runtime.agents.ir import GuardrailDefinition
|
|
52
|
+
from aid_runtime.agents.ir import DecisionDefinition, GuardrailDefinition
|
|
53
|
+
from aid_runtime.decisions import DecisionClient, DecisionQuestion, DecisionResponse
|
|
53
54
|
from aid_runtime.errors import ConfigurationError, GuardrailBlockedError
|
|
54
55
|
|
|
55
56
|
__all__ = [
|
|
@@ -108,6 +109,11 @@ class GuardrailRecord(BaseModel):
|
|
|
108
109
|
action: GuardrailAction
|
|
109
110
|
policies: tuple[str, ...]
|
|
110
111
|
message: str
|
|
112
|
+
#: Set when a decision answered rather than a detector, so a reviewer can tell
|
|
113
|
+
#: "the classifier said block" from "the regex matched" without the log message.
|
|
114
|
+
decision: str | None = None
|
|
115
|
+
label: str | None = None
|
|
116
|
+
confidence: float | None = None
|
|
111
117
|
|
|
112
118
|
|
|
113
119
|
class GuardrailEscalation(BaseModel):
|
|
@@ -243,6 +249,10 @@ DETECTORS: Mapping[str, tuple[re.Pattern[str], ...]] = {
|
|
|
243
249
|
#: safe -- which is why a ``redact`` guardrail over them fails closed below.
|
|
244
250
|
_REDACTABLE = frozenset({"pii-detect", "pii-redact", "secret-scan"})
|
|
245
251
|
|
|
252
|
+
#: The outcomes a decision label may map to. ``allow`` is the one that records
|
|
253
|
+
#: nothing; ``redact`` is absent because a label cannot name the field to rewrite.
|
|
254
|
+
_DECISION_OUTCOMES = frozenset({"allow", "block", "flag", "escalate"})
|
|
255
|
+
|
|
246
256
|
|
|
247
257
|
def detect(policy: str, text: str) -> tuple[Detection, ...]:
|
|
248
258
|
"""Every match of ``policy`` in ``text``, in document order.
|
|
@@ -287,6 +297,19 @@ def _texts(payload: Mapping[str, Any]) -> tuple[tuple[str, str], ...]:
|
|
|
287
297
|
)
|
|
288
298
|
|
|
289
299
|
|
|
300
|
+
class DecisionAsker(Protocol):
|
|
301
|
+
"""The one call the engine makes, so a test double need not subclass the client."""
|
|
302
|
+
|
|
303
|
+
async def decide(
|
|
304
|
+
self,
|
|
305
|
+
*,
|
|
306
|
+
model: str,
|
|
307
|
+
state: Any,
|
|
308
|
+
questions: Mapping[str, DecisionQuestion],
|
|
309
|
+
reproducible: bool = False,
|
|
310
|
+
) -> DecisionResponse: ...
|
|
311
|
+
|
|
312
|
+
|
|
290
313
|
class GuardrailEngine:
|
|
291
314
|
"""Evaluates an agent's guardrails at one stage.
|
|
292
315
|
|
|
@@ -295,8 +318,18 @@ class GuardrailEngine:
|
|
|
295
318
|
same way, which is what makes the corpus results reproducible.
|
|
296
319
|
"""
|
|
297
320
|
|
|
298
|
-
def __init__(
|
|
321
|
+
def __init__(
|
|
322
|
+
self,
|
|
323
|
+
guardrails: Mapping[str, GuardrailDefinition] | None = None,
|
|
324
|
+
decisions: Mapping[str, DecisionDefinition] | None = None,
|
|
325
|
+
client_factory: Callable[[], DecisionAsker] | None = None,
|
|
326
|
+
) -> None:
|
|
299
327
|
self._declared = dict(guardrails or {})
|
|
328
|
+
self._decisions = dict(decisions or {})
|
|
329
|
+
#: Built on first use, not at import: an app with no TypeSafe key must still
|
|
330
|
+
#: boot when no request reaches a decision-backed guardrail.
|
|
331
|
+
self._client_factory = client_factory or DecisionClient
|
|
332
|
+
self._client: DecisionAsker | None = None
|
|
300
333
|
|
|
301
334
|
def declared(self, guardrail_id: str) -> GuardrailDefinition | None:
|
|
302
335
|
return self._declared.get(guardrail_id)
|
|
@@ -308,6 +341,154 @@ class GuardrailEngine:
|
|
|
308
341
|
) -> GuardrailVerdict:
|
|
309
342
|
return self._check("input", guardrails, {"text": text}, tool_id=None)
|
|
310
343
|
|
|
344
|
+
async def check_input_async(
|
|
345
|
+
self,
|
|
346
|
+
guardrails: Sequence[GuardrailDefinition],
|
|
347
|
+
text: str,
|
|
348
|
+
) -> GuardrailVerdict:
|
|
349
|
+
"""The ``input`` stage with decisions: the same rules, one awaitable question.
|
|
350
|
+
|
|
351
|
+
This is the only stage a decision may guard, so it is the only stage with an
|
|
352
|
+
async twin; ``check_input`` stays synchronous and unchanged for callers that
|
|
353
|
+
have no network check to make. Order is preserved across both kinds of
|
|
354
|
+
guardrail -- declaration order still decides between comparable actions, and
|
|
355
|
+
``block`` still outranks it.
|
|
356
|
+
|
|
357
|
+
A guardrail that declares both policies and a decision asks the decision only
|
|
358
|
+
when its policies found nothing: asking a model about input a detector already
|
|
359
|
+
refused would spend tokens to learn what the detector said.
|
|
360
|
+
"""
|
|
361
|
+
payload: Mapping[str, Any] = {"text": text}
|
|
362
|
+
applicable = [guardrail for guardrail in guardrails if "input" in guardrail.stages]
|
|
363
|
+
records: list[GuardrailRecord] = []
|
|
364
|
+
fired: list[GuardrailDefinition] = []
|
|
365
|
+
for guardrail in applicable:
|
|
366
|
+
policies = _firing_policies(guardrail, "input", payload)
|
|
367
|
+
record: GuardrailRecord | None = None
|
|
368
|
+
if policies:
|
|
369
|
+
record = GuardrailRecord(
|
|
370
|
+
guardrail=guardrail.id,
|
|
371
|
+
stage="input",
|
|
372
|
+
action=_action(guardrail),
|
|
373
|
+
policies=policies,
|
|
374
|
+
message=guardrail.message,
|
|
375
|
+
)
|
|
376
|
+
elif guardrail.decision is not None:
|
|
377
|
+
record = await self._ask_decision(guardrail, text)
|
|
378
|
+
if record is None:
|
|
379
|
+
continue
|
|
380
|
+
fired.append(guardrail)
|
|
381
|
+
records.append(record)
|
|
382
|
+
|
|
383
|
+
deciding = _decide(records)
|
|
384
|
+
if deciding is None:
|
|
385
|
+
return GuardrailVerdict(stage="input", text=text, arguments=dict(payload))
|
|
386
|
+
|
|
387
|
+
redacted = payload
|
|
388
|
+
if deciding.action == ACTION_REDACT:
|
|
389
|
+
redacted = _redact(
|
|
390
|
+
"input",
|
|
391
|
+
[guardrail for guardrail in fired if guardrail.action == ACTION_REDACT],
|
|
392
|
+
payload,
|
|
393
|
+
)
|
|
394
|
+
return GuardrailVerdict(
|
|
395
|
+
stage="input",
|
|
396
|
+
records=tuple(records),
|
|
397
|
+
action=deciding.action,
|
|
398
|
+
deciding=deciding,
|
|
399
|
+
text=_text_of(redacted),
|
|
400
|
+
arguments=dict(redacted),
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
async def _ask_decision(
|
|
404
|
+
self,
|
|
405
|
+
guardrail: GuardrailDefinition,
|
|
406
|
+
text: str,
|
|
407
|
+
) -> GuardrailRecord | None:
|
|
408
|
+
"""Ask one ``choice`` decision and turn its label into a record, or ``None``.
|
|
409
|
+
|
|
410
|
+
Everything that is not a usable answer is a :class:`GuardrailBlockedError`:
|
|
411
|
+
an undeclared decision, an unmapped label, a confidence below the declared
|
|
412
|
+
floor, or a call that raised. That is the same rule the policy path follows
|
|
413
|
+
-- there is no path from "the check could not run" to "no violation found".
|
|
414
|
+
"""
|
|
415
|
+
name = guardrail.decision or ""
|
|
416
|
+
definition = self._decisions.get(name)
|
|
417
|
+
if definition is None:
|
|
418
|
+
raise GuardrailBlockedError(
|
|
419
|
+
guardrail.id,
|
|
420
|
+
"input",
|
|
421
|
+
f"decision {name!r} has no definition in this runtime",
|
|
422
|
+
)
|
|
423
|
+
outcomes = dict(guardrail.on_decision)
|
|
424
|
+
if not outcomes:
|
|
425
|
+
raise GuardrailBlockedError(
|
|
426
|
+
guardrail.id,
|
|
427
|
+
"input",
|
|
428
|
+
f"decision {name!r} maps no answer label, so no answer could be acted on",
|
|
429
|
+
)
|
|
430
|
+
if self._client is None:
|
|
431
|
+
self._client = self._client_factory()
|
|
432
|
+
question = DecisionQuestion(
|
|
433
|
+
type="choice",
|
|
434
|
+
instructions=definition.instructions,
|
|
435
|
+
criteria=definition.criteria,
|
|
436
|
+
)
|
|
437
|
+
try:
|
|
438
|
+
response = await self._client.decide(
|
|
439
|
+
model=definition.model,
|
|
440
|
+
state=text,
|
|
441
|
+
questions={name: question},
|
|
442
|
+
)
|
|
443
|
+
except Exception as error:
|
|
444
|
+
raise GuardrailBlockedError(
|
|
445
|
+
guardrail.id,
|
|
446
|
+
"input",
|
|
447
|
+
f"decision {name!r} could not answer: {error}",
|
|
448
|
+
) from error
|
|
449
|
+
|
|
450
|
+
answer = response.answers.get(name)
|
|
451
|
+
if answer is None:
|
|
452
|
+
raise GuardrailBlockedError(
|
|
453
|
+
guardrail.id,
|
|
454
|
+
"input",
|
|
455
|
+
f"decision {name!r} returned no answer for its own question",
|
|
456
|
+
)
|
|
457
|
+
label = answer.choice
|
|
458
|
+
if label is None or label not in outcomes:
|
|
459
|
+
raise GuardrailBlockedError(
|
|
460
|
+
guardrail.id,
|
|
461
|
+
"input",
|
|
462
|
+
f"decision {name!r} answered {label!r}, which this guardrail does not map",
|
|
463
|
+
)
|
|
464
|
+
if answer.confidence is not None and answer.confidence < guardrail.min_confidence:
|
|
465
|
+
raise GuardrailBlockedError(
|
|
466
|
+
guardrail.id,
|
|
467
|
+
"input",
|
|
468
|
+
f"decision {name!r} answered {label!r} with confidence "
|
|
469
|
+
f"{answer.confidence:.2f}, below the declared minimum "
|
|
470
|
+
f"{guardrail.min_confidence:.2f}",
|
|
471
|
+
)
|
|
472
|
+
outcome = outcomes[label]
|
|
473
|
+
if outcome not in _DECISION_OUTCOMES:
|
|
474
|
+
raise GuardrailBlockedError(
|
|
475
|
+
guardrail.id,
|
|
476
|
+
"input",
|
|
477
|
+
f"decision {name!r} maps {label!r} to unknown outcome {outcome!r}",
|
|
478
|
+
)
|
|
479
|
+
if outcome == "allow":
|
|
480
|
+
return None
|
|
481
|
+
return GuardrailRecord(
|
|
482
|
+
guardrail=guardrail.id,
|
|
483
|
+
stage="input",
|
|
484
|
+
action=cast(GuardrailAction, outcome),
|
|
485
|
+
policies=(),
|
|
486
|
+
message=guardrail.message,
|
|
487
|
+
decision=name,
|
|
488
|
+
label=label,
|
|
489
|
+
confidence=answer.confidence,
|
|
490
|
+
)
|
|
491
|
+
|
|
311
492
|
def check_output(
|
|
312
493
|
self,
|
|
313
494
|
guardrails: Sequence[GuardrailDefinition],
|
|
@@ -28,6 +28,7 @@ from aid_runtime.types import (
|
|
|
28
28
|
|
|
29
29
|
__all__ = [
|
|
30
30
|
"AgentDefinition",
|
|
31
|
+
"DecisionDefinition",
|
|
31
32
|
"GuardrailDefinition",
|
|
32
33
|
"HumanApproval",
|
|
33
34
|
"MemoryDefinition",
|
|
@@ -83,12 +84,33 @@ class ToolSpec:
|
|
|
83
84
|
auth: Mapping[str, Any] | None
|
|
84
85
|
|
|
85
86
|
|
|
87
|
+
@dataclass(frozen=True, slots=True)
|
|
88
|
+
class DecisionDefinition:
|
|
89
|
+
"""A decision resolved from the IR's ``decisions`` collection.
|
|
90
|
+
|
|
91
|
+
Only the fields a runtime needs are here: the model that answers, the
|
|
92
|
+
question, and the ordered labels or levels. A guardrail uses a ``choice``
|
|
93
|
+
decision's labels through an ``on_decision`` map; an eval's ``DecisionScore``
|
|
94
|
+
uses a ``score`` decision's criteria, where the position is the score.
|
|
95
|
+
"""
|
|
96
|
+
|
|
97
|
+
id: str
|
|
98
|
+
model: str
|
|
99
|
+
instructions: str
|
|
100
|
+
criteria: tuple[str, ...]
|
|
101
|
+
|
|
102
|
+
|
|
86
103
|
@dataclass(frozen=True, slots=True)
|
|
87
104
|
class GuardrailDefinition:
|
|
88
105
|
"""One resolved guardrail: where it runs, what it detects, and what it does.
|
|
89
106
|
|
|
90
107
|
``tools`` holds tool *ids*, because that is how the IR scopes a tool-call
|
|
91
108
|
guardrail; the loop translates a model's tool *name* to the id before asking.
|
|
109
|
+
|
|
110
|
+
``decision`` names a ``choice`` decision to ask at the ``input`` stage, and
|
|
111
|
+
``on_decision`` maps each of its labels to an outcome. ``allow`` is the one
|
|
112
|
+
outcome that produces no record; the rest are guardrail actions. The trailing
|
|
113
|
+
defaults keep every keyword call site written before decisions existed valid.
|
|
92
114
|
"""
|
|
93
115
|
|
|
94
116
|
id: str
|
|
@@ -100,6 +122,9 @@ class GuardrailDefinition:
|
|
|
100
122
|
message: str
|
|
101
123
|
fallback_prompt: str | None
|
|
102
124
|
log: bool
|
|
125
|
+
decision: str | None = None
|
|
126
|
+
on_decision: tuple[tuple[str, str], ...] = ()
|
|
127
|
+
min_confidence: float = 0.0
|
|
103
128
|
|
|
104
129
|
|
|
105
130
|
@dataclass(frozen=True, slots=True)
|
|
@@ -17,6 +17,7 @@ from aid_runtime.evals.gate import (
|
|
|
17
17
|
)
|
|
18
18
|
from aid_runtime.evals.metrics import (
|
|
19
19
|
AssertMetric,
|
|
20
|
+
DecisionScore,
|
|
20
21
|
ExactMatch,
|
|
21
22
|
JsonSchemaValid,
|
|
22
23
|
JudgeVerdict,
|
|
@@ -44,6 +45,7 @@ __all__ = [
|
|
|
44
45
|
"CaseOutcome",
|
|
45
46
|
"Dataset",
|
|
46
47
|
"DatasetRun",
|
|
48
|
+
"DecisionScore",
|
|
47
49
|
"EvalBaseline",
|
|
48
50
|
"EvalCase",
|
|
49
51
|
"EvalRun",
|
|
@@ -15,13 +15,17 @@ from typing import Any, Protocol, runtime_checkable
|
|
|
15
15
|
|
|
16
16
|
from pydantic import BaseModel, ConfigDict, Field
|
|
17
17
|
|
|
18
|
-
from aid_runtime.
|
|
18
|
+
from aid_runtime.agents.guardrails import DecisionAsker
|
|
19
|
+
from aid_runtime.agents.ir import DecisionDefinition
|
|
20
|
+
from aid_runtime.decisions import DecisionClient, DecisionQuestion, DecisionResponse
|
|
21
|
+
from aid_runtime.errors import ConfigurationError, EvalError
|
|
19
22
|
from aid_runtime.evals.dataset import EvalCase
|
|
20
23
|
from aid_runtime.evals.predicate import evaluate_predicate
|
|
21
24
|
from aid_runtime.structured import run_structured
|
|
22
25
|
|
|
23
26
|
__all__ = [
|
|
24
27
|
"AssertMetric",
|
|
28
|
+
"DecisionScore",
|
|
25
29
|
"ExactMatch",
|
|
26
30
|
"JsonSchemaValid",
|
|
27
31
|
"JudgeVerdict",
|
|
@@ -273,6 +277,91 @@ class LlmJudge:
|
|
|
273
277
|
)
|
|
274
278
|
|
|
275
279
|
|
|
280
|
+
class DecisionScore:
|
|
281
|
+
"""Scores a prediction with a TypeSafe AI Jev ``score`` decision.
|
|
282
|
+
|
|
283
|
+
``deterministic`` is ``False``: the answer comes from a model, so the gate
|
|
284
|
+
refuses to threshold on it and ``gate.ci`` may not declare it. What makes a
|
|
285
|
+
``score`` decision the right rubric here is that its ``criteria`` are
|
|
286
|
+
ordered: the position the model returns *is* the score, so this metric maps
|
|
287
|
+
that position onto ``[0, 1]`` instead of inventing an order for labels.
|
|
288
|
+
|
|
289
|
+
A call that fails, or an answer outside the declared range, raises rather
|
|
290
|
+
than scoring. A ``0.0`` would read as a regression the candidate caused and
|
|
291
|
+
a ``1.0`` would silently pass; neither is what "the vendor did not answer"
|
|
292
|
+
means.
|
|
293
|
+
|
|
294
|
+
``threshold`` marks a single answer as passing, which is useful when the
|
|
295
|
+
metric is read directly. It is not a gate: ``BaselineGate`` refuses a
|
|
296
|
+
``Threshold`` on any non-deterministic metric, so a spec has no way to floor
|
|
297
|
+
this score and the generated eval never asks for one.
|
|
298
|
+
"""
|
|
299
|
+
|
|
300
|
+
def __init__(
|
|
301
|
+
self,
|
|
302
|
+
decision: DecisionDefinition,
|
|
303
|
+
*,
|
|
304
|
+
threshold: float = 0.5,
|
|
305
|
+
name: str = "decision",
|
|
306
|
+
client_factory: Callable[[], DecisionAsker] | None = None,
|
|
307
|
+
) -> None:
|
|
308
|
+
if len(decision.criteria) < 2:
|
|
309
|
+
raise ConfigurationError(
|
|
310
|
+
f"decision {decision.id!r} declares {len(decision.criteria)} criteria, "
|
|
311
|
+
"but scoring needs at least two ordered levels"
|
|
312
|
+
)
|
|
313
|
+
self._decision = decision
|
|
314
|
+
self._threshold = threshold
|
|
315
|
+
self._name = name
|
|
316
|
+
self._client_factory = client_factory
|
|
317
|
+
self._client: DecisionAsker | None = None
|
|
318
|
+
|
|
319
|
+
@property
|
|
320
|
+
def name(self) -> str:
|
|
321
|
+
return self._name
|
|
322
|
+
|
|
323
|
+
@property
|
|
324
|
+
def deterministic(self) -> bool:
|
|
325
|
+
return False
|
|
326
|
+
|
|
327
|
+
async def score(self, case: EvalCase, prediction: str) -> MetricResult:
|
|
328
|
+
response = await self._ask(prediction)
|
|
329
|
+
answer = response.answers.get(self._decision.id)
|
|
330
|
+
if answer is None or answer.score is None:
|
|
331
|
+
raise EvalError(
|
|
332
|
+
f"decision {self._decision.id!r} returned no score for its own question"
|
|
333
|
+
)
|
|
334
|
+
levels = len(self._decision.criteria)
|
|
335
|
+
normalized = (answer.score - 1.0) / (levels - 1)
|
|
336
|
+
if not 0.0 <= normalized <= 1.0:
|
|
337
|
+
raise EvalError(
|
|
338
|
+
f"decision {self._decision.id!r} answered {answer.score:g}, "
|
|
339
|
+
f"which is outside the declared 1..{levels} range"
|
|
340
|
+
)
|
|
341
|
+
return MetricResult(
|
|
342
|
+
metric=self.name,
|
|
343
|
+
score=normalized,
|
|
344
|
+
passed=normalized >= self._threshold,
|
|
345
|
+
detail=f"level {answer.score:g} of {levels}",
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
async def _ask(self, prediction: str) -> DecisionResponse:
|
|
349
|
+
if self._client is None:
|
|
350
|
+
self._client = (self._client_factory or DecisionClient)()
|
|
351
|
+
return await self._client.decide(
|
|
352
|
+
model=self._decision.model,
|
|
353
|
+
state=prediction,
|
|
354
|
+
questions={
|
|
355
|
+
self._decision.id: DecisionQuestion(
|
|
356
|
+
type="score",
|
|
357
|
+
instructions=self._decision.instructions,
|
|
358
|
+
criteria=self._decision.criteria,
|
|
359
|
+
)
|
|
360
|
+
},
|
|
361
|
+
reproducible=True,
|
|
362
|
+
)
|
|
363
|
+
|
|
364
|
+
|
|
276
365
|
def default_metrics() -> tuple[Metric, ...]:
|
|
277
366
|
"""The deterministic gate metrics: everything that needs no model call."""
|
|
278
367
|
return (ExactMatch(), NormalizedContains(), JsonSchemaValid())
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime_python.egg-info/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: aid-runtime-python
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.91
|
|
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
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"""A ``score`` decision measured as an eval metric.
|
|
2
|
+
|
|
3
|
+
Two things are pinned here beyond the arithmetic: the metric asks reproducibly, so a
|
|
4
|
+
vendor temperature change cannot move a recorded score, and a vendor failure raises
|
|
5
|
+
instead of scoring. A ``0.0`` would look like a regression the candidate caused.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
|
|
14
|
+
from aid_runtime.agents.ir import DecisionDefinition
|
|
15
|
+
from aid_runtime.decisions import DecisionAnswer, DecisionQuestion, DecisionResponse
|
|
16
|
+
from aid_runtime.errors import ConfigurationError, EvalError
|
|
17
|
+
from aid_runtime.evals import DecisionScore, EvalCase
|
|
18
|
+
|
|
19
|
+
DEFINITION = DecisionDefinition(
|
|
20
|
+
id="helpfulness",
|
|
21
|
+
model="jev-1.14.0",
|
|
22
|
+
instructions="How helpful is this answer",
|
|
23
|
+
criteria=("unusable", "partial", "complete"),
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class FakeAsker:
|
|
28
|
+
"""A ``DecisionAsker`` returning one scripted answer, and recording the call."""
|
|
29
|
+
|
|
30
|
+
def __init__(self, answer: DecisionAnswer | None, *, key: str = "helpfulness") -> None:
|
|
31
|
+
self._answer = answer
|
|
32
|
+
self._key = key
|
|
33
|
+
self.calls: list[dict[str, Any]] = []
|
|
34
|
+
|
|
35
|
+
async def decide(
|
|
36
|
+
self,
|
|
37
|
+
*,
|
|
38
|
+
model: str,
|
|
39
|
+
state: Any,
|
|
40
|
+
questions: Any,
|
|
41
|
+
reproducible: bool = False,
|
|
42
|
+
) -> DecisionResponse:
|
|
43
|
+
self.calls.append(
|
|
44
|
+
{
|
|
45
|
+
"model": model,
|
|
46
|
+
"state": state,
|
|
47
|
+
"questions": dict(questions),
|
|
48
|
+
"reproducible": reproducible,
|
|
49
|
+
}
|
|
50
|
+
)
|
|
51
|
+
answers = {} if self._answer is None else {self._key: self._answer}
|
|
52
|
+
return DecisionResponse(model=model, answers=answers)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def metric_for(answer: DecisionAnswer | None, **kwargs: Any) -> tuple[DecisionScore, FakeAsker]:
|
|
56
|
+
asker = FakeAsker(answer)
|
|
57
|
+
return DecisionScore(DEFINITION, client_factory=lambda: asker, **kwargs), asker
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
CASE = EvalCase(id="a", input="Q", expected="A")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def scored(level: float) -> DecisionAnswer:
|
|
64
|
+
return DecisionAnswer(type="score", score=level)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
async def test_a_score_metric_is_not_deterministic() -> None:
|
|
68
|
+
metric, _ = metric_for(scored(3))
|
|
69
|
+
|
|
70
|
+
assert metric.name == "decision"
|
|
71
|
+
assert metric.deterministic is False
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@pytest.mark.parametrize(("level", "expected"), [(1, 0.0), (2, 0.5), (3, 1.0)])
|
|
75
|
+
async def test_the_ordered_criteria_position_maps_onto_the_unit_interval(
|
|
76
|
+
level: int, expected: float
|
|
77
|
+
) -> None:
|
|
78
|
+
metric, _ = metric_for(scored(level))
|
|
79
|
+
|
|
80
|
+
assert (await metric.score(CASE, "answer")).score == expected
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
async def test_the_detail_names_the_level_and_the_number_of_levels() -> None:
|
|
84
|
+
metric, _ = metric_for(scored(2))
|
|
85
|
+
|
|
86
|
+
assert (await metric.score(CASE, "answer")).detail == "level 2 of 3"
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
async def test_the_threshold_marks_a_single_answer_as_passing() -> None:
|
|
90
|
+
at_half, _ = metric_for(scored(2))
|
|
91
|
+
at_bottom, _ = metric_for(scored(1))
|
|
92
|
+
|
|
93
|
+
assert (await at_half.score(CASE, "answer")).passed is True
|
|
94
|
+
assert (await at_bottom.score(CASE, "answer")).passed is False
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
async def test_the_metric_asks_the_decision_it_was_built_from() -> None:
|
|
98
|
+
metric, asker = metric_for(scored(3))
|
|
99
|
+
|
|
100
|
+
await metric.score(CASE, "the prediction")
|
|
101
|
+
|
|
102
|
+
assert asker.calls == [
|
|
103
|
+
{
|
|
104
|
+
"model": "jev-1.14.0",
|
|
105
|
+
"state": "the prediction",
|
|
106
|
+
"questions": {
|
|
107
|
+
"helpfulness": DecisionQuestion(
|
|
108
|
+
type="score",
|
|
109
|
+
instructions="How helpful is this answer",
|
|
110
|
+
criteria=("unusable", "partial", "complete"),
|
|
111
|
+
)
|
|
112
|
+
},
|
|
113
|
+
# A recorded score has to mean the same thing when the eval runs again.
|
|
114
|
+
"reproducible": True,
|
|
115
|
+
}
|
|
116
|
+
]
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
async def test_a_missing_answer_raises_rather_than_scoring_zero() -> None:
|
|
120
|
+
metric, _ = metric_for(None)
|
|
121
|
+
|
|
122
|
+
with pytest.raises(EvalError, match="returned no score"):
|
|
123
|
+
await metric.score(CASE, "answer")
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
async def test_an_answer_without_a_score_raises() -> None:
|
|
127
|
+
metric, _ = metric_for(DecisionAnswer(type="score"))
|
|
128
|
+
|
|
129
|
+
with pytest.raises(EvalError, match="returned no score"):
|
|
130
|
+
await metric.score(CASE, "answer")
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@pytest.mark.parametrize("level", [0, 4])
|
|
134
|
+
async def test_a_level_outside_the_declared_range_raises(level: int) -> None:
|
|
135
|
+
metric, _ = metric_for(scored(level))
|
|
136
|
+
|
|
137
|
+
with pytest.raises(EvalError, match=r"outside the declared 1\.\.3 range"):
|
|
138
|
+
await metric.score(CASE, "answer")
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def test_a_decision_with_fewer_than_two_levels_cannot_be_scored() -> None:
|
|
142
|
+
single = DecisionDefinition(
|
|
143
|
+
id="helpfulness",
|
|
144
|
+
model="jev-1.14.0",
|
|
145
|
+
instructions="How helpful is this answer",
|
|
146
|
+
criteria=("complete",),
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
with pytest.raises(ConfigurationError, match="at least two ordered levels"):
|
|
150
|
+
DecisionScore(single)
|
|
@@ -26,6 +26,7 @@ from aid_runtime.agents import (
|
|
|
26
26
|
DETECTORS,
|
|
27
27
|
GUARDRAIL_ACTIONS,
|
|
28
28
|
GUARDRAIL_STAGES,
|
|
29
|
+
DecisionDefinition,
|
|
29
30
|
GuardrailDefinition,
|
|
30
31
|
GuardrailEngine,
|
|
31
32
|
HumanDecision,
|
|
@@ -34,6 +35,7 @@ from aid_runtime.agents import (
|
|
|
34
35
|
detect,
|
|
35
36
|
)
|
|
36
37
|
from aid_runtime.agents.ir import from_ir
|
|
38
|
+
from aid_runtime.decisions import DecisionAnswer, DecisionResponse
|
|
37
39
|
from aid_runtime.errors import ConfigurationError
|
|
38
40
|
from aid_runtime.retrieval import FakeDeterministicEmbedder, InMemoryVectorStore, Retriever
|
|
39
41
|
|
|
@@ -76,6 +78,9 @@ def definition(
|
|
|
76
78
|
redact: tuple[str, ...] = (),
|
|
77
79
|
tools: tuple[str, ...] = (),
|
|
78
80
|
message: str = "stopped by a guardrail",
|
|
81
|
+
decision: str | None = None,
|
|
82
|
+
on_decision: tuple[tuple[str, str], ...] = (),
|
|
83
|
+
min_confidence: float = 0.0,
|
|
79
84
|
) -> GuardrailDefinition:
|
|
80
85
|
"""Build a guardrail, refusing the one mistake that would silently half-work.
|
|
81
86
|
|
|
@@ -95,6 +100,9 @@ def definition(
|
|
|
95
100
|
message=message,
|
|
96
101
|
fallback_prompt=None,
|
|
97
102
|
log=True,
|
|
103
|
+
decision=decision,
|
|
104
|
+
on_decision=on_decision,
|
|
105
|
+
min_confidence=min_confidence,
|
|
98
106
|
)
|
|
99
107
|
|
|
100
108
|
|
|
@@ -880,3 +888,167 @@ async def test_a_retrieval_escalation_blocks_because_a_retriever_has_no_human()
|
|
|
880
888
|
|
|
881
889
|
with pytest.raises(GuardrailBlockedError, match="no human channel"):
|
|
882
890
|
await retriever.retrieve("contact")
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
# --- the decision seam -----------------------------------------------------------
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
TOPICAL = DecisionDefinition(
|
|
897
|
+
id="topical",
|
|
898
|
+
model="jev-1.14.0",
|
|
899
|
+
instructions="Is this message about our product",
|
|
900
|
+
criteria=("billing", "off_topic", "product"),
|
|
901
|
+
)
|
|
902
|
+
|
|
903
|
+
|
|
904
|
+
class FakeDecisionClient:
|
|
905
|
+
"""Answers the one question with the label the test chose, recording every ask."""
|
|
906
|
+
|
|
907
|
+
def __init__(
|
|
908
|
+
self,
|
|
909
|
+
label: str = "product",
|
|
910
|
+
*,
|
|
911
|
+
confidence: float = 0.9,
|
|
912
|
+
error: Exception | None = None,
|
|
913
|
+
) -> None:
|
|
914
|
+
self.label = label
|
|
915
|
+
self.confidence = confidence
|
|
916
|
+
self.error = error
|
|
917
|
+
self.calls: list[dict[str, Any]] = []
|
|
918
|
+
|
|
919
|
+
async def decide(
|
|
920
|
+
self,
|
|
921
|
+
*,
|
|
922
|
+
model: str,
|
|
923
|
+
state: Any,
|
|
924
|
+
questions: Any,
|
|
925
|
+
reproducible: bool = False,
|
|
926
|
+
) -> DecisionResponse:
|
|
927
|
+
self.calls.append({"model": model, "state": state, "questions": questions})
|
|
928
|
+
if self.error is not None:
|
|
929
|
+
raise self.error
|
|
930
|
+
return DecisionResponse(
|
|
931
|
+
model=model,
|
|
932
|
+
answers={
|
|
933
|
+
"topical": DecisionAnswer(
|
|
934
|
+
type="choice",
|
|
935
|
+
choice=self.label,
|
|
936
|
+
confidence=self.confidence,
|
|
937
|
+
)
|
|
938
|
+
},
|
|
939
|
+
)
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
def decision_guardrail(
|
|
943
|
+
*,
|
|
944
|
+
policies: tuple[str, ...] = (),
|
|
945
|
+
on_decision: tuple[tuple[str, str], ...] = (("product", "allow"), ("off_topic", "block")),
|
|
946
|
+
min_confidence: float = 0.0,
|
|
947
|
+
) -> GuardrailDefinition:
|
|
948
|
+
return definition(
|
|
949
|
+
"g-dec",
|
|
950
|
+
policies=policies,
|
|
951
|
+
decision="topical",
|
|
952
|
+
on_decision=on_decision,
|
|
953
|
+
min_confidence=min_confidence,
|
|
954
|
+
)
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
def decision_engine(client: FakeDecisionClient, *, decisions: Any = None) -> GuardrailEngine:
|
|
958
|
+
return GuardrailEngine(
|
|
959
|
+
decisions={"topical": TOPICAL} if decisions is None else decisions,
|
|
960
|
+
client_factory=lambda: client,
|
|
961
|
+
)
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
async def test_a_decision_label_mapped_to_allow_lets_the_text_through() -> None:
|
|
965
|
+
client = FakeDecisionClient(label="product")
|
|
966
|
+
|
|
967
|
+
verdict = await decision_engine(client).check_input_async((decision_guardrail(),), CLEAN)
|
|
968
|
+
|
|
969
|
+
assert verdict.action is None
|
|
970
|
+
assert verdict.records == ()
|
|
971
|
+
assert verdict.text == CLEAN
|
|
972
|
+
# The text, not a rendered prompt, is the state the classifier sees.
|
|
973
|
+
assert [call["state"] for call in client.calls] == [CLEAN]
|
|
974
|
+
assert client.calls[0]["model"] == "jev-1.14.0"
|
|
975
|
+
|
|
976
|
+
|
|
977
|
+
async def test_a_decision_label_mapped_to_block_stops_the_turn() -> None:
|
|
978
|
+
client = FakeDecisionClient(label="off_topic", confidence=0.81)
|
|
979
|
+
|
|
980
|
+
verdict = await decision_engine(client).check_input_async((decision_guardrail(),), CLEAN)
|
|
981
|
+
|
|
982
|
+
assert verdict.action == "block"
|
|
983
|
+
assert len(verdict.records) == 1
|
|
984
|
+
deciding = verdict.deciding
|
|
985
|
+
assert deciding is not None
|
|
986
|
+
assert (deciding.guardrail, deciding.decision, deciding.label) == (
|
|
987
|
+
"g-dec",
|
|
988
|
+
"topical",
|
|
989
|
+
"off_topic",
|
|
990
|
+
)
|
|
991
|
+
assert deciding.confidence == 0.81
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
async def test_a_decision_label_mapped_to_flag_does_not_stop_the_turn() -> None:
|
|
995
|
+
client = FakeDecisionClient(label="off_topic")
|
|
996
|
+
|
|
997
|
+
verdict = await decision_engine(client).check_input_async(
|
|
998
|
+
(decision_guardrail(on_decision=(("product", "allow"), ("off_topic", "flag"))),),
|
|
999
|
+
CLEAN,
|
|
1000
|
+
)
|
|
1001
|
+
|
|
1002
|
+
assert verdict.action == "flag"
|
|
1003
|
+
assert verdict.text == CLEAN
|
|
1004
|
+
|
|
1005
|
+
|
|
1006
|
+
async def test_a_low_confidence_answer_is_a_blocked_check() -> None:
|
|
1007
|
+
client = FakeDecisionClient(label="product", confidence=0.4)
|
|
1008
|
+
|
|
1009
|
+
with pytest.raises(GuardrailBlockedError, match="below the declared minimum"):
|
|
1010
|
+
await decision_engine(client).check_input_async(
|
|
1011
|
+
(decision_guardrail(min_confidence=0.6),), CLEAN
|
|
1012
|
+
)
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
async def test_an_answer_outside_the_declared_labels_is_a_blocked_check() -> None:
|
|
1016
|
+
client = FakeDecisionClient(label="maybe")
|
|
1017
|
+
|
|
1018
|
+
with pytest.raises(GuardrailBlockedError, match="does not map"):
|
|
1019
|
+
await decision_engine(client).check_input_async((decision_guardrail(),), CLEAN)
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
async def test_a_decision_call_that_raises_is_a_blocked_check() -> None:
|
|
1023
|
+
# Fail closed: a 502 is not "no violation found".
|
|
1024
|
+
client = FakeDecisionClient(error=RuntimeError("502 from Jev"))
|
|
1025
|
+
|
|
1026
|
+
with pytest.raises(GuardrailBlockedError, match="could not answer"):
|
|
1027
|
+
await decision_engine(client).check_input_async((decision_guardrail(),), CLEAN)
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
async def test_an_undeclared_decision_is_a_blocked_check() -> None:
|
|
1031
|
+
with pytest.raises(GuardrailBlockedError, match="no definition"):
|
|
1032
|
+
await decision_engine(FakeDecisionClient(), decisions={}).check_input_async(
|
|
1033
|
+
(decision_guardrail(),), CLEAN
|
|
1034
|
+
)
|
|
1035
|
+
|
|
1036
|
+
|
|
1037
|
+
async def test_a_decision_that_maps_no_label_is_a_blocked_check() -> None:
|
|
1038
|
+
with pytest.raises(GuardrailBlockedError, match="maps no answer label"):
|
|
1039
|
+
await decision_engine(FakeDecisionClient()).check_input_async(
|
|
1040
|
+
(decision_guardrail(on_decision=()),), CLEAN
|
|
1041
|
+
)
|
|
1042
|
+
|
|
1043
|
+
|
|
1044
|
+
async def test_policies_run_before_the_decision_so_a_match_costs_no_tokens() -> None:
|
|
1045
|
+
client = FakeDecisionClient(label="product")
|
|
1046
|
+
|
|
1047
|
+
verdict = await decision_engine(client).check_input_async(
|
|
1048
|
+
(decision_guardrail(policies=("prompt-injection",)),), INJECTION
|
|
1049
|
+
)
|
|
1050
|
+
|
|
1051
|
+
assert verdict.action == "block"
|
|
1052
|
+
assert verdict.deciding is not None
|
|
1053
|
+
assert verdict.deciding.policies == ("prompt-injection",)
|
|
1054
|
+
assert client.calls == []
|
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/agents/chat_model.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/agents/deep_agents.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/providers/__init__.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/providers/anthropic.py
RENAMED
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/providers/bedrock.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/__init__.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/chunking.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/citations.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/connectors.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/embedding.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/metrics.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/pgvector.py
RENAMED
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime/retrieval/retriever.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.89 → aid_runtime_python-0.1.91}/src/aid_runtime_python.egg-info/requires.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|