← Lesson
Packing the Context Window
Joeven
Run
Reset
Python loads on first run
HITS = [ {"id": "a", "score": 0.9, "text": "Refunds take 5-7 days. Never cash."}, {"id": "b", "score": 0.8, "text": "INV-17 is open for 4000 cents."}, {"id": "c", "score": 0.4, "text": "Pizza is on Fridays in the cafeteria." * 3}, ] LIMIT = 80 def pack(hits, limit=LIMIT): ordered = sorted(hits, key=lambda h: -h["score"]) used = 0 out = [] for h in ordered: room = limit - used if room <= 0: break text = h["text"] truncated = False if len(text) > room: text = text[:room] truncated = True out.append({"id": h["id"], "text": text, "truncated": truncated}) used += len(text) return out, used packed, used = pack(HITS) print("used", used, "of", LIMIT) for p in packed: print(p["id"], "trunc=" + str(p["truncated"]), p["text"][:50])
Run to execute this in your browser. Nothing is sent to a server.