RAG Chunk Eligibility
Part of AI Engineering: Diagnose a missing document
RAG Chunk Eligibility
A retrieval-augmented assistant serves private policy documents for several organizations. Before a retrieved chunk can be sent to the model, the application checks that this user is allowed to use it. Write the check.
Implement exclusionReason(chunk, access, publishedRevision). It returns the first reason the chunk is excluded, or null when the chunk is eligible. Check in this order:
| Order | Return | When |
|---|---|---|
| 1 | "tenant" | chunk.tenant differs from access.tenant |
| 2 | "deleted" | chunk.deleted is true |
| 3 | "unpublished" | chunk.state is anything other than "published" |
| 4 | "revision" | publishedRevision does not map chunk.document to exactly chunk.revision |
| 5 | "department" | access.departments does not contain chunk.department |
Inputs:
chunk:{ id, tenant, document, revision, department, state, deleted, tokens }access:{ tenant, departments }, wheredepartmentsis aSetof strings (asetin Python)publishedRevision: aMapfrom document ID to the currently published revision number (adictin Python)
Fixtures
The user belongs to tenant alpha and the support department. The returns document has revision 3 published. Each chunk differs from C1 in one field.
| Chunk | Difference from C1 | Expected |
|---|---|---|
| C1 | (none) | null |
| C2 | department procurement | "department" |
| C3 | tenant beta | "tenant" |
| C4 | revision 2 | "revision" |
| C5 | state staged | "unpublished" |
| C6 | deleted | "deleted" |
Notes
- A revision that is newer than the published one is excluded too: only the published revision is current.
- A document that is missing from
publishedRevisionhas no published revision, so its chunks are excluded with"revision". tokensdoes not affect eligibility. A later stage, context packing, uses it.
In Python, implement exclusion_reason(chunk, access, published_revision) with dicts, and return None for an eligible chunk.
MEDIUM
AI Engineering
RAG (Retrieval-Augmented Generation)
Authorization
Multi-Tenant
Intermediate
0 views
Solution
Hints
Hint 1
Hint 2
Premium
Hint 3
Premium
This section is available for CodeSnatch Premium members only.
