Intermediate10 min read38 of 40

Relevant Segment Extraction (RSE)

The context reconstructor — instead of returning scattered chunks, finds the best contiguous section of the document that answers the query.

Overview

PropertyDetail
PatternRelevant Segment Extraction (RSE) — The Context Reconstructor
Level4 (Better post-retrieval)
Key InnovationFinds best CONTIGUOUS section, not scattered chunks
Core AlgorithmMaximum sub-array (Kadane's-style)
Notebook10_Contextual_Compression.ipynb

Architecture

flowchart TD
    A["1. Chunk Document (NO overlap)"] --> B["2. Score ALL Chunks Against Query"]
    B --> C["3. Subtract Threshold from Each Score"]
    C --> D["4. Find MAX-SUM Contiguous Segment\n(Kadane's Algorithm)"]
    D --> E["5. Concatenate Best Segment"]
    E --> F["Return Coherent Passage"]

    style D fill:#1e293b,stroke:#f59e0b,stroke-width:2px

Test Results (Actual Notebook Output)

Query: "What are the effects of climate change on sea levels?"

MethodResult
Simple top-33 scattered chunks (~2,400 chars) from random parts
RSEChunks 7-12 contiguous (4,804 chars) = "Chapter 3: Effects of Climate Change"

RSE correctly identified that the entire "Effects" chapter is the best context — not 3 isolated paragraphs.


The Problem RSE Solves

Issue with Simple RAGHow RSE Fixes It
Top-K chunks are scattered across documentFinds contiguous section
Chunks cut mid-sentenceNo-overlap chunking + concatenation
Missing "glue" between relevant chunksIncludes slightly-irrelevant chunks if sandwiched
LLM gets fragments, not narrativeLLM gets coherent passage

The Algorithm (Simple Version)

Chunk values: [-0.2, -0.2, 0.4, 0.8, -0.1, 0.6, 0.3, -0.2, -0.2] ↑_____________________↑ Best segment: chunks 3-7, sum = 2.0

Key insight: Chunk 5 (value -0.1) is included even though it's slightly irrelevant — because the overall segment including it has a higher total value than skipping it.


RSE vs Simple RAG vs Semantic Chunking

AspectSimple RAGSemantic ChunkingRSE
ReturnsTop-K scattered chunksTop-K coherent chunksBest contiguous section
Context for LLMFragmentsBetter fragmentsFull narrative
Character count~2,400 (3 chunks)~2,400 (3 chunks)~4,800 (whole section)
Best forQuick lookupsTopic-based queriesMulti-paragraph answers

When to Use

ScenarioUse RSE?
Questions needing multi-paragraph answersYes
Documents with info in consecutive sectionsYes
Need LLM to have coherent narrative, not fragmentsYes
Reports, papers, legal docsYes
Simple one-fact lookupsNo — overkill
Scattered info across unrelated sectionsNo — simple top-K is fine

Tech Stack

ComponentTechnology
EmbeddingsAWS Bedrock — Amazon Titan Embed Text v2 (1024 dims)
LLMAWS Bedrock — Claude Sonnet
Vector DBQdrant Cloud (cosine similarity)
Chunk Size800 characters (no overlap)
AlgorithmMaximum sub-array (modified Kadane's)
FrameworkLangChain

Source