Level 4 — ApplicationAlignment, Prompting & RAG

Alignment, Prompting & RAG

60 minutes60 marksprintable — key stays hidden on paper

Level 4 — Application (novel problems, no hints) Time limit: 60 minutes Total marks: 60

Instructions: Answer all questions. Show all working. Use ...... for inline math and ...... for displayed equations.


Question 1 — DPO vs. RLHF derivation and computation (14 marks)

A team switches from PPO-based RLHF to Direct Preference Optimization. Recall the DPO loss for a preference pair (x,yw,yl)(x, y_w, y_l) where ywy_w is preferred:

LDPO=logσ ⁣(β[logπθ(ywx)πref(ywx)logπθ(ylx)πref(ylx)])\mathcal{L}_{\text{DPO}} = -\log \sigma\!\left( \beta \left[ \log\frac{\pi_\theta(y_w|x)}{\pi_{\text{ref}}(y_w|x)} - \log\frac{\pi_\theta(y_l|x)}{\pi_{\text{ref}}(y_l|x)} \right] \right)

(a) Explain why DPO removes the need for an explicit reward model and a separate RL loop, referencing the Bradley–Terry model that also underlies reward modeling. (4)

(b) For one training example, the following log-probabilities are given (β=0.1\beta = 0.1):

  • logπθ(ywx)=8.0\log \pi_\theta(y_w|x) = -8.0, logπref(ywx)=9.0\log \pi_{\text{ref}}(y_w|x) = -9.0
  • logπθ(ylx)=6.0\log \pi_\theta(y_l|x) = -6.0, logπref(ylx)=5.0\log \pi_{\text{ref}}(y_l|x) = -5.0

Compute the argument of σ\sigma, then the DPO loss value. State whether the current update is pushing the model in the desired direction and justify. (6)

(c) The "implicit reward" DPO assigns to a completion is r^(x,y)=βlogπθ(yx)πref(yx)\hat{r}(x,y) = \beta \log\frac{\pi_\theta(y|x)}{\pi_{\text{ref}}(y|x)}. Using the numbers above, compute r^\hat r for ywy_w and yly_l and interpret the sign of their difference. (4)


Question 2 — Retrieval design under a latency/quality budget (14 marks)

You are building a RAG system over a 2-million-chunk corpus. A user query must return an answer within 800 ms.

(a) You must choose between (i) dense-only retrieval, (ii) sparse (BM25) only, and (iii) hybrid search with a cross-encoder reranker. For a query using rare product SKU codes and natural-language intent, justify which pipeline you pick and explain the specific failure mode each rejected option would suffer. (6)

(b) The reranker cross-encoder takes 12 ms per (query, candidate) pair. First-stage retrieval returns top-kk candidates and you rerank all of them. Retrieval + embedding + generation overhead is fixed at 500 ms. Compute the maximum kk you can rerank while staying under 800 ms. (4)

(c) Your embeddings are 768-dim floats. Explain one concrete reason to use an ANN index (e.g., HNSW) rather than exact cosine search over 2M vectors, and name one quality trade-off this introduces. (4)


Question 3 — Chunking and hallucination mitigation (12 marks)

A legal-document RAG system is returning confident but fabricated clause numbers.

(a) The documents are long contracts with numbered clauses and cross-references. Propose a chunking strategy (size, overlap, and boundary policy) suited to this structure and explain why naive fixed-256-token chunking harms this use case. (5)

(b) Give three distinct hallucination-mitigation techniques applicable at retrieval time, generation time, and evaluation time respectively (one each). For each, state the mechanism by which it reduces fabrication. (6)

(c) State one reason increasing retrieved context (more chunks) can increase hallucination rather than reduce it. (1)


Question 4 — Prompting strategy selection & self-consistency (12 marks)

(a) For a multi-step arithmetic word problem, contrast zero-shot, few-shot, and chain-of-thought prompting in terms of expected accuracy and token cost. Explain why CoT helps in mechanistic terms. (5)

(b) Self-consistency samples nn reasoning chains and takes a majority vote. Suppose each independent chain reaches the correct final answer with probability p=0.6p = 0.6, and wrong answers are spread so no single wrong answer beats the correct one in a majority vote unless the correct answer is a minority. Model the correct answer winning as "correct is the majority among n=5n=5 chains" (i.e., at least 3 of 5 correct). Compute this probability. (5)

