Skip to content

Context Enricher

Modules concerning the context enrichers used in Gen AI applications.

MetadataContextEnricher(metadata_fields, position=MetadataPosition.PREFIX, separator='\n---\n', field_template='- {field}: {value}', skip_empty=True, binary_handling=BinaryHandlingStrategy.BASE64)

Bases: BaseContextEnricher

A metadata context enricher that adds metadata to the chunk content.

This enricher formats metadata fields into a string and appends it to the chunk content based on the specified position (prefix or suffix).

Attributes:

Name Type Description
metadata_fields list[str]

List of metadata fields to include in the enriched content.

position MetadataPosition

Position of the metadata in the content. Valid values are defined in the MetadataPosition enum: - PREFIX: Metadata block is placed before content - SUFFIX: Metadata block is placed after content

separator str

Separator between the metadata and the content.

field_template str

Template for formatting each metadata field.

skip_empty bool

Whether to skip fields with empty values.

binary_handling BinaryHandlingStrategy

Strategy for handling binary data. Valid values are defined in the BinaryHandlingStrategy: - BASE64: Binary data is converted to base64 (default) - HEX: Binary data is converted to hexadecimal - NONE: Binary data is not included in the metadata block

Initialize the metadata context enricher.

Parameters:

Name Type Description Default
metadata_fields list[str]

List of metadata field names to include.

required
position MetadataPosition

Where to place metadata block. Valid values are defined in the MetadataPosition enum: 1. "prefix": Metadata block is placed before content 2. "suffix": Metadata block is placed after content

PREFIX
separator str

String to separate metadata from content.

'\n---\n'
field_template str

Template for formatting each metadata field. Available fields: - {field}: Field name - {value}: Field value

'- {field}: {value}'
skip_empty bool

Whether to skip empty fields.

True
binary_handling BinaryHandlingStrategy

Strategy for handling binary data. Valid values are defined in the BinaryHandlingStrategy: 1. "base64": Binary data is converted to base64 (default) 2. "hex": Binary data is converted to hexadecimal 3. "none": Binary data is not included in the metadata block

BASE64

enrich(chunks) async

Enrich chunks with metadata.

Parameters:

Name Type Description Default
chunks list[Chunk]

List of chunks to enrich.

required

Returns:

Type Description
list[Chunk]

list[Chunk]: List of enriched chunks.