Reorder Context for Long-Context Models

easy
Unit 3 · RAG I0/3 to milestone

"Lost in the Middle" (Liu et al., 2023) found that LLMs use information at the start and end of a long context far better than information in the middle. So put the most relevant documents at the edges.

Implement:

  • edge_reorder(docs): docs are sorted best first. Place the 1st, 3rd, 5th, … at the front in order and the 2nd, 4th, 6th, … at the back in reverse, so the two most relevant documents end up at the two ends: [1, 2, 3, 4, 5, 6] → [1, 3, 5, 6, 4, 2].
  • reorder_by_score(docs, scores): sort by score (highest first, ties by original index), then apply edge_reorder.
  • edge_distance(order, doc): how far doc is from the nearest end of the list (0 at either end).
#rag#long-context#prompting2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Reorder Context for Long-Context Models · TechGradify