(c) State one scenario where tree-of-thought is preferable to self-consistency. (2)


Question 5 — Constitutional AI & reward modeling short design (8 marks)

(a) Constitutional AI replaces much human feedback with AI feedback guided by a set of principles. Describe the two-phase (supervised critique-revision, then RL) pipeline and state the main advantage over standard RLHF regarding scalability. (5)

(b) A reward model is trained on pairwise human preferences. Explain what "reward hacking" (over-optimization) means in this context and give one practical mitigation. (3)

Answer keyMark scheme & solutions

Question 1 (14 marks)

(a) (4 marks)

  • RLHF trains a reward model rϕr_\phi under the Bradley–Terry model P(ywyl)=σ(r(yw)r(yl))P(y_w \succ y_l) = \sigma(r(y_w)-r(y_l)), then optimizes the policy against rϕr_\phi with PPO. (1)
  • DPO shows the optimal RLHF policy has a closed form relating reward to policy: r(x,y)=βlogπθ(yx)πref(yx)+βlogZ(x)r(x,y) = \beta\log\frac{\pi_\theta(y|x)}{\pi_{\text{ref}}(y|x)} + \beta\log Z(x). (1)
  • Substituting into the Bradley–Terry likelihood, Z(x)Z(x) cancels (it is the same for yw,yly_w,y_l given xx), giving a loss directly in terms of the policy. (1)
  • Hence no separate reward network and no online RL sampling loop are needed — it becomes a supervised classification-style loss on preference pairs. (1)

(b) (6 marks) Compute the two log-ratios:

  • ywy_w: 8.0(9.0)=1.0-8.0 - (-9.0) = 1.0 (1)
  • yly_l: 6.0(5.0)=1.0-6.0 - (-5.0) = -1.0 (1)
  • Difference: 1.0(1.0)=2.01.0 - (-1.0) = 2.0; argument =β2.0=0.1×2.0=0.2= \beta \cdot 2.0 = 0.1 \times 2.0 = 0.2 (1)
  • Loss =logσ(0.2)= -\log\sigma(0.2). σ(0.2)=1/(1+e0.2)=0.549834\sigma(0.2) = 1/(1+e^{-0.2}) = 0.549834; L=log(0.549834)=0.598139\mathcal{L} = -\log(0.549834) = 0.598139 (2)
  • Argument is positive \Rightarrow model already ranks ywy_w above yly_l relative to reference; gradient will reinforce the correct ordering — desired direction. (1)

(c) (4 marks)

  • r^(yw)=0.1×1.0=0.1\hat r(y_w) = 0.1 \times 1.0 = 0.1 (1)
  • r^(yl)=0.1×(1.0)=0.1\hat r(y_l) = 0.1 \times (-1.0) = -0.1 (1)
  • Difference =0.2>0= 0.2 > 0 (1)
  • Positive difference means the implicit reward prefers ywy_w, consistent with the human preference; the policy is aligned on this pair. (1)

Question 2 (14 marks)

(a) (6 marks)

  • Choose (iii) hybrid + reranker. (1)
  • SKU codes are rare exact tokens: dense embeddings tend to smear/normalize rare literal strings, so dense-only may miss exact code matches (lexical failure). (1.5)
  • Natural-language intent (semantics, paraphrase): BM25-only fails on synonyms/paraphrase where no lexical overlap exists (vocabulary mismatch). (1.5)
  • Hybrid combines lexical (BM25 catches SKU) + dense (catches intent); reranker (cross-encoder) resolves final ordering with full query-doc attention. (1.5)
  • Note reranker adds latency, justified by needing precision — leads into (b). (0.5)

(b) (4 marks)

  • Budget for reranking =800500=300= 800 - 500 = 300 ms. (2)
  • kmax=300/12=25k_{\max} = \lfloor 300 / 12 \rfloor = 25 candidates. (2)

(c) (4 marks)

  • Exact cosine over 2M×768 vectors is O(N)O(N) per query (~1.5B multiply-adds) → too slow for 800 ms at scale; HNSW gives sub-linear (~log) search. (2)
  • Trade-off: ANN is approximate — recall < 100%, so some true nearest neighbors may be missed (recall/latency trade-off tuned by parameters like efSearch). (2)

Question 3 (12 marks)

