Skip to main content

order_by_value

Function order_by_value 

Source
pub fn order_by_value(
    frames: Vec<(String, ContextFrame)>,
) -> Vec<(String, ContextFrame)>
Expand description

Order frames for placement in the prompt, highest value at the attention-favored edges — the Lost-in-the-Middle placement (Liu et al., TACL 2024, arXiv:2307.03172; docs/protocol-advantages.md §12), which shows an LLM attends most to the top and bottom of a long context and least to its middle.

Frames are first ranked by score descending, ties broken by canonical FrameId — so the ranking, and therefore the placement, is a pure function of the input set. The ranked frames are then dealt to alternating ends of the output: rank 0 to the top, rank 1 to the bottom, rank 2 just below the top, rank 3 just above the bottom, and so on, leaving the lowest-value frames in the low-attention middle. For a fixed set of frames and scores this yields identical bytes every time; it does not promise the stricter score-independence of compose_context, because placing by value is exactly a choice to let score matter.