← All projects

Case study 06 / AI Engineering · Software Engineering

Retrieval with an authorization boundary.

An internal knowledge assistant with department-scoped retrieval, hybrid search, streaming responses, and evaluation.

  • FastAPI
  • LangChain
  • ChromaDB
  • Redis

The problem

A shared knowledge assistant must answer useful questions without retrieving documents the current user is not allowed to read.

What I built

Built a RAG application with JWT authentication, Redis token revocation, department-scoped hybrid retrieval (BM25 plus vector search), cross-encoder reranking, streaming responses, and 191 automated tests.

How it works

Requests pass through per-user rate limits, authentication, and input guardrails that block injection and off-topic queries, with Llama Prompt Guard 2 as an optional second check. Both halves of retrieval are restricted to the user’s departments before reranking. Redis stores conversation state and revoked tokens, and output handling redacts PII.

  1. Authenticated request
  2. Scoped hybrid retrieval
  3. Rerank & generate
  4. Output handling
A simplified flow; implementation details are linked below.

Engineering decisions

Authorize before generation

Department access is enforced in both the BM25 index and the vector query, rather than relying on instructions to the model or a hidden UI control. Chat history is private to each user.

Name the tradeoff

A facts-first prompt stopped the model from stating links the documents don’t make. With the same model and judge, faithfulness rose from 0.8916 to 0.9490 while answer relevancy fell from 0.9048 to 0.8831.

Score refusals separately

RAGAS scores a correct “I don’t know” as zero, so unanswerable questions are scored by refusal accuracy instead. The README reports how much that choice raises the averages.

Results & evidence

RAGAS scores by question type

  • Faithfulness
  • Answer relevancy
Answerable questions only, n per type in the label. gpt-oss-120b with the facts-first prompt, judged by deepseek-chat, single run.
View data table
FaithfulnessAnswer relevancy
Factual (n=15)0.980.95
Ambiguous (n=10)0.960.86
Edge case (n=5)0.920.94
Multi-hop (n=10)0.910.77

Faithfulness

0.9490

40 answerable questions from a 50-question set; gpt-oss-120b answers judged by deepseek-chat, single run. Answer relevancy 0.8831.

Correct refusals

10/10

Questions the documents can’t answer; 0 of 40 answerable questions were wrongly refused.

Results are documented in the linked project artifacts. They have not been independently reproduced for this portfolio.

Limits & lessons

Each score is a single run with an LLM judge. Earlier entries used other judges, models, or question sets and aren’t comparable. Multi-hop answers remain the weakest. Pattern-based guardrails and optional model checks don’t establish comprehensive security or production readiness.

The authorization boundary and evaluation conditions matter more than a single headline score.

Source material