Lesson 9 / 25
Compressing Content
Keep the sentences that matter for this question.
Extractive and abstractive compression
When good content does not fit, compress it. Extractive compression keeps only the sentences relevant to the query (cheap, exact wording preserved, easy to cite). Abstractive compression asks a model to summarise (shorter, but it costs a call and can drop or distort details). Compress per chunk with the question in mind, keep identifiers, numbers and dates exact, and keep a link to the original so answers can still be cited and checked. Measure whether compression hurts accuracy before relying on it.
Keeping query-relevant sentences, run
I ran this with plain Python 3 (standard library only); the data is made-up example data. A six-sentence policy is cut to the three sentences that mention refunds or returns, from 253 to 155 characters. Real systems would match on embeddings or a model, not a fixed word list.
doc = ("Our company was founded in 2009. Refunds are issued within 14 days of receiving the item. "
"We have offices in three cities. Items must be unused to qualify for a refund. "
"Our newsletter is sent monthly. Shipping costs for returns are paid by the customer.")
query = {"refund", "refunds", "returns", "return"}
sentences = [s.strip() + "." for s in doc.split(".") if s.strip()]
kept = [s for s in sentences if query & set(w.strip(",.").lower() for w in s.split())]
print("sentences:", len(sentences), "-> kept:", len(kept))
print("chars:", len(doc), "->", len(" ".join(kept)))
for s in kept:
print(" -", s)
Output:
sentences: 6 -> kept: 3 chars: 253 -> 155 - Refunds are issued within 14 days of receiving the item. - Items must be unused to qualify for a refund. - Shipping costs for returns are paid by the customer.
Never paraphrase numbers
If you summarise with a model, instruct it to copy amounts, dates and IDs verbatim and spot-check that it did.
Quick check: What is an advantage of extractive compression?
- It needs a second large model
- It always produces the shortest text
- Original wording is preserved, so it stays exact and citable
- It removes the need for retrieval
Answer
Original wording is preserved, so it stays exact and citable — Extractive keeps exact sentences; abstractive rewrites them.