Downloads · 30 days
0
abhshkp/lost-in-the-middle-reproduction
lost-in-the-middle-reproduction is a machine learning model from abhshkp. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
This repository contains self-contained code to reproduce the key-value retrieval experiment from the paper "Lost in the Middle: How Language Models Use Long Contexts" (Liu et al., 2023, arXiv:2307.03172).
Downloads · 30 days
0
Access
Public
Updated May 5, 2026
Repo size
—
Likes
0
Public
Click a slice to open those files.
.py54.1 KB · 92%
From the Hugging Face model README
This repository contains self-contained code to reproduce the key-value retrieval experiment from the paper "Lost in the Middle: How Language Models Use Long Contexts" (Liu et al., 2023, arXiv:2307.03172).
The paper demonstrates that LLMs exhibit a U-shaped performance curve when asked to retrieve information from long contexts: they perform best when the relevant information is at the beginning (primacy bias) or end (recency bias) of the context, and worst when it is in the middle.
python gpu_experiment.py /app/litm/results_gpu Qwen/Qwen2.5-0.5B-Instruct 50 50
python run_kv_gpu.py \
--data-path kv_data_50.jsonl \
--num-keys 50 \
--model Qwen/Qwen2.5-0.5B-Instruct \
--output-dir results_gpu \
--max-examples 50
export OPENAI_API_KEY=sk-...
python run_openai.py \
--data-path kv_data_50.jsonl \
--num-keys 50 \
--model gpt-3.5-turbo \
--max-examples 50
From a CPU run with Qwen2.5-0.5B-Instruct (10 keys, 5 examples per position):
| Gold Position | Accuracy |
|---|---|
| 0 (start) | 1.00 |
| 2 | 0.60 |
| 5 (middle) | 1.00 |
| 7 | 0.80 |
| 9 (end) | 0.60 |
With more examples and longer contexts, the U-shaped pattern becomes more pronounced (per the paper).
The exact prompt from the paper:
Extract the value corresponding to the specified key in the JSON object below.
JSON data:
{"<key1>": "<value1>",
"<key2>": "<value2>",
...}
Key: "<query_key>"
Corresponding value:
@article{liu2023lost,
title={Lost in the Middle: How Language Models Use Long Contexts},
author={Liu, Nelson F and Lin, Kevin and Hewitt, John and Paranjape, Ashwin and Bevilacqua, Michele and Petroni, Fabio and Liang, Percy},
journal={arXiv preprint arXiv:2307.03172},
year={2023}
}
<!-- ml-intern-provenance -->
This model repository was generated by ML Intern, an agent for machine learning research and development on the Hugging Face Hub.
from transformers import AutoModelForCausalLM, AutoTokenizer
model_id = "abhshkp/lost-in-the-middle-reproduction"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id)
For non-causal architectures, replace AutoModelForCausalLM with the appropriate AutoModel class.