LLM Engineering · Exercise 5 of 5 Previous
Intermediate
Trim a chat to fit the context window
Write trim_history(messages, budget, count). messages is a list of (role, text) pairs, count(text) returns a token count and budget is the most input tokens allowed.
Keep the first message (the system prompt) and the last message. While the total is over budget, remove the oldest message between them. Return the kept list.
If the first and last messages alone are over budget, raise ValueError with a message that contains the word budget.
What Check tests
- Nothing is dropped when everything fits
- The oldest middle messages go first
- The system prompt and the newest message are always kept
- The input list is not changed
- Too big even alone raises ValueError mentioning budget
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.