(a) (5 marks)

  • Use structure-aware / semantic chunking on clause boundaries, one clause (or clause group) per chunk, with modest overlap (e.g., 10–20%) to preserve cross-reference context, keeping clause numbers/headers as metadata. (3)
  • Naive fixed-256-token chunking can split a clause mid-sentence, orphan a clause number from its body, and break cross-references — so the model sees fragments and invents/mismatches clause numbers. (2)

(b) (6 marks) — one per stage, 2 each:

  • Retrieval time: relevance/score thresholding — drop low-similarity chunks so the model isn't fed off-topic context that invites fabrication. (2)
  • Generation time: grounding/citation prompting ("answer only from context; cite chunk; say 'not found' if absent") — constrains generation to supplied evidence. (2)
  • Evaluation time: faithfulness check / LLM-as-judge or NLI entailment of claims against retrieved sources — flags/filters unsupported statements before returning. (2)

(c) (1 mark)

  • More chunks add irrelevant/contradictory context ("lost in the middle", distractors) that the model may blend or misattribute, increasing fabrication. (1)

Question 4 (12 marks)

(a) (5 marks)

  • Zero-shot: lowest tokens, lowest accuracy on multi-step problems. (1)
  • Few-shot: examples improve format/accuracy, moderate token cost, but may still skip reasoning. (1)
  • CoT: highest accuracy on multi-step arithmetic, highest token cost (generates reasoning). (1)
  • Mechanism: CoT allocates intermediate compute/tokens, decomposing the problem so each step conditions on prior derived results, reducing single-shot arithmetic errors and giving the model "scratch space." (2)

(b) (5 marks)

  • Need P(at least 3 of 5 correct)P(\text{at least 3 of 5 correct}) with p=0.6p=0.6, Binomial(5, 0.6). (1)
  • P(3)=(53)(0.6)3(0.4)2=10(0.216)(0.16)=0.3456P(3)=\binom{5}{3}(0.6)^3(0.4)^2 = 10(0.216)(0.16)=0.3456
  • P(4)=(54)(0.6)4(0.4)=5(0.1296)(0.4)=0.2592P(4)=\binom{5}{4}(0.6)^4(0.4)=5(0.1296)(0.4)=0.2592
  • P(5)=(0.6)5=0.07776P(5)=(0.6)^5=0.07776 (3)
  • Sum =0.3456+0.2592+0.07776=0.68256=0.3456+0.2592+0.07776 = 0.68256 (1)

(c) (2 marks)

  • ToT preferred when the task needs exploration/backtracking over a branching search space (e.g., puzzles, planning, game-of-24) where intermediate states must be evaluated and pruned — not just independent parallel chains. (2)

Question 5 (8 marks)

(a) (5 marks)

  • Phase 1 (SL, critique-revision): model generates a response, critiques it against constitutional principles, then revises; fine-tune on the revised responses. (2)
  • Phase 2 (RLAIF): train a preference/reward model from AI-generated preference labels (chosen per principles), then RL-optimize the policy against it. (2)
  • Advantage: replaces expensive/slow human labels with scalable AI feedback → cheaper, faster, more consistent harmlessness labeling. (1)

(b) (3 marks)

  • Reward hacking: policy exploits flaws/biases in the imperfect reward model, maximizing predicted reward while degrading true quality (e.g., verbosity, sycophancy, formatting tricks). (2)
  • Mitigation: KL penalty to reference policy (or early stopping / reward-model refresh / ensembling). (1)

[
  {"claim": "DPO sigmoid argument = 0.2 and loss = 0.598139", "code": "arg = 0.1*((-8.0-(-9.0)) - (-6.0-(-5.0))); import sympy as sp; loss = -sp.log(1/(1+sp.exp(-arg))); result = (abs(arg-0.2)<1e-9) and (abs(float(loss)-0.598139)<1e-4)"},
  {"claim": "Implicit reward difference = 0.2", "code": "rw=0.1*1.0; rl=0.1*(-1.0); result = abs((rw-rl)-0.2)<1e-9"},
  {"claim": "Max reranker candidates k = 25", "code": "k = (800-500)//12; result = k==25"},
  {"claim": "Self-consistency P(>=3 of 5 correct, p=0.6) = 0.68256", "code": "from sympy import binomial, Rational; p=Rational(6,10); q=1-p; total=sum(binomial(5,i)*p**i*q**(5-i) for i in range(3,6)); result = abs(float(total)-0.68256)<1e-6"}
]