Contextualizers¶
The free, offline heuristic contextualizer and the opt-in, paid LLM contextualizer — see Contextual retrieval for how these two compare.
retrieval.contextualizer¶
Deterministic, offline contextualizer for RAG chunks.
No model, no network, no randomness. Output is byte-identical for identical inputs.
contextualize(chunk, all_chunks_in_doc)
¶
Return contextualized text for chunk using all chunks in the same document.
Source code in engine/retrieval/contextualizer.py
59 60 61 | |
make_context(chunk, neighbors)
¶
Build a context prefix for a chunk given its neighbour chunks.
Format
[
When heading or previous neighbour is absent, degrade gracefully.
Source code in engine/retrieval/contextualizer.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
retrieval.llm_contextualizer¶
LLM-based contextualizer — Anthropic's original Contextual Retrieval.
Where the heuristic contextualizer (retrieval/contextualizer.py) prepends a
deterministic breadcrumb, this implements the technique from Anthropic's
"Contextual Retrieval" post: for each chunk, an LLM is shown the whole
document and the chunk, and writes a short context that situates the chunk
within the document. That context is prepended before indexing, giving the
sparse retriever vocabulary the chunk body lacks.
This is an opt-in, online comparison arm — it needs the anthropic
package and ANTHROPIC_API_KEY. The import is lazy (inside methods), so the
default offline pipeline and the no-network-imports test are unaffected.
The whole document is sent in a cache_control block, so every chunk from the
same document reuses the cached document prefix (the standard cost optimization
for this technique).
LLMContextualizer
¶
Situate each chunk within its document via an LLM (Contextual Retrieval).
Exposes the same generate(chunk, all_chunks) -> str contract as the
contextualizers in retrieval/providers.py.
Source code in engine/retrieval/llm_contextualizer.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
generate(chunk, all_chunks_in_doc)
¶
Return <llm context> <chunk text> for indexing.
Source code in engine/retrieval/llm_contextualizer.py
111 112 113 114 115 | |
LLMDocumentContextualizer
¶
Document-level context enrichment (document-granularity analog).
Used by ContextualLexicalRetriever (retrieval/retrievers.py), which
retrieves whole documents rather than chunks, so there is no parent
document to situate a chunk within. The document-level adaptation of
Contextual Retrieval is to ask the LLM for a short context (topics + key
entities) for the whole document and prepend it before indexing — giving
the sparse retriever extra surface vocabulary.
Runs on Haiku (the cheapest tier) since this is the cost arm — one call per
document. The static instruction sits in a cache_control system
block so the shared prefix is reused across documents; the document body is
the varying suffix and isn't cached.
Exposes generate(document_text) -> str returning the context string
(the caller prepends it to the document).
Source code in engine/retrieval/llm_contextualizer.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
generate(document_text)
¶
Return a short LLM-written context describing the document.
Source code in engine/retrieval/llm_contextualizer.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
contextualize_llm(chunk, all_chunks_in_doc)
¶
Module-level convenience matching the contextualize signature.
Source code in engine/retrieval/llm_contextualizer.py
172 173 174 175 176 177 | |