aid-runtime-python 0.1.125__tar.gz → 0.1.127__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.125 → aid_runtime_python-0.1.127}/PKG-INFO +1 -1
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/pyproject.toml +1 -1
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/__init__.py +1 -1
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/__init__.py +14 -0
- aid_runtime_python-0.1.127/src/aid_runtime/retrieval/filters.py +165 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/retriever.py +39 -3
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/types.py +15 -8
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime_python.egg-info/PKG-INFO +1 -1
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime_python.egg-info/SOURCES.txt +2 -0
- aid_runtime_python-0.1.127/tests/test_retrieval_filters.py +261 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_retrieval_types.py +40 -6
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/README.md +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/setup.cfg +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/__init__.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/chat_model.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/deep_agents.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/engine.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/guardrails.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/ir.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/loop.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/managed.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/memory.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/policy.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/state.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/cache.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/concurrency.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/control.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/cost.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/decisions.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/errors.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/evals/__init__.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/evals/dataset.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/evals/gate.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/evals/metrics.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/evals/predicate.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/evals/runner.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/gateway.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/local_trace.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/pipeline.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/prompts.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/__init__.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/anthropic.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/base.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/bedrock.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/fake.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/gemini.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/gemini_wire.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/http.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/vertex.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/py.typed +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/chunking.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/citations.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/connectors.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/embedding.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/gate.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/hybrid.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/knowledge_base.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/metrics.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/pgvector.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/rerank.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/stores.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/steps.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/structured.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/tools.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/tracing.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/types.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime_python.egg-info/dependency_links.txt +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime_python.egg-info/requires.txt +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime_python.egg-info/top_level.txt +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_agents_budget.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_agents_deep_agents.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_agents_hitl.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_agents_loop.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_agents_memory.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_anthropic_provider.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_bedrock_provider.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_cache.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_concurrency.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_control.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_cost.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_dataset.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_decisions.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_evals.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_evals_assert_gate.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_evals_decision.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_evals_gate.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_evals_predicate.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_gateway.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_gemini_provider.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_guardrails.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_guardrails_managed.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_guardrails_model_armor.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_http_provider.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_local_trace.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_package.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_pipeline.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_prompts.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_retrieval_chunking.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_retrieval_citations.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_retrieval_connectors.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_retrieval_embedding.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_retrieval_gate.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_retrieval_hybrid.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_retrieval_knowledge_base.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_retrieval_metrics.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_retrieval_rerank.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_retrieval_stores.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_steps.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_streaming.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_structured.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_tool_permissions.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_tools.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_tracing.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_types.py +0 -0
- {aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/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.127
|
|
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.127"
|
|
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"
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/__init__.py
RENAMED
|
@@ -55,6 +55,14 @@ from aid_runtime.retrieval.embedding import (
|
|
|
55
55
|
embedder_for,
|
|
56
56
|
estimate_tokens,
|
|
57
57
|
)
|
|
58
|
+
from aid_runtime.retrieval.filters import (
|
|
59
|
+
FILTER_OPERATORS,
|
|
60
|
+
Filter,
|
|
61
|
+
compile_filters,
|
|
62
|
+
matches,
|
|
63
|
+
matches_all,
|
|
64
|
+
parse_filter,
|
|
65
|
+
)
|
|
58
66
|
from aid_runtime.retrieval.gate import RetrievalGate
|
|
59
67
|
from aid_runtime.retrieval.hybrid import (
|
|
60
68
|
RankedId,
|
|
@@ -130,6 +138,7 @@ from aid_runtime.retrieval.types import (
|
|
|
130
138
|
|
|
131
139
|
__all__ = [
|
|
132
140
|
"DEFAULT_CANDIDATE_FACTOR",
|
|
141
|
+
"FILTER_OPERATORS",
|
|
133
142
|
"FIXTURE_ENV",
|
|
134
143
|
"BedrockEmbedder",
|
|
135
144
|
"BedrockKnowledgeBaseClient",
|
|
@@ -148,6 +157,7 @@ __all__ = [
|
|
|
148
157
|
"EmbeddingSpec",
|
|
149
158
|
"FakeDeterministicEmbedder",
|
|
150
159
|
"FileConnector",
|
|
160
|
+
"Filter",
|
|
151
161
|
"FixedChunker",
|
|
152
162
|
"FixtureKnowledgeBaseClient",
|
|
153
163
|
"GeminiEmbedder",
|
|
@@ -205,6 +215,7 @@ __all__ = [
|
|
|
205
215
|
"chunk_id_for",
|
|
206
216
|
"chunker_for",
|
|
207
217
|
"cite",
|
|
218
|
+
"compile_filters",
|
|
208
219
|
"create_store",
|
|
209
220
|
"embedder_for",
|
|
210
221
|
"estimate_tokens",
|
|
@@ -213,7 +224,10 @@ __all__ = [
|
|
|
213
224
|
"knowledge_base_env_id",
|
|
214
225
|
"lexical_ranking",
|
|
215
226
|
"lexical_score",
|
|
227
|
+
"matches",
|
|
228
|
+
"matches_all",
|
|
216
229
|
"open_connector",
|
|
230
|
+
"parse_filter",
|
|
217
231
|
"pinned_embedding",
|
|
218
232
|
"reranker_for",
|
|
219
233
|
"resolve_uri",
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"""Filter expressions over a chunk's identity and metadata.
|
|
2
|
+
|
|
3
|
+
The IR carries ``filters`` as strings, because the two hosted knowledge bases take
|
|
4
|
+
strings and a structured shape would have to be translated twice. This module pins
|
|
5
|
+
the grammar the *local* pipeline reads, and pins it narrowly: five comparison
|
|
6
|
+
operators, ANDed, no boolean algebra, no regular expressions.
|
|
7
|
+
|
|
8
|
+
The restriction is deliberate. A filter is a security boundary -- a tenant or
|
|
9
|
+
classification selector -- so it must be impossible to express one that silently
|
|
10
|
+
matches more than it says. Regex would make ``*=` a denial-of-service surface and
|
|
11
|
+
would not port to either hosted engine.
|
|
12
|
+
|
|
13
|
+
A mature library was considered and rejected. JMESPath (``jmespath``) is the closest
|
|
14
|
+
fit and exists in both Python and JavaScript, but its ``!=`` on an absent field is
|
|
15
|
+
true, which is the fail-open behaviour this module exists to prevent, and its grammar
|
|
16
|
+
is far larger than the five operators documented here. Wrapping it would mean writing
|
|
17
|
+
the validator and the absence rule by hand anyway, so the parser stays here where
|
|
18
|
+
both runtimes can be held to the same table.
|
|
19
|
+
|
|
20
|
+
Grammar::
|
|
21
|
+
|
|
22
|
+
filter := field op value
|
|
23
|
+
op := "==" | "!=" | "^=" (prefix) | "$=" (suffix) | "*=" (contains)
|
|
24
|
+
field := "doc_id" | "uri" | metadata key
|
|
25
|
+
value := bare token, or "..." / '...' quoted
|
|
26
|
+
|
|
27
|
+
Every entry in the list is ANDed. Evaluation is per chunk: a field the chunk does not
|
|
28
|
+
carry matches nothing, *including* under ``!=`` -- absence is not a value, and
|
|
29
|
+
treating it as one would widen a selector instead of narrowing it.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
from __future__ import annotations
|
|
33
|
+
|
|
34
|
+
import re
|
|
35
|
+
from collections.abc import Sequence
|
|
36
|
+
from dataclasses import dataclass
|
|
37
|
+
from typing import Protocol
|
|
38
|
+
|
|
39
|
+
from aid_runtime.errors import ConfigurationError
|
|
40
|
+
|
|
41
|
+
__all__ = [
|
|
42
|
+
"FILTER_OPERATORS",
|
|
43
|
+
"Filter",
|
|
44
|
+
"compile_filters",
|
|
45
|
+
"matches",
|
|
46
|
+
"matches_all",
|
|
47
|
+
"metadata_fields",
|
|
48
|
+
"parse_filter",
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
#: The operators the grammar admits, in the order a diagnostic lists them.
|
|
52
|
+
FILTER_OPERATORS: tuple[str, ...] = ("==", "!=", "^=", "$=", "*=")
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class ChunkLike(Protocol):
|
|
56
|
+
"""The slice of a chunk a filter observes.
|
|
57
|
+
|
|
58
|
+
Retrieval's ``Chunk`` satisfies this structurally. Naming the shape here rather
|
|
59
|
+
than importing it keeps this module free of a cycle back into ``retrieval.types``,
|
|
60
|
+
which imports :func:`metadata_fields` at module level.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
doc_id: str
|
|
64
|
+
uri: str
|
|
65
|
+
metadata: dict[str, str]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
_FIELD = r"[A-Za-z0-9_.\-]+"
|
|
69
|
+
_OPERATOR = r"==|!=|\^=|\$=|\*="
|
|
70
|
+
_EXPRESSION = re.compile(rf"^\s*({_FIELD})\s*({_OPERATOR})\s*(.*)", re.DOTALL)
|
|
71
|
+
|
|
72
|
+
#: Fields resolved from the chunk itself, never from ``chunk.metadata``.
|
|
73
|
+
_RESERVED_FIELDS = frozenset({"doc_id", "uri"})
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass(frozen=True, slots=True)
|
|
77
|
+
class Filter:
|
|
78
|
+
"""One parsed comparison. ``value`` is already unquoted."""
|
|
79
|
+
|
|
80
|
+
field: str
|
|
81
|
+
operator: str
|
|
82
|
+
value: str
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def parse_filter(expression: str) -> Filter:
|
|
86
|
+
"""Parse one expression, or explain precisely what is wrong with it.
|
|
87
|
+
|
|
88
|
+
An unsupported operator is reported against the operator itself rather than as a
|
|
89
|
+
generic "invalid expression": the five spellings are short enough to list, and a
|
|
90
|
+
user who wrote ``=`` instead of ``==`` should be told so.
|
|
91
|
+
"""
|
|
92
|
+
if not isinstance(expression, str):
|
|
93
|
+
raise ConfigurationError(
|
|
94
|
+
f"a filter expression must be a string, not {type(expression).__name__}"
|
|
95
|
+
)
|
|
96
|
+
match = _EXPRESSION.match(expression)
|
|
97
|
+
if match is None:
|
|
98
|
+
raise ConfigurationError(
|
|
99
|
+
f"filter expression {expression!r} is not '<field> <op> <value>'; the operators are "
|
|
100
|
+
+ ", ".join(FILTER_OPERATORS)
|
|
101
|
+
)
|
|
102
|
+
field, operator, raw = match.groups()
|
|
103
|
+
value = _unquote(raw.strip(), expression)
|
|
104
|
+
if not value:
|
|
105
|
+
raise ConfigurationError(
|
|
106
|
+
f"filter expression {expression!r} names field {field!r} with an empty value"
|
|
107
|
+
)
|
|
108
|
+
return Filter(field=field, operator=operator, value=value)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _unquote(raw: str, expression: str) -> str:
|
|
112
|
+
"""Strip one matching pair of quotes, and nothing else."""
|
|
113
|
+
if len(raw) >= 2 and raw[0] == raw[-1] and raw[0] in {'"', "'"}:
|
|
114
|
+
return raw[1:-1]
|
|
115
|
+
if raw[:1] in {'"', "'"}:
|
|
116
|
+
raise ConfigurationError(
|
|
117
|
+
f"filter expression {expression!r} starts a quoted value but does not close it"
|
|
118
|
+
)
|
|
119
|
+
return raw
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def compile_filters(expressions: Sequence[str]) -> tuple[Filter, ...]:
|
|
123
|
+
"""Parse every expression once, at a point where a mistake is still local."""
|
|
124
|
+
return tuple(parse_filter(expression) for expression in expressions)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def metadata_fields(expressions: Sequence[str]) -> tuple[str, ...]:
|
|
128
|
+
"""The metadata keys ``expressions`` read, in declaration order, without repeats.
|
|
129
|
+
|
|
130
|
+
A chunk only carries the metadata ``chunking.keepMetadata`` names, so a filter on
|
|
131
|
+
a key that is not kept would match nothing. The binder unions this into
|
|
132
|
+
``keepMetadata`` rather than asking a user to remember the same key twice.
|
|
133
|
+
"""
|
|
134
|
+
fields = (parsed.field for parsed in compile_filters(expressions))
|
|
135
|
+
return tuple(dict.fromkeys(field for field in fields if field not in _RESERVED_FIELDS))
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def matches(chunk: ChunkLike, condition: Filter) -> bool:
|
|
139
|
+
"""Whether ``chunk`` satisfies one comparison.
|
|
140
|
+
|
|
141
|
+
A missing field is a non-match for every operator. ``!=`` therefore means "this
|
|
142
|
+
field is present and differs", not "this field is absent or differs".
|
|
143
|
+
"""
|
|
144
|
+
if condition.field == "doc_id":
|
|
145
|
+
actual: str | None = chunk.doc_id
|
|
146
|
+
elif condition.field == "uri":
|
|
147
|
+
actual = chunk.uri
|
|
148
|
+
else:
|
|
149
|
+
actual = chunk.metadata.get(condition.field)
|
|
150
|
+
if actual is None:
|
|
151
|
+
return False
|
|
152
|
+
if condition.operator == "==":
|
|
153
|
+
return actual == condition.value
|
|
154
|
+
if condition.operator == "!=":
|
|
155
|
+
return actual != condition.value
|
|
156
|
+
if condition.operator == "^=":
|
|
157
|
+
return actual.startswith(condition.value)
|
|
158
|
+
if condition.operator == "$=":
|
|
159
|
+
return actual.endswith(condition.value)
|
|
160
|
+
return condition.value in actual
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def matches_all(chunk: ChunkLike, filters: Sequence[Filter]) -> bool:
|
|
164
|
+
"""Whether ``chunk`` satisfies every filter. An empty list admits everything."""
|
|
165
|
+
return all(matches(chunk, condition) for condition in filters)
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/retriever.py
RENAMED
|
@@ -41,6 +41,7 @@ from aid_runtime.retrieval.chunking import chunker_for
|
|
|
41
41
|
from aid_runtime.retrieval.citations import RetrievalResult, RetrievedItem, cite
|
|
42
42
|
from aid_runtime.retrieval.connectors import Connector, SourceDocument, open_connector
|
|
43
43
|
from aid_runtime.retrieval.embedding import Embedder, EmbeddingBatch, embedder_for
|
|
44
|
+
from aid_runtime.retrieval.filters import Filter, compile_filters, matches_all
|
|
44
45
|
from aid_runtime.retrieval.hybrid import fuse, lexical_ranking
|
|
45
46
|
from aid_runtime.retrieval.knowledge_base import KnowledgeBaseClient, knowledge_base_client_for
|
|
46
47
|
from aid_runtime.retrieval.rerank import Reranker, reranker_for
|
|
@@ -154,6 +155,9 @@ class Retriever:
|
|
|
154
155
|
self._base_dir = None
|
|
155
156
|
return
|
|
156
157
|
self._knowledge_base_client = None
|
|
158
|
+
# Parsed once here so a malformed selector fails at construction, and so the
|
|
159
|
+
# per-query path never re-parses. A syntax error names the grammar.
|
|
160
|
+
self._filters = compile_filters(binding.filters or ())
|
|
157
161
|
self._embedder = embedder or embedder_for(binding.embedding)
|
|
158
162
|
self._store = store or create_store(
|
|
159
163
|
binding.store_kind,
|
|
@@ -260,10 +264,11 @@ class Retriever:
|
|
|
260
264
|
|
|
261
265
|
batch, _ = await self._embed((query,))
|
|
262
266
|
vector = batch.vectors[0]
|
|
263
|
-
candidates = wanted * self._candidate_factor
|
|
264
267
|
local = self._local
|
|
265
|
-
|
|
266
|
-
|
|
268
|
+
filters = self._filters
|
|
269
|
+
hits = await self._search_vector(vector, wanted, local, filters)
|
|
270
|
+
candidates = max(len(hits), wanted * self._candidate_factor)
|
|
271
|
+
ranked = await self._rank(query, hits, candidates, filters)
|
|
267
272
|
items = _to_items(ranked)
|
|
268
273
|
if self._reranker is not None:
|
|
269
274
|
top_n = local.rerank.top_n if local.rerank is not None else wanted
|
|
@@ -445,22 +450,53 @@ class Retriever:
|
|
|
445
450
|
)
|
|
446
451
|
return embedded, cost
|
|
447
452
|
|
|
453
|
+
async def _search_vector(
|
|
454
|
+
self,
|
|
455
|
+
vector: Sequence[float],
|
|
456
|
+
wanted: int,
|
|
457
|
+
local: RetrieverBinding,
|
|
458
|
+
filters: Sequence[Filter],
|
|
459
|
+
) -> tuple[SearchHit, ...]:
|
|
460
|
+
"""Vector search, widened until ``wanted`` passages survive the filters.
|
|
461
|
+
|
|
462
|
+
A store can only apply ``top_k``, so a selective filter has to be satisfied by
|
|
463
|
+
looking deeper. The limit doubles until enough passages match or the store
|
|
464
|
+
returns fewer rows than were asked for -- the latter is the only signal that
|
|
465
|
+
the corpus is exhausted. With no filters this is one search at the historical
|
|
466
|
+
candidate width, so the unfiltered path is byte-for-byte unchanged.
|
|
467
|
+
"""
|
|
468
|
+
limit = max(wanted * self._candidate_factor, 1)
|
|
469
|
+
while True:
|
|
470
|
+
hits = await self.store.search(vector, top_k=limit, metric=local.index.metric)
|
|
471
|
+
kept = tuple(hit for hit in hits if matches_all(hit.record.chunk, filters))
|
|
472
|
+
if len(kept) >= wanted or len(hits) < limit:
|
|
473
|
+
return kept
|
|
474
|
+
limit *= 2
|
|
475
|
+
|
|
448
476
|
async def _rank(
|
|
449
477
|
self,
|
|
450
478
|
query: str,
|
|
451
479
|
hits: Sequence[SearchHit],
|
|
452
480
|
candidates: int,
|
|
481
|
+
filters: Sequence[Filter] = (),
|
|
453
482
|
) -> tuple[ScoredChunk, ...]:
|
|
454
483
|
"""The candidate ranking: vector only, or fused with a lexical ranking.
|
|
455
484
|
|
|
456
485
|
Fusion is skipped when ``index.hybrid`` is false, and that is the whole
|
|
457
486
|
meaning of the flag -- not "use a different metric", but "do not consult the
|
|
458
487
|
lexical ranking at all".
|
|
488
|
+
|
|
489
|
+
The lexical half reads the stored corpus rather than the vector hits, so it is
|
|
490
|
+
filtered too. Ranking the whole corpus and filtering afterwards would let a
|
|
491
|
+
passage outside the selector re-enter through BM25, which is the one way a
|
|
492
|
+
filter could silently match more than it says.
|
|
459
493
|
"""
|
|
460
494
|
if not self._local.index.hybrid:
|
|
461
495
|
return tuple(ScoredChunk(chunk=hit.record.chunk, score=hit.score) for hit in hits)
|
|
462
496
|
|
|
463
497
|
corpus = await self.store.corpus()
|
|
498
|
+
if filters:
|
|
499
|
+
corpus = tuple(record for record in corpus if matches_all(record.chunk, filters))
|
|
464
500
|
by_id: dict[str, StoreRecord] = {record.chunk.chunk_id: record for record in corpus}
|
|
465
501
|
for hit in hits:
|
|
466
502
|
by_id.setdefault(hit.record.chunk.chunk_id, hit.record)
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/types.py
RENAMED
|
@@ -29,6 +29,7 @@ from typing import Any, Literal
|
|
|
29
29
|
from pydantic import BaseModel, ConfigDict, Field
|
|
30
30
|
|
|
31
31
|
from aid_runtime.errors import ConfigurationError, FeatureDeferredError
|
|
32
|
+
from aid_runtime.retrieval.filters import metadata_fields
|
|
32
33
|
from aid_runtime.types import CostProfile
|
|
33
34
|
|
|
34
35
|
__all__ = [
|
|
@@ -302,6 +303,7 @@ class RetrieverBinding(BaseModel):
|
|
|
302
303
|
index: IndexSpec
|
|
303
304
|
top_k: int = Field(gt=0)
|
|
304
305
|
rerank: RerankSpec | None = None
|
|
306
|
+
filters: tuple[str, ...] = ()
|
|
305
307
|
refresh_mode: RefreshMode = RefreshMode.FULL
|
|
306
308
|
traits: tuple[str, ...] = ()
|
|
307
309
|
|
|
@@ -525,6 +527,10 @@ def binding_from_spec(
|
|
|
525
527
|
``store`` names a *stores entry id*. Both are checked here rather than
|
|
526
528
|
trusted, because a mistyped reference in generated code would otherwise
|
|
527
529
|
surface as an empty or mis-scored corpus far from its cause.
|
|
530
|
+
|
|
531
|
+
A declared filter also widens ``chunking.keepMetadata``: a chunk carries only the
|
|
532
|
+
metadata that field names, so filtering on a key it omits would match nothing. The
|
|
533
|
+
union is done here so the spec does not have to state the same key twice.
|
|
528
534
|
"""
|
|
529
535
|
if spec.embed_model != embedding.id:
|
|
530
536
|
raise ConfigurationError(
|
|
@@ -535,13 +541,13 @@ def binding_from_spec(
|
|
|
535
541
|
raise ConfigurationError(
|
|
536
542
|
f"retriever {spec.id!r} names store {spec.store!r} but store {store.id!r} was supplied"
|
|
537
543
|
)
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
)
|
|
544
|
+
filters = tuple(spec.filters or ())
|
|
545
|
+
chunking = spec.chunking
|
|
546
|
+
if filters:
|
|
547
|
+
# Parse now: a malformed selector is a configuration fault, and generated
|
|
548
|
+
# code is reachable without the spec validator having run.
|
|
549
|
+
keep = tuple(dict.fromkeys((*chunking.keep_metadata, *metadata_fields(filters))))
|
|
550
|
+
chunking = chunking.model_copy(update={"keep_metadata": keep})
|
|
545
551
|
pinned = pinned_embedding(embedding, alias=spec.embed_model, dimensions=dimensions)
|
|
546
552
|
if store.dimensions is not None and store.dimensions != pinned.dimensions:
|
|
547
553
|
raise ConfigurationError(
|
|
@@ -552,7 +558,7 @@ def binding_from_spec(
|
|
|
552
558
|
return RetrieverBinding(
|
|
553
559
|
retriever_id=spec.id,
|
|
554
560
|
source=spec.source,
|
|
555
|
-
chunking=
|
|
561
|
+
chunking=chunking,
|
|
556
562
|
embed_model=spec.embed_model,
|
|
557
563
|
embedding=pinned,
|
|
558
564
|
store=spec.store,
|
|
@@ -561,6 +567,7 @@ def binding_from_spec(
|
|
|
561
567
|
index=spec.index,
|
|
562
568
|
top_k=spec.top_k,
|
|
563
569
|
rerank=spec.rerank,
|
|
570
|
+
filters=filters,
|
|
564
571
|
refresh_mode=spec.refresh.mode,
|
|
565
572
|
traits=spec.traits,
|
|
566
573
|
)
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/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.127
|
|
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
|
|
@@ -48,6 +48,7 @@ src/aid_runtime/retrieval/chunking.py
|
|
|
48
48
|
src/aid_runtime/retrieval/citations.py
|
|
49
49
|
src/aid_runtime/retrieval/connectors.py
|
|
50
50
|
src/aid_runtime/retrieval/embedding.py
|
|
51
|
+
src/aid_runtime/retrieval/filters.py
|
|
51
52
|
src/aid_runtime/retrieval/gate.py
|
|
52
53
|
src/aid_runtime/retrieval/hybrid.py
|
|
53
54
|
src/aid_runtime/retrieval/knowledge_base.py
|
|
@@ -94,6 +95,7 @@ tests/test_retrieval_chunking.py
|
|
|
94
95
|
tests/test_retrieval_citations.py
|
|
95
96
|
tests/test_retrieval_connectors.py
|
|
96
97
|
tests/test_retrieval_embedding.py
|
|
98
|
+
tests/test_retrieval_filters.py
|
|
97
99
|
tests/test_retrieval_gate.py
|
|
98
100
|
tests/test_retrieval_hybrid.py
|
|
99
101
|
tests/test_retrieval_knowledge_base.py
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
"""Filter expressions: the grammar, its evaluation, and its place in retrieval.
|
|
2
|
+
|
|
3
|
+
The grammar is pinned deliberately narrow, so the accept/reject table is the
|
|
4
|
+
specification. Two behaviours are worth stating where a reader will look for them:
|
|
5
|
+
|
|
6
|
+
* a field the chunk does not carry matches **nothing**, including under ``!=``, because
|
|
7
|
+
absence is not a value and treating it as one widens a selector;
|
|
8
|
+
* filtering happens in the retriever, not in the store, and the hybrid lexical half
|
|
9
|
+
reads the same filtered corpus -- otherwise a filtered-out passage could re-enter
|
|
10
|
+
through BM25.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from collections.abc import Sequence
|
|
16
|
+
|
|
17
|
+
import pytest
|
|
18
|
+
|
|
19
|
+
from aid_runtime import ConfigurationError
|
|
20
|
+
from aid_runtime.retrieval import (
|
|
21
|
+
FILTER_OPERATORS,
|
|
22
|
+
FakeDeterministicEmbedder,
|
|
23
|
+
Filter,
|
|
24
|
+
InMemoryVectorStore,
|
|
25
|
+
Retriever,
|
|
26
|
+
SearchHit,
|
|
27
|
+
compile_filters,
|
|
28
|
+
matches,
|
|
29
|
+
matches_all,
|
|
30
|
+
parse_filter,
|
|
31
|
+
)
|
|
32
|
+
from aid_runtime.retrieval.types import DistanceMetric
|
|
33
|
+
from retrieval_support import binding, chunk, document
|
|
34
|
+
|
|
35
|
+
# --------------------------------------------------------------------------- grammar
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def test_every_declared_operator_round_trips() -> None:
|
|
39
|
+
assert FILTER_OPERATORS == ("==", "!=", "^=", "$=", "*=")
|
|
40
|
+
for operator in FILTER_OPERATORS:
|
|
41
|
+
parsed = parse_filter(f"tenant {operator} acme")
|
|
42
|
+
assert parsed == Filter(field="tenant", operator=operator, value="acme")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_a_value_may_be_double_or_single_quoted() -> None:
|
|
46
|
+
assert parse_filter('path == "a/b c"').value == "a/b c"
|
|
47
|
+
assert parse_filter("path == 'a/b c'").value == "a/b c"
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_quotes_are_only_stripped_when_they_pair() -> None:
|
|
51
|
+
# A value that merely starts with a quote has no closing quote, which is a typo
|
|
52
|
+
# rather than a value containing one.
|
|
53
|
+
with pytest.raises(ConfigurationError, match="does not close it"):
|
|
54
|
+
parse_filter('path == "unterminated')
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@pytest.mark.parametrize(
|
|
58
|
+
"expression",
|
|
59
|
+
[
|
|
60
|
+
"tenant", # no operator
|
|
61
|
+
"tenant = acme", # single '=' is not an operator
|
|
62
|
+
"tenant == ", # empty value
|
|
63
|
+
"tenant ==",
|
|
64
|
+
"== acme", # no field
|
|
65
|
+
"tenant ~= acme", # unsupported operator
|
|
66
|
+
],
|
|
67
|
+
)
|
|
68
|
+
def test_malformed_expressions_are_refused_with_the_grammar(expression: str) -> None:
|
|
69
|
+
with pytest.raises(ConfigurationError):
|
|
70
|
+
parse_filter(expression)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_a_malformed_expression_lists_the_operators() -> None:
|
|
74
|
+
with pytest.raises(ConfigurationError) as raised:
|
|
75
|
+
parse_filter("tenant = acme")
|
|
76
|
+
for operator in FILTER_OPERATORS:
|
|
77
|
+
assert operator in str(raised.value)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def test_metadata_keys_may_contain_dots_and_hyphens() -> None:
|
|
81
|
+
assert parse_filter("classification.level-2 == internal").field == "classification.level-2"
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
# ------------------------------------------------------------------------ evaluation
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def test_equality_is_exact_and_case_sensitive() -> None:
|
|
88
|
+
compiled = compile_filters(['tenant == "Acme"'])
|
|
89
|
+
assert matches_all(chunk("a", "text", metadata={"tenant": "Acme"}), compiled)
|
|
90
|
+
assert not matches_all(chunk("a", "text", metadata={"tenant": "acme"}), compiled)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def test_the_five_operators_differ_on_the_same_value() -> None:
|
|
94
|
+
subject = chunk("a", "text", metadata={"path": "docs/guide.md"})
|
|
95
|
+
assert matches(subject, parse_filter("path == docs/guide.md"))
|
|
96
|
+
assert matches(subject, parse_filter("path != docs/other.md"))
|
|
97
|
+
assert matches(subject, parse_filter("path ^= docs/"))
|
|
98
|
+
assert matches(subject, parse_filter("path $= .md"))
|
|
99
|
+
assert matches(subject, parse_filter("path *= guide"))
|
|
100
|
+
assert not matches(subject, parse_filter("path ^= guide"))
|
|
101
|
+
assert not matches(subject, parse_filter("path $= guide"))
|
|
102
|
+
assert not matches(subject, parse_filter("path *= other"))
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def test_a_missing_field_never_matches_even_under_not_equal() -> None:
|
|
106
|
+
# This is the security-relevant rule: `tenant != other` on a chunk with no tenant
|
|
107
|
+
# must not admit the chunk, because that would turn a selector into a bypass.
|
|
108
|
+
subject = chunk("a", "text", metadata={})
|
|
109
|
+
for operator in FILTER_OPERATORS:
|
|
110
|
+
assert not matches(subject, parse_filter(f"tenant {operator} acme"))
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def test_doc_id_and_uri_are_resolved_from_the_chunk_not_the_metadata() -> None:
|
|
114
|
+
subject = chunk("doc-1", "text", uri="s3://bucket/key.md", metadata={"doc_id": "shadowed"})
|
|
115
|
+
assert matches(subject, parse_filter("doc_id == doc-1"))
|
|
116
|
+
assert matches(subject, parse_filter("uri == s3://bucket/key.md"))
|
|
117
|
+
assert not matches(subject, parse_filter("doc_id == shadowed"))
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def test_metadata_may_not_shadow_the_reserved_fields() -> None:
|
|
121
|
+
# A document can carry any metadata, so pinning precedence keeps a corpus from
|
|
122
|
+
# choosing which identity the filter sees.
|
|
123
|
+
subject = chunk("doc-1", "text", uri="real.md", metadata={"uri": "forged.md"})
|
|
124
|
+
assert matches(subject, parse_filter("uri == real.md"))
|
|
125
|
+
assert not matches(subject, parse_filter("uri == forged.md"))
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def test_entries_are_anded() -> None:
|
|
129
|
+
subject = chunk("a", "text", metadata={"tenant": "acme", "tier": "gold"})
|
|
130
|
+
assert matches_all(subject, compile_filters(["tenant == acme", "tier == gold"]))
|
|
131
|
+
assert not matches_all(subject, compile_filters(["tenant == acme", "tier == silver"]))
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def test_an_empty_filter_list_admits_everything() -> None:
|
|
135
|
+
assert matches_all(chunk("a", "text"), ())
|
|
136
|
+
assert compile_filters([]) == ()
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
# --------------------------------------------------------------------- in retrieval
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def make_retriever(
|
|
143
|
+
*,
|
|
144
|
+
filters: Sequence[str] | None = None,
|
|
145
|
+
hybrid: bool = False,
|
|
146
|
+
top_k: int = 3,
|
|
147
|
+
store: InMemoryVectorStore | None = None,
|
|
148
|
+
) -> Retriever:
|
|
149
|
+
return Retriever(
|
|
150
|
+
binding(filters=filters, hybrid=hybrid, top_k=top_k, size=2000),
|
|
151
|
+
embedder=FakeDeterministicEmbedder(),
|
|
152
|
+
store=store or InMemoryVectorStore(),
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
class OrderedStore(InMemoryVectorStore):
|
|
157
|
+
"""Returns insertion order, so a test can place a filtered-out chunk first."""
|
|
158
|
+
|
|
159
|
+
def __init__(self) -> None:
|
|
160
|
+
super().__init__()
|
|
161
|
+
self.limits: list[int] = []
|
|
162
|
+
|
|
163
|
+
async def search(
|
|
164
|
+
self, vector: Sequence[float], *, top_k: int, metric: DistanceMetric
|
|
165
|
+
) -> tuple[SearchHit, ...]:
|
|
166
|
+
self.limits.append(top_k)
|
|
167
|
+
hits = [SearchHit(record=record, score=1.0) for record in self._records.values()]
|
|
168
|
+
return tuple(hits[:top_k])
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
async def test_a_filter_narrows_the_result() -> None:
|
|
172
|
+
retriever = make_retriever(filters=['tenant == "acme"'])
|
|
173
|
+
await retriever.ingest(
|
|
174
|
+
[
|
|
175
|
+
document("ours.md", "alpha", metadata={"tenant": "acme"}),
|
|
176
|
+
document("theirs.md", "alpha alpha", metadata={"tenant": "other"}),
|
|
177
|
+
]
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
result = await retriever.retrieve("alpha")
|
|
181
|
+
|
|
182
|
+
assert [item.chunk.doc_id for item in result.items] == ["ours.md"]
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
async def test_a_filter_matching_nothing_yields_an_empty_result() -> None:
|
|
186
|
+
retriever = make_retriever(filters=['tenant == "absent"'])
|
|
187
|
+
await retriever.ingest([document("a.md", "alpha", metadata={"tenant": "acme"})])
|
|
188
|
+
|
|
189
|
+
result = await retriever.retrieve("alpha")
|
|
190
|
+
|
|
191
|
+
assert result.items == ()
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
async def test_a_selective_filter_widens_the_search_until_it_can_fill_top_k() -> None:
|
|
195
|
+
store = OrderedStore()
|
|
196
|
+
retriever = make_retriever(filters=['tenant == "acme"'], top_k=1, store=store)
|
|
197
|
+
await retriever.ingest(
|
|
198
|
+
[
|
|
199
|
+
document(f"other-{index}.md", f"text {index}", metadata={"tenant": "other"})
|
|
200
|
+
for index in range(4)
|
|
201
|
+
]
|
|
202
|
+
+ [document("ours.md", "text ours", metadata={"tenant": "acme"})]
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
result = await retriever.retrieve("text")
|
|
206
|
+
|
|
207
|
+
# 1 * candidate_factor (4) misses, then doubles past the five-record corpus.
|
|
208
|
+
assert store.limits == [4, 8]
|
|
209
|
+
assert [item.chunk.doc_id for item in result.items] == ["ours.md"]
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
async def test_hybrid_lexical_ranking_cannot_reintroduce_a_filtered_out_chunk() -> None:
|
|
213
|
+
# The lexical half reads the whole corpus, which is exactly where a leak would
|
|
214
|
+
# come from: the disallowed document matches the query far more strongly.
|
|
215
|
+
retriever = make_retriever(filters=['tenant == "acme"'], hybrid=True, top_k=2)
|
|
216
|
+
await retriever.ingest(
|
|
217
|
+
[
|
|
218
|
+
document("ours.md", "alpha", metadata={"tenant": "acme"}),
|
|
219
|
+
document("theirs.md", "alpha alpha alpha alpha", metadata={"tenant": "other"}),
|
|
220
|
+
]
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
result = await retriever.retrieve("alpha")
|
|
224
|
+
|
|
225
|
+
assert [item.chunk.doc_id for item in result.items] == ["ours.md"]
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
async def test_an_unfiltered_retriever_search_does_not_widen() -> None:
|
|
229
|
+
store = OrderedStore()
|
|
230
|
+
retriever = make_retriever(top_k=1, store=store)
|
|
231
|
+
await retriever.ingest([document(f"{index}.md", f"text {index}") for index in range(4)])
|
|
232
|
+
|
|
233
|
+
await retriever.retrieve("text")
|
|
234
|
+
|
|
235
|
+
assert store.limits == [4]
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def test_a_malformed_filter_fails_at_construction() -> None:
|
|
239
|
+
# Configuration faults should surface where the configuration is read, not on the
|
|
240
|
+
# first query after a successful deploy.
|
|
241
|
+
with pytest.raises(ConfigurationError, match="operators are"):
|
|
242
|
+
make_retriever(filters=["tenant = acme"])
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
async def test_filters_do_not_change_the_result_without_matches_among_them() -> None:
|
|
246
|
+
# A filter every chunk satisfies must be invisible: same docs, same order, same
|
|
247
|
+
# scores as the unfiltered run.
|
|
248
|
+
texts = [("a.md", "alpha beta"), ("b.md", "gamma delta"), ("c.md", "epsilon")]
|
|
249
|
+
plain = make_retriever(top_k=3)
|
|
250
|
+
filtered = make_retriever(filters=['tier == "gold"'], top_k=3)
|
|
251
|
+
for retriever in (plain, filtered):
|
|
252
|
+
await retriever.ingest(
|
|
253
|
+
[document(name, text, metadata={"tier": "gold"}) for name, text in texts]
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
unfiltered = await plain.retrieve("alpha")
|
|
257
|
+
narrowed = await filtered.retrieve("alpha")
|
|
258
|
+
|
|
259
|
+
assert [(item.chunk.doc_id, item.score) for item in narrowed.items] == [
|
|
260
|
+
(item.chunk.doc_id, item.score) for item in unfiltered.items
|
|
261
|
+
]
|
|
@@ -11,7 +11,7 @@ from __future__ import annotations
|
|
|
11
11
|
|
|
12
12
|
import pytest
|
|
13
13
|
|
|
14
|
-
from aid_runtime import Capability, ConfigurationError,
|
|
14
|
+
from aid_runtime import Capability, ConfigurationError, TokenUsage
|
|
15
15
|
from aid_runtime.retrieval import (
|
|
16
16
|
ChunkingStrategy,
|
|
17
17
|
DistanceMetric,
|
|
@@ -197,11 +197,45 @@ def test_a_store_with_no_declared_width_is_accepted() -> None:
|
|
|
197
197
|
assert pinned_binding.embedding.dimensions == DIMENSIONS
|
|
198
198
|
|
|
199
199
|
|
|
200
|
-
def
|
|
201
|
-
#
|
|
202
|
-
#
|
|
203
|
-
#
|
|
204
|
-
|
|
200
|
+
def test_a_binding_carries_filters_and_keeps_the_metadata_they_read() -> None:
|
|
201
|
+
# A chunk carries only the metadata `keepMetadata` names, so a filter on a key
|
|
202
|
+
# that is not kept would silently match nothing. Declaring the filter must be
|
|
203
|
+
# enough -- the same key should not have to be listed twice.
|
|
204
|
+
bound = binding_from_spec(
|
|
205
|
+
retriever_spec(filters=('tenant == "acme"', "tier ^= gold")),
|
|
206
|
+
embedding=embedding_spec(),
|
|
207
|
+
store=store_spec(),
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
assert bound.filters == ('tenant == "acme"', "tier ^= gold")
|
|
211
|
+
assert bound.chunking.keep_metadata == ("tenant", "tier")
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def test_filter_metadata_is_added_without_disturbing_declared_keys() -> None:
|
|
215
|
+
bound = binding_from_spec(
|
|
216
|
+
retriever_spec(filters=("tenant == acme",), keep_metadata=("source", "tenant")),
|
|
217
|
+
embedding=embedding_spec(),
|
|
218
|
+
store=store_spec(),
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
assert bound.chunking.keep_metadata == ("source", "tenant")
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def test_a_reserved_filter_field_is_not_added_to_keep_metadata() -> None:
|
|
225
|
+
bound = binding_from_spec(
|
|
226
|
+
retriever_spec(filters=("doc_id == a.md",)),
|
|
227
|
+
embedding=embedding_spec(),
|
|
228
|
+
store=store_spec(),
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
assert bound.chunking.keep_metadata == ()
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def test_a_malformed_filter_is_refused_when_the_retriever_binds() -> None:
|
|
235
|
+
# Generated code is reachable without the spec validator having run, so the
|
|
236
|
+
# runtime refuses a grammar it cannot evaluate rather than ignoring it, which
|
|
237
|
+
# would return passages the caller asked to exclude.
|
|
238
|
+
with pytest.raises(ConfigurationError, match="operators are"):
|
|
205
239
|
binding_from_spec(
|
|
206
240
|
retriever_spec(filters=("lang:en",)),
|
|
207
241
|
embedding=embedding_spec(),
|
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/__init__.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/chat_model.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/deep_agents.py
RENAMED
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/agents/guardrails.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
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/evals/predicate.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/__init__.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/anthropic.py
RENAMED
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/bedrock.py
RENAMED
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/gemini.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/gemini_wire.py
RENAMED
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/providers/vertex.py
RENAMED
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/chunking.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/citations.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/connectors.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/embedding.py
RENAMED
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/hybrid.py
RENAMED
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/metrics.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/pgvector.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/rerank.py
RENAMED
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/src/aid_runtime/retrieval/stores.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
|
|
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.125 → aid_runtime_python-0.1.127}/tests/test_guardrails_model_armor.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
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_retrieval_connectors.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{aid_runtime_python-0.1.125 → aid_runtime_python-0.1.127}/tests/test_retrieval_knowledge_base.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
|