RAG Chunk Eligibility

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:

OrderReturnWhen
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 }, where departments is a Set of strings (a set in Python)
  • publishedRevision: a Map from document ID to the currently published revision number (a dict in 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.

ChunkDifference from C1Expected
C1(none)null
C2department procurement"department"
C3tenant beta"tenant"
C4revision 2"revision"
C5state staged"unpublished"
C6deleted"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 publishedRevision has no published revision, so its chunks are excluded with "revision".
  • tokens does 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