aid-runtime-python 0.1.97__py3-none-any.whl → 0.1.99__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aid_runtime/__init__.py +19 -1
- aid_runtime/agents/__init__.py +14 -0
- aid_runtime/agents/guardrails.py +115 -49
- aid_runtime/agents/ir.py +107 -1
- aid_runtime/agents/managed.py +324 -0
- aid_runtime/providers/bedrock.py +105 -48
- aid_runtime/retrieval/__init__.py +38 -0
- aid_runtime/retrieval/knowledge_base.py +728 -0
- aid_runtime/retrieval/retriever.py +171 -36
- aid_runtime/retrieval/types.py +173 -1
- {aid_runtime_python-0.1.97.dist-info → aid_runtime_python-0.1.99.dist-info}/METADATA +1 -1
- {aid_runtime_python-0.1.97.dist-info → aid_runtime_python-0.1.99.dist-info}/RECORD +14 -12
- {aid_runtime_python-0.1.97.dist-info → aid_runtime_python-0.1.99.dist-info}/WHEEL +0 -0
- {aid_runtime_python-0.1.97.dist-info → aid_runtime_python-0.1.99.dist-info}/top_level.txt +0 -0
|
@@ -42,9 +42,16 @@ 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
44
|
from aid_runtime.retrieval.hybrid import fuse, lexical_ranking
|
|
45
|
+
from aid_runtime.retrieval.knowledge_base import KnowledgeBaseClient, knowledge_base_client_for
|
|
45
46
|
from aid_runtime.retrieval.rerank import Reranker, reranker_for
|
|
46
47
|
from aid_runtime.retrieval.stores import SearchHit, StoreRecord, VectorStore, create_store
|
|
47
|
-
from aid_runtime.retrieval.types import
|
|
48
|
+
from aid_runtime.retrieval.types import (
|
|
49
|
+
Chunk,
|
|
50
|
+
ManagedRetrieverBinding,
|
|
51
|
+
RefreshMode,
|
|
52
|
+
RetrieverBinding,
|
|
53
|
+
ScoredChunk,
|
|
54
|
+
)
|
|
48
55
|
from aid_runtime.types import TokenUsage
|
|
49
56
|
|
|
50
57
|
__all__ = [
|
|
@@ -89,15 +96,17 @@ class IngestReport(BaseModel):
|
|
|
89
96
|
|
|
90
97
|
|
|
91
98
|
class Retriever:
|
|
92
|
-
"""A bound retriever: its source, its chunker, its embedder, its store
|
|
99
|
+
"""A bound retriever: its source, its chunker, its embedder, its store -- or, for a
|
|
100
|
+
managed knowledge base, the client that asks the engine.
|
|
93
101
|
|
|
94
|
-
Seams are injectable and default from the binding. Tests inject the fake
|
|
95
|
-
|
|
102
|
+
Seams are injectable and default from the binding. Tests inject the fake embedder and
|
|
103
|
+
an in-memory store; generated code passes the real ones. A managed binding has no
|
|
104
|
+
embedder or store to inject, and takes a ``knowledge_base_client`` instead.
|
|
96
105
|
"""
|
|
97
106
|
|
|
98
107
|
def __init__(
|
|
99
108
|
self,
|
|
100
|
-
binding: RetrieverBinding,
|
|
109
|
+
binding: RetrieverBinding | ManagedRetrieverBinding,
|
|
101
110
|
*,
|
|
102
111
|
embedder: Embedder | None = None,
|
|
103
112
|
store: VectorStore | None = None,
|
|
@@ -108,38 +117,65 @@ class Retriever:
|
|
|
108
117
|
base_dir: str | None = None,
|
|
109
118
|
candidate_factor: int = DEFAULT_CANDIDATE_FACTOR,
|
|
110
119
|
guardrails: Sequence[GuardrailDefinition] = (),
|
|
120
|
+
knowledge_base_client: KnowledgeBaseClient | None = None,
|
|
111
121
|
) -> None:
|
|
122
|
+
if candidate_factor < 1:
|
|
123
|
+
raise ConfigurationError("candidate_factor must be at least 1")
|
|
124
|
+
self._binding = binding
|
|
125
|
+
self._reranker = reranker or reranker_for(binding.rerank)
|
|
126
|
+
self._ledger = ledger
|
|
127
|
+
self._budget = budget
|
|
128
|
+
self._candidate_factor = candidate_factor
|
|
129
|
+
self._guardrails = tuple(guardrails)
|
|
130
|
+
self._guardrail_engine = GuardrailEngine()
|
|
131
|
+
self._closed = False
|
|
132
|
+
if isinstance(binding, ManagedRetrieverBinding):
|
|
133
|
+
client = knowledge_base_client or knowledge_base_client_for(binding.knowledge_base)
|
|
134
|
+
if binding.filters and not client.supports_filter_expressions:
|
|
135
|
+
raise FeatureDeferredError(
|
|
136
|
+
f"retriever {binding.retriever_id!r} declares filters and knowledge base "
|
|
137
|
+
f"{binding.knowledge_base.id!r} runs on "
|
|
138
|
+
f"{binding.knowledge_base.engine.value!r}, whose retrieve API does not take "
|
|
139
|
+
"the expression the spec carries; serving the call unfiltered would return "
|
|
140
|
+
"the whole corpus"
|
|
141
|
+
)
|
|
142
|
+
self._knowledge_base_client: KnowledgeBaseClient | None = client
|
|
143
|
+
self._embedder = None
|
|
144
|
+
self._store = None
|
|
145
|
+
self._connector = None
|
|
146
|
+
self._base_dir = None
|
|
147
|
+
return
|
|
112
148
|
if binding.refresh_mode is not RefreshMode.FULL:
|
|
113
149
|
raise FeatureDeferredError(
|
|
114
150
|
f"refresh mode {binding.refresh_mode.value!r} is declared but not implemented: "
|
|
115
151
|
"spec v0.1 reindexes the whole source, and incremental refresh needs change "
|
|
116
152
|
"detection the runtime does not have"
|
|
117
153
|
)
|
|
118
|
-
|
|
119
|
-
raise ConfigurationError("candidate_factor must be at least 1")
|
|
120
|
-
self._binding = binding
|
|
154
|
+
self._knowledge_base_client = None
|
|
121
155
|
self._embedder = embedder or embedder_for(binding.embedding)
|
|
122
156
|
self._store = store or create_store(
|
|
123
157
|
binding.store_kind,
|
|
124
158
|
uri=binding.store_uri,
|
|
125
159
|
dimensions=binding.embedding.dimensions,
|
|
126
160
|
)
|
|
127
|
-
self._reranker = reranker or reranker_for(binding.rerank)
|
|
128
161
|
self._connector = connector
|
|
129
162
|
self._base_dir = base_dir
|
|
130
|
-
self._ledger = ledger
|
|
131
|
-
self._budget = budget
|
|
132
|
-
self._candidate_factor = candidate_factor
|
|
133
|
-
self._guardrails = tuple(guardrails)
|
|
134
|
-
self._guardrail_engine = GuardrailEngine()
|
|
135
|
-
self._closed = False
|
|
136
163
|
|
|
137
164
|
@property
|
|
138
|
-
def binding(self) -> RetrieverBinding:
|
|
165
|
+
def binding(self) -> RetrieverBinding | ManagedRetrieverBinding:
|
|
139
166
|
return self._binding
|
|
140
167
|
|
|
168
|
+
@property
|
|
169
|
+
def is_managed(self) -> bool:
|
|
170
|
+
return self._knowledge_base_client is not None
|
|
171
|
+
|
|
141
172
|
@property
|
|
142
173
|
def store(self) -> VectorStore:
|
|
174
|
+
if self._store is None:
|
|
175
|
+
raise ConfigurationError(
|
|
176
|
+
f"retriever {self._binding.retriever_id!r} reads a managed knowledge base, so "
|
|
177
|
+
"it has no local store: the engine owns the index"
|
|
178
|
+
)
|
|
143
179
|
return self._store
|
|
144
180
|
|
|
145
181
|
async def ingest(self, documents: Sequence[SourceDocument] | None = None) -> IngestReport:
|
|
@@ -148,9 +184,20 @@ class Retriever:
|
|
|
148
184
|
Idempotent by construction: chunk ids are content hashes and the store
|
|
149
185
|
upserts on them, so ingesting the same input twice replaces rather than
|
|
150
186
|
duplicates. Re-ingesting is therefore a legitimate way to refresh a corpus.
|
|
187
|
+
|
|
188
|
+
Refused for a managed retriever. The engine ingests from its own data source, so
|
|
189
|
+
a local ingest would write vectors the retriever never reads -- work that costs
|
|
190
|
+
money and changes nothing, which is the worst kind of silent success.
|
|
151
191
|
"""
|
|
192
|
+
if self._knowledge_base_client is not None:
|
|
193
|
+
raise ConfigurationError(
|
|
194
|
+
f"retriever {self._binding.retriever_id!r} reads a managed knowledge base, "
|
|
195
|
+
"which ingests from its data source inside the engine; there is no local "
|
|
196
|
+
"index to fill, and writing one would be read by nothing"
|
|
197
|
+
)
|
|
152
198
|
source_documents = tuple(documents) if documents is not None else await self._read_source()
|
|
153
|
-
|
|
199
|
+
local = self._local
|
|
200
|
+
chunker = chunker_for(local.chunking)
|
|
154
201
|
chunks = [chunk for document in source_documents for chunk in chunker.chunk(document)]
|
|
155
202
|
|
|
156
203
|
records: list[StoreRecord] = []
|
|
@@ -166,9 +213,9 @@ class Retriever:
|
|
|
166
213
|
for chunk, vector in zip(batch, embedded.vectors, strict=True)
|
|
167
214
|
)
|
|
168
215
|
|
|
169
|
-
written = await self.
|
|
216
|
+
written = await self.store.upsert(records)
|
|
170
217
|
return IngestReport(
|
|
171
|
-
retriever=
|
|
218
|
+
retriever=local.retriever_id,
|
|
172
219
|
documents=len(source_documents),
|
|
173
220
|
chunks=len(chunks),
|
|
174
221
|
written=written,
|
|
@@ -178,23 +225,26 @@ class Retriever:
|
|
|
178
225
|
)
|
|
179
226
|
|
|
180
227
|
async def retrieve(self, query: str, *, top_k: int | None = None) -> RetrievalResult:
|
|
181
|
-
"""Search
|
|
228
|
+
"""Search for ``query`` and return ranked, cited passages."""
|
|
182
229
|
wanted = top_k if top_k is not None else self._binding.top_k
|
|
183
230
|
if wanted < 1:
|
|
184
231
|
raise ConfigurationError("top_k must be at least 1")
|
|
232
|
+
if self._knowledge_base_client is not None:
|
|
233
|
+
return await self._retrieve_managed(query, wanted)
|
|
185
234
|
|
|
186
235
|
batch, _ = await self._embed((query,))
|
|
187
236
|
vector = batch.vectors[0]
|
|
188
237
|
candidates = wanted * self._candidate_factor
|
|
189
|
-
|
|
238
|
+
local = self._local
|
|
239
|
+
hits = await self.store.search(vector, top_k=candidates, metric=local.index.metric)
|
|
190
240
|
ranked = await self._rank(query, hits, candidates)
|
|
191
241
|
items = _to_items(ranked)
|
|
192
242
|
if self._reranker is not None:
|
|
193
|
-
top_n =
|
|
243
|
+
top_n = local.rerank.top_n if local.rerank is not None else wanted
|
|
194
244
|
items = await self._reranker.rerank(query, items, top_n=top_n)
|
|
195
245
|
guarded, records = self._guard_retrieval(items[:wanted])
|
|
196
246
|
return RetrievalResult(
|
|
197
|
-
retriever=
|
|
247
|
+
retriever=local.retriever_id,
|
|
198
248
|
query=query,
|
|
199
249
|
items=guarded,
|
|
200
250
|
guardrails=records,
|
|
@@ -245,14 +295,95 @@ class Retriever:
|
|
|
245
295
|
if self._closed:
|
|
246
296
|
return
|
|
247
297
|
self._closed = True
|
|
248
|
-
|
|
249
|
-
|
|
298
|
+
if self._knowledge_base_client is not None:
|
|
299
|
+
await self._knowledge_base_client.aclose()
|
|
300
|
+
return
|
|
301
|
+
await self.store.aclose()
|
|
302
|
+
embedder = self._embedder
|
|
303
|
+
if embedder is not None:
|
|
304
|
+
await embedder.aclose()
|
|
250
305
|
if self._connector is not None:
|
|
251
306
|
await self._connector.aclose()
|
|
252
307
|
|
|
308
|
+
async def _retrieve_managed(self, query: str, wanted: int) -> RetrievalResult:
|
|
309
|
+
"""Ask the engine, then run the same rerank and guardrail stages as a local search.
|
|
310
|
+
|
|
311
|
+
The chunk mapping goes through :class:`ScoredChunk` and :func:`_to_items` rather
|
|
312
|
+
than straight to ``RetrievedItem`` so citations are derived the one way they are
|
|
313
|
+
derived everywhere: a managed passage then cites identically to a local one, and
|
|
314
|
+
a change to how a citation is built cannot reach only one of the two paths.
|
|
315
|
+
"""
|
|
316
|
+
binding = self._binding
|
|
317
|
+
if not isinstance(binding, ManagedRetrieverBinding):
|
|
318
|
+
raise AssertionError("_retrieve_managed called for a local binding")
|
|
319
|
+
client = self._knowledge_base_client
|
|
320
|
+
if client is None:
|
|
321
|
+
raise AssertionError("_retrieve_managed called without a client")
|
|
322
|
+
|
|
323
|
+
result = await client.retrieve(
|
|
324
|
+
binding.knowledge_base,
|
|
325
|
+
query,
|
|
326
|
+
top_k=wanted,
|
|
327
|
+
filters=binding.filters,
|
|
328
|
+
)
|
|
329
|
+
if result.cost_usd is not None and self._ledger is not None:
|
|
330
|
+
self._ledger.charge(
|
|
331
|
+
self._budget,
|
|
332
|
+
LedgerEntry(
|
|
333
|
+
alias=binding.retriever_id,
|
|
334
|
+
model=binding.knowledge_base.engine.value,
|
|
335
|
+
provider=binding.knowledge_base.engine.value,
|
|
336
|
+
usage=TokenUsage(input_tokens=0, output_tokens=0),
|
|
337
|
+
cost_usd=result.cost_usd,
|
|
338
|
+
),
|
|
339
|
+
)
|
|
340
|
+
|
|
341
|
+
ranked = tuple(
|
|
342
|
+
ScoredChunk(
|
|
343
|
+
chunk=Chunk(
|
|
344
|
+
chunk_id=chunk.chunk_id,
|
|
345
|
+
doc_id=chunk.source_uri or binding.retriever_id,
|
|
346
|
+
uri=chunk.source_uri,
|
|
347
|
+
index=chunk.position,
|
|
348
|
+
start=0,
|
|
349
|
+
end=len(chunk.text),
|
|
350
|
+
text=chunk.text,
|
|
351
|
+
metadata=dict(chunk.metadata),
|
|
352
|
+
),
|
|
353
|
+
score=chunk.score,
|
|
354
|
+
)
|
|
355
|
+
for chunk in result.chunks
|
|
356
|
+
)
|
|
357
|
+
items = _to_items(ranked)
|
|
358
|
+
if self._reranker is not None:
|
|
359
|
+
top_n = binding.rerank.top_n if binding.rerank is not None else wanted
|
|
360
|
+
items = await self._reranker.rerank(query, items, top_n=top_n)
|
|
361
|
+
guarded, records = self._guard_retrieval(items[:wanted])
|
|
362
|
+
return RetrievalResult(
|
|
363
|
+
retriever=binding.retriever_id,
|
|
364
|
+
query=query,
|
|
365
|
+
items=guarded,
|
|
366
|
+
guardrails=records,
|
|
367
|
+
)
|
|
368
|
+
|
|
369
|
+
@property
|
|
370
|
+
def _local(self) -> RetrieverBinding:
|
|
371
|
+
"""The binding, narrowed to the local shape.
|
|
372
|
+
|
|
373
|
+
Reached only from the local code paths. Raising rather than asserting keeps a
|
|
374
|
+
runtime failure explained even under ``python -O``, which strips assertions.
|
|
375
|
+
"""
|
|
376
|
+
binding = self._binding
|
|
377
|
+
if not isinstance(binding, RetrieverBinding):
|
|
378
|
+
raise ConfigurationError(
|
|
379
|
+
f"retriever {binding.retriever_id!r} reads a managed knowledge base, so it "
|
|
380
|
+
"has no local source, chunker, embedder, or store"
|
|
381
|
+
)
|
|
382
|
+
return binding
|
|
383
|
+
|
|
253
384
|
async def _read_source(self) -> tuple[SourceDocument, ...]:
|
|
254
385
|
if self._connector is None:
|
|
255
|
-
self._connector = open_connector(self.
|
|
386
|
+
self._connector = open_connector(self._local.source, base_dir=self._base_dir)
|
|
256
387
|
return await self._connector.documents()
|
|
257
388
|
|
|
258
389
|
async def _embed(self, texts: Sequence[str]) -> tuple[EmbeddingBatch, float]:
|
|
@@ -262,27 +393,31 @@ class Retriever:
|
|
|
262
393
|
operation spent: the ledger's total is cumulative across every call the
|
|
263
394
|
process made, and reporting that as this ingest's cost would double-count.
|
|
264
395
|
"""
|
|
265
|
-
|
|
396
|
+
embedder = self._embedder
|
|
397
|
+
if embedder is None:
|
|
398
|
+
raise AssertionError("_embed called without an embedder")
|
|
399
|
+
local = self._local
|
|
400
|
+
embedded = await embedder.embed(texts, model=local.embedding)
|
|
266
401
|
usage = TokenUsage(
|
|
267
|
-
input_tokens=
|
|
268
|
-
output_tokens=
|
|
402
|
+
input_tokens=embedded.usage.input_tokens,
|
|
403
|
+
output_tokens=embedded.usage.output_tokens,
|
|
269
404
|
)
|
|
270
405
|
# Priced through CostProfile rather than a ModelSpec: a ModelSpec requires a
|
|
271
406
|
# context window and an output ceiling, and fabricating those to reuse a
|
|
272
407
|
# pricing helper would put invented numbers in a generated artifact.
|
|
273
|
-
cost =
|
|
408
|
+
cost = local.embedding.cost.cost_usd(usage)
|
|
274
409
|
if self._ledger is not None:
|
|
275
410
|
self._ledger.charge(
|
|
276
411
|
self._budget,
|
|
277
412
|
LedgerEntry(
|
|
278
|
-
alias=
|
|
279
|
-
model=
|
|
280
|
-
provider=
|
|
413
|
+
alias=local.embed_model,
|
|
414
|
+
model=local.embedding.model_id,
|
|
415
|
+
provider=local.embedding.provider,
|
|
281
416
|
usage=usage,
|
|
282
417
|
cost_usd=cost,
|
|
283
418
|
),
|
|
284
419
|
)
|
|
285
|
-
return
|
|
420
|
+
return embedded, cost
|
|
286
421
|
|
|
287
422
|
async def _rank(
|
|
288
423
|
self,
|
|
@@ -296,10 +431,10 @@ class Retriever:
|
|
|
296
431
|
meaning of the flag -- not "use a different metric", but "do not consult the
|
|
297
432
|
lexical ranking at all".
|
|
298
433
|
"""
|
|
299
|
-
if not self.
|
|
434
|
+
if not self._local.index.hybrid:
|
|
300
435
|
return tuple(ScoredChunk(chunk=hit.record.chunk, score=hit.score) for hit in hits)
|
|
301
436
|
|
|
302
|
-
corpus = await self.
|
|
437
|
+
corpus = await self.store.corpus()
|
|
303
438
|
by_id: dict[str, StoreRecord] = {record.chunk.chunk_id: record for record in corpus}
|
|
304
439
|
for hit in hits:
|
|
305
440
|
by_id.setdefault(hit.record.chunk.chunk_id, hit.record)
|
aid_runtime/retrieval/types.py
CHANGED
|
@@ -39,6 +39,14 @@ __all__ = [
|
|
|
39
39
|
"EmbeddingCostProfile",
|
|
40
40
|
"EmbeddingSpec",
|
|
41
41
|
"IndexSpec",
|
|
42
|
+
"KnowledgeBaseEngine",
|
|
43
|
+
"KnowledgeBaseSpec",
|
|
44
|
+
"KnowledgeChunkingSpec",
|
|
45
|
+
"KnowledgeDataSourceKind",
|
|
46
|
+
"KnowledgeDataSourceSpec",
|
|
47
|
+
"KnowledgeVectorStoreSpec",
|
|
48
|
+
"ManagedRetrieverBinding",
|
|
49
|
+
"ManagedRetrieverSpec",
|
|
42
50
|
"PinnedEmbedding",
|
|
43
51
|
"RefreshMode",
|
|
44
52
|
"RefreshSpec",
|
|
@@ -53,6 +61,7 @@ __all__ = [
|
|
|
53
61
|
"StoreKind",
|
|
54
62
|
"StoreRecord",
|
|
55
63
|
"VectorStoreSpec",
|
|
64
|
+
"binding_from_managed_spec",
|
|
56
65
|
"binding_from_spec",
|
|
57
66
|
"chunk_id_for",
|
|
58
67
|
"pinned_embedding",
|
|
@@ -166,10 +175,16 @@ class RefreshSpec(BaseModel):
|
|
|
166
175
|
|
|
167
176
|
|
|
168
177
|
class RetrieverSpec(BaseModel):
|
|
169
|
-
"""One entry of the IR's ``retrievers`` map.
|
|
178
|
+
"""One entry of the IR's ``retrievers`` map that reads a local corpus.
|
|
179
|
+
|
|
180
|
+
``kind`` is the union's discriminator. It defaults to ``local`` because every
|
|
181
|
+
retriever was local before managed knowledge bases existed, so an IR written by
|
|
182
|
+
an older generator still validates.
|
|
183
|
+
"""
|
|
170
184
|
|
|
171
185
|
model_config = _IR_RECORD
|
|
172
186
|
|
|
187
|
+
kind: Literal["local"] = "local"
|
|
173
188
|
id: str = Field(min_length=1)
|
|
174
189
|
source: SourceSpec
|
|
175
190
|
chunking: ChunkingSpec
|
|
@@ -286,6 +301,119 @@ class RetrieverBinding(BaseModel):
|
|
|
286
301
|
traits: tuple[str, ...] = ()
|
|
287
302
|
|
|
288
303
|
|
|
304
|
+
class KnowledgeDataSourceKind(StrEnum):
|
|
305
|
+
"""``knowledgeBase.dataSource.kind``."""
|
|
306
|
+
|
|
307
|
+
S3 = "s3"
|
|
308
|
+
GCS = "gcs"
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
class KnowledgeBaseEngine(StrEnum):
|
|
312
|
+
"""``knowledgeBase.engine`` -- the managed service that owns the corpus."""
|
|
313
|
+
|
|
314
|
+
AWS_BEDROCK = "aws-bedrock"
|
|
315
|
+
VERTEX_RAG = "vertex-rag"
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
class KnowledgeDataSourceSpec(BaseModel):
|
|
319
|
+
"""``knowledgeBase.dataSource`` -- the bucket the corpus is uploaded to."""
|
|
320
|
+
|
|
321
|
+
model_config = _IR_RECORD
|
|
322
|
+
|
|
323
|
+
kind: KnowledgeDataSourceKind
|
|
324
|
+
uri: str = Field(min_length=1)
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
class KnowledgeChunkingSpec(BaseModel):
|
|
328
|
+
"""``knowledgeBase.chunking``.
|
|
329
|
+
|
|
330
|
+
Deliberately not :class:`ChunkingSpec`: a managed base has no ``keepMetadata``,
|
|
331
|
+
because the engine decides which metadata reaches a filter and the spec
|
|
332
|
+
declares that through ``filterable`` instead.
|
|
333
|
+
"""
|
|
334
|
+
|
|
335
|
+
model_config = _IR_RECORD
|
|
336
|
+
|
|
337
|
+
strategy: ChunkingStrategy
|
|
338
|
+
size: int = Field(gt=0)
|
|
339
|
+
overlap: int = Field(ge=0)
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
class KnowledgeVectorStoreSpec(BaseModel):
|
|
343
|
+
"""``knowledgeBase.vectorStore``.
|
|
344
|
+
|
|
345
|
+
``provisioned`` means the emitted infrastructure creates the store; ``existing``
|
|
346
|
+
means the operator already pays for one and ``arn`` names it. Neither is a local
|
|
347
|
+
:class:`VectorStoreSpec`: nothing in this runtime opens it.
|
|
348
|
+
"""
|
|
349
|
+
|
|
350
|
+
model_config = _IR_RECORD
|
|
351
|
+
|
|
352
|
+
kind: Literal["provisioned", "existing"]
|
|
353
|
+
arn: str | None = None
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
class KnowledgeBaseSpec(BaseModel):
|
|
357
|
+
"""One entry of the IR's ``knowledgeBases`` map.
|
|
358
|
+
|
|
359
|
+
The engine owns chunking, embedding and the index, so this record is what the
|
|
360
|
+
runtime needs in order to *ask* it a question and to say which deployment the
|
|
361
|
+
question belongs to -- never what it needs to answer one itself.
|
|
362
|
+
"""
|
|
363
|
+
|
|
364
|
+
model_config = _IR_RECORD
|
|
365
|
+
|
|
366
|
+
id: str = Field(min_length=1)
|
|
367
|
+
engine: KnowledgeBaseEngine
|
|
368
|
+
data_source: KnowledgeDataSourceSpec = Field(alias="dataSource")
|
|
369
|
+
chunking: KnowledgeChunkingSpec
|
|
370
|
+
embed_model: str = Field(alias="embedModel")
|
|
371
|
+
filterable: tuple[str, ...]
|
|
372
|
+
vector_store: KnowledgeVectorStoreSpec = Field(alias="vectorStore")
|
|
373
|
+
#: USD per retrieve call, inlined from the cost catalog by the generator. ``None``
|
|
374
|
+
#: means the catalog has no verified rate -- the cost is then unknown, not zero,
|
|
375
|
+
#: and the ledger records nothing rather than inventing a number.
|
|
376
|
+
retrieve_cost_per_query_usd: float | None = Field(default=None, alias="retrieveCostPerQueryUsd")
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
class ManagedRetrieverSpec(BaseModel):
|
|
380
|
+
"""One ``retrievers`` entry that reads a knowledge base.
|
|
381
|
+
|
|
382
|
+
The counterpart of :class:`RetrieverSpec`, and disjoint from it: a managed
|
|
383
|
+
retriever has no source, chunking, embedModel, store or index, because all five
|
|
384
|
+
belong to the engine.
|
|
385
|
+
"""
|
|
386
|
+
|
|
387
|
+
model_config = _IR_RECORD
|
|
388
|
+
|
|
389
|
+
kind: Literal["managed"] = "managed"
|
|
390
|
+
id: str = Field(min_length=1)
|
|
391
|
+
knowledge_base: str = Field(alias="knowledgeBase")
|
|
392
|
+
top_k: int = Field(gt=0, alias="topK")
|
|
393
|
+
rerank: RerankSpec | None = None
|
|
394
|
+
filters: tuple[str, ...] | None = None
|
|
395
|
+
traits: tuple[str, ...]
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
class ManagedRetrieverBinding(BaseModel):
|
|
399
|
+
"""A managed retriever as generated code binds it.
|
|
400
|
+
|
|
401
|
+
Kept apart from :class:`RetrieverBinding` rather than folded into it with
|
|
402
|
+
optional fields: the two have no field in common beyond ``top_k``, and a merged
|
|
403
|
+
record would let a local retriever arrive with no store -- the exact state the
|
|
404
|
+
union exists to make unrepresentable.
|
|
405
|
+
"""
|
|
406
|
+
|
|
407
|
+
model_config = _RUNTIME_RECORD
|
|
408
|
+
|
|
409
|
+
retriever_id: str = Field(min_length=1)
|
|
410
|
+
knowledge_base: KnowledgeBaseSpec
|
|
411
|
+
top_k: int = Field(gt=0)
|
|
412
|
+
rerank: RerankSpec | None = None
|
|
413
|
+
filters: tuple[str, ...] = ()
|
|
414
|
+
traits: tuple[str, ...] = ()
|
|
415
|
+
|
|
416
|
+
|
|
289
417
|
class Chunk(BaseModel):
|
|
290
418
|
"""One indexed passage.
|
|
291
419
|
|
|
@@ -431,3 +559,47 @@ def binding_from_spec(
|
|
|
431
559
|
refresh_mode=spec.refresh.mode,
|
|
432
560
|
traits=spec.traits,
|
|
433
561
|
)
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
def binding_from_managed_spec(
|
|
565
|
+
spec: ManagedRetrieverSpec,
|
|
566
|
+
*,
|
|
567
|
+
knowledge_base: KnowledgeBaseSpec,
|
|
568
|
+
) -> ManagedRetrieverBinding:
|
|
569
|
+
"""Bind one managed retriever entry to the knowledge base it names.
|
|
570
|
+
|
|
571
|
+
The same posture as :func:`binding_from_spec`: the reference is checked rather
|
|
572
|
+
than trusted, because generated code is reachable without the plan validator
|
|
573
|
+
having run. What is checked here is only what this side can see -- the alias, the
|
|
574
|
+
reranker the runtime actually has, and the filters it cannot apply. Whether the
|
|
575
|
+
engine has finished ingesting is a question only the engine can answer, and
|
|
576
|
+
:class:`~aid_runtime.retrieval.knowledge_base.KnowledgeBaseClient` asks it.
|
|
577
|
+
"""
|
|
578
|
+
if spec.knowledge_base != knowledge_base.id:
|
|
579
|
+
raise ConfigurationError(
|
|
580
|
+
f"retriever {spec.id!r} names knowledge base {spec.knowledge_base!r} but "
|
|
581
|
+
f"knowledge base {knowledge_base.id!r} was supplied"
|
|
582
|
+
)
|
|
583
|
+
if spec.rerank is not None and "lexical" not in spec.rerank.model.casefold():
|
|
584
|
+
raise FeatureDeferredError(
|
|
585
|
+
f"retriever {spec.id!r} reranks with {spec.rerank.model!r}, and the runtime's "
|
|
586
|
+
"only reranker is lexical: it matches the declared name, so a name that does "
|
|
587
|
+
"not say so would be served by a reranker it never named"
|
|
588
|
+
)
|
|
589
|
+
if spec.filters:
|
|
590
|
+
raise FeatureDeferredError(
|
|
591
|
+
f"retriever {spec.id!r} declares {len(spec.filters)} filter "
|
|
592
|
+
f"{'expression' if len(spec.filters) == 1 else 'expressions'} on a managed "
|
|
593
|
+
"knowledge base, and this runtime does not translate them: Bedrock takes a "
|
|
594
|
+
"structured filter document and Vertex takes its own expression language, so "
|
|
595
|
+
"the only alternatives are to drop the filter -- which would retrieve the "
|
|
596
|
+
"whole corpus -- or to invent one engine's document from another's string"
|
|
597
|
+
)
|
|
598
|
+
return ManagedRetrieverBinding(
|
|
599
|
+
retriever_id=spec.id,
|
|
600
|
+
knowledge_base=knowledge_base,
|
|
601
|
+
top_k=spec.top_k,
|
|
602
|
+
rerank=spec.rerank,
|
|
603
|
+
filters=spec.filters or (),
|
|
604
|
+
traits=spec.traits,
|
|
605
|
+
)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: aid-runtime-python
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.99
|
|
4
4
|
Summary: The AID Framework Python runtime: model gateway with pinning and cost budgets, prompt registry, tool executor, OTel GenAI tracing, plain-async pipeline, and an eval harness that gates on statistical regression against a stored baseline.
|
|
5
5
|
Author: The AID Framework authors
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
aid_runtime/__init__.py,sha256=
|
|
1
|
+
aid_runtime/__init__.py,sha256=0-aPDvpX-WmLAvG5hdMpP-UHZ9xKN0-JNdlZKUQl2pw,13237
|
|
2
2
|
aid_runtime/cache.py,sha256=jhw_EBd9dfWEUzYeibOvIkVBqqiLUjqF5WKEPz9GkhE,6836
|
|
3
3
|
aid_runtime/concurrency.py,sha256=z0tFQHw35ttFLKfazGBjCMPOLQOnUotE_OPpvR1piH4,2829
|
|
4
4
|
aid_runtime/control.py,sha256=y6wPn0fqYJv0nVSsjzpzfiaq1Y7zBoMxCpdrEuqkfvg,17529
|
|
@@ -15,13 +15,14 @@ aid_runtime/structured.py,sha256=Uga-VoGJN56lUtr-M3nmUjAKGF32cAAk-p7WceUgPzg,506
|
|
|
15
15
|
aid_runtime/tools.py,sha256=7LeaPI5aJioNe3vexUv94tvIRpOwxj1Ujt723qvO3UI,9906
|
|
16
16
|
aid_runtime/tracing.py,sha256=q7v1GQ0FaYOq4uWfGMhloXwQb3fXjxE0ixi6Yij9OJQ,9624
|
|
17
17
|
aid_runtime/types.py,sha256=cVYgmacDIhStQT5dX_X67-q57sPBBaWC5nybtGVqVsw,10389
|
|
18
|
-
aid_runtime/agents/__init__.py,sha256=
|
|
18
|
+
aid_runtime/agents/__init__.py,sha256=hna-peyTFCRuF9TM9_lKy5b4O12cJnsQPrMcDncSCcg,2865
|
|
19
19
|
aid_runtime/agents/chat_model.py,sha256=fZdCmSpoECuBVGZE1s8NmT2erT33RhDuLh3yJM6kAJE,14808
|
|
20
20
|
aid_runtime/agents/deep_agents.py,sha256=ZM6ZSc92arCvtsYGsIGdF-NryqN_tvXtRqbdlHaGpeE,26948
|
|
21
21
|
aid_runtime/agents/engine.py,sha256=RQUV49u7MLDmirnFv8YsRGdIutzUGsGaLA6Y2JT5wj0,4129
|
|
22
|
-
aid_runtime/agents/guardrails.py,sha256=
|
|
23
|
-
aid_runtime/agents/ir.py,sha256=
|
|
22
|
+
aid_runtime/agents/guardrails.py,sha256=FC8RX8YUeUHgw9Kh24NavkSIkaXayPeVpFri-OZGi0Y,30200
|
|
23
|
+
aid_runtime/agents/ir.py,sha256=pDWIia4t7rzRqJO3ZBsOrl24_uBTWT03NFKOEPV1JrU,29355
|
|
24
24
|
aid_runtime/agents/loop.py,sha256=T-QnC9BQ9K3RyZfHdAuzWiwMXBpQjDLGdHrLgf2fnFM,52608
|
|
25
|
+
aid_runtime/agents/managed.py,sha256=8AEv8HV_EOFZIzjBE92_dB_xsNffn2k0P3ccd-TM5oQ,12143
|
|
25
26
|
aid_runtime/agents/memory.py,sha256=6_D6k2bzAiR2euNvzr6HB28wsUUq-R-WTQc12gkB8ao,9156
|
|
26
27
|
aid_runtime/agents/policy.py,sha256=ael0EW_hWfLu95rbqFMrR4WyqAPtgiSNvNw97xnthdw,6122
|
|
27
28
|
aid_runtime/agents/state.py,sha256=4itT7BWv2JKSeWpRrDyxLguOOVnz9gGH_qgRdCbWCaw,6086
|
|
@@ -34,26 +35,27 @@ aid_runtime/evals/runner.py,sha256=Lkl-kAVxegPO40pCBUcprRBKvfL6bjYzVf__xe6Z-Mc,7
|
|
|
34
35
|
aid_runtime/providers/__init__.py,sha256=MQ-vU_QeeUgfqMlwLi0layJBhmvbZat9gIkDFcFl7Pg,1827
|
|
35
36
|
aid_runtime/providers/anthropic.py,sha256=xh_fJyujnFRcknEHFRxA5M-izeR2hJtit-865RrISOQ,15853
|
|
36
37
|
aid_runtime/providers/base.py,sha256=1VYTZ_V9FR6f6pS0MB3LFeu-Zzv4toQvm3CwAQNbeT0,1851
|
|
37
|
-
aid_runtime/providers/bedrock.py,sha256=
|
|
38
|
+
aid_runtime/providers/bedrock.py,sha256=bcipfWld7JdFHWslgDapbmg2UwevxmxBt8g88zFhyT0,22796
|
|
38
39
|
aid_runtime/providers/fake.py,sha256=oAcpaDOUIp7lmKGEEWG5ktagstg2dvoHR-9xaUeCDLo,9526
|
|
39
40
|
aid_runtime/providers/gemini.py,sha256=gM4Kvgz6lnFbHgYYhyVCBTFTcGAKfiNuj7lS1XSldnU,3975
|
|
40
41
|
aid_runtime/providers/gemini_wire.py,sha256=ZwIiYuAVihszE0c90-uAbSn6_hdMa2Md4asySIYeQTw,13032
|
|
41
42
|
aid_runtime/providers/http.py,sha256=c5DLmX1FqtAUO01MqDf0m82x8zHD480LyL-QtTrxnn4,13168
|
|
42
43
|
aid_runtime/providers/vertex.py,sha256=zWBDXeP6kBo89KkCVbAkeiqDm6_4ujBdL0Ebk9W-vPM,7421
|
|
43
|
-
aid_runtime/retrieval/__init__.py,sha256=
|
|
44
|
+
aid_runtime/retrieval/__init__.py,sha256=ChLOCf2ubV5En8HvguB1s5j0WDcYWgRMNnxQNZx_ltU,5681
|
|
44
45
|
aid_runtime/retrieval/chunking.py,sha256=GMJvY1GoIua-FkpCH1S5qNmUrWSaSbl6sBJPCrSDAg4,8296
|
|
45
46
|
aid_runtime/retrieval/citations.py,sha256=UL9fsxLTzzO0n8tEW3X2O5erJ0hrk08NYMkZ9Pk8Jdg,4287
|
|
46
47
|
aid_runtime/retrieval/connectors.py,sha256=JNEwq6S8zKX1GdlWe4Gn2Rdon87DWXlG_WNQfZTlN34,4744
|
|
47
48
|
aid_runtime/retrieval/embedding.py,sha256=nKyk70iPDlSq_-W7cbm1cHy3rbH7LHSq8ulcssjGMlY,18203
|
|
48
49
|
aid_runtime/retrieval/gate.py,sha256=3avH1Gwp7iv7mMOskmNbk70Yq85tXwJrKVrQAKKI4Js,4840
|
|
49
50
|
aid_runtime/retrieval/hybrid.py,sha256=-GIBeI3njT5uaEvSeq2t151Zlv2yOPHOPje4TJLNVyU,5804
|
|
51
|
+
aid_runtime/retrieval/knowledge_base.py,sha256=xNFTdIdIMvS7Ij9y4niXGrgxAhtsmWe2kOVyR_j9AU4,28360
|
|
50
52
|
aid_runtime/retrieval/metrics.py,sha256=LN36BIXzpbhkG9grzE0q5wncgXt_Y85-b5jGJ038-3Y,7904
|
|
51
53
|
aid_runtime/retrieval/pgvector.py,sha256=kdE6Gdk9JhR6-QxN353CmuNYMLyr5eBvnQl4krLzLew,10040
|
|
52
54
|
aid_runtime/retrieval/rerank.py,sha256=L4ICBi9YHDzhnKVtImKh5Wzre1HDzS8at-AKDkXBJMc,5114
|
|
53
|
-
aid_runtime/retrieval/retriever.py,sha256=
|
|
55
|
+
aid_runtime/retrieval/retriever.py,sha256=5I8BocFaJ292cqh9-Gz2NRB4OwBHa-pitAB6--5ARBE,19890
|
|
54
56
|
aid_runtime/retrieval/stores.py,sha256=VEi68AaMHcQjdAbWOHflp3jCl1_1db71rIzZjTouLlo,5857
|
|
55
|
-
aid_runtime/retrieval/types.py,sha256=
|
|
56
|
-
aid_runtime_python-0.1.
|
|
57
|
-
aid_runtime_python-0.1.
|
|
58
|
-
aid_runtime_python-0.1.
|
|
59
|
-
aid_runtime_python-0.1.
|
|
57
|
+
aid_runtime/retrieval/types.py,sha256=bke4W2q8ecGFvF2tj8y1tfFq8EtGb6iiPVbT-l02a98,19860
|
|
58
|
+
aid_runtime_python-0.1.99.dist-info/METADATA,sha256=IZZ9XHk2OQGOY4cL3ASQ4boQgATK4yqifh4JDBPi2uo,17005
|
|
59
|
+
aid_runtime_python-0.1.99.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
60
|
+
aid_runtime_python-0.1.99.dist-info/top_level.txt,sha256=QvODgNVRDDMmLcTnoIQ4jYnhqf-N4b9sHWEmaUUxp44,12
|
|
61
|
+
aid_runtime_python-0.1.99.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|