pub struct ContextualEnhancer { /* private fields */ }Expand description
Contextual Retrieval enhancer (index-time transform).
L is any BaseChatModel; a cheap small model is recommended (one call
per chunk). Enhancement failures are logged and degrade to the original
content — see the module docs.
Implementations§
Source§impl ContextualEnhancer
impl ContextualEnhancer
Sourcepub fn new<L>(llm: L) -> Self
pub fn new<L>(llm: L) -> Self
Creates an enhancer from any BaseChatModel.
Sourcepub fn new_arc(
llm: Arc<dyn BaseChatModel<Error = ProviderError> + Send + Sync>,
) -> Self
pub fn new_arc( llm: Arc<dyn BaseChatModel<Error = ProviderError> + Send + Sync>, ) -> Self
Builds from an already-wrapped Arc<dyn BaseChatModel<Error = ProviderError>>.
Sourcepub fn with_config(self, config: ContextualConfig) -> Self
pub fn with_config(self, config: ContextualConfig) -> Self
Sets the configuration.
Sourcepub fn with_max_concurrency(self, max_concurrency: usize) -> Self
pub fn with_max_concurrency(self, max_concurrency: usize) -> Self
Sets the max in-flight LLM calls.
Sourcepub async fn generate_context(
&self,
chunk: &str,
) -> Result<String, ContextualError>
pub async fn generate_context( &self, chunk: &str, ) -> Result<String, ContextualError>
Generates the situating context for one chunk (raw LLM access).
Sourcepub async fn enhance_document(
&self,
doc: &Document,
) -> Result<Document, ContextualError>
pub async fn enhance_document( &self, doc: &Document, ) -> Result<Document, ContextualError>
Enhances one document: returns a copy whose content is
context + "\n" + original and whose metadata carries the context under
CONTEXTUAL_METADATA_KEY.
Errors surface to the caller (use Self::enhance_documents for the
fail-open batch path).
Sourcepub async fn enhance_documents(&self, docs: &[Document]) -> Vec<Document>
pub async fn enhance_documents(&self, docs: &[Document]) -> Vec<Document>
Batch enhancement with bounded concurrency and fail-open semantics.
Each chunk is enhanced concurrently (up to max_concurrency in flight);
a chunk whose context generation fails keeps its original content (a
warning is logged) — indexing never blocks on the enhancement.
Idempotent: chunks already carrying CONTEXTUAL_METADATA_KEY are
passed through untouched, so re-running over an indexed corpus is a
no-op.