Skip to content

Reference formatter

Defines a base class for reference formatters used in Gen AI applications.

BaseReferenceFormatter(stringify=True, format_references_func=None, format_chunk_func=None, format_chunk_metadata_map=None, streamable=True)

Bases: Component, ABC

An abstract base class for the reference formatters used in Gen AI applications.

The BaseReferenceFormatter class provides a framework for formatting references based on the synthesized response and retrieved chunks. It first retrieves the relevant chunks based on the synthesized response, then formats the references as a string or a list of chunks depending on the stringify attribute. Subclasses must implement the _get_relevant_chunks method to retrieve the relevant chunks.

Attributes:

Name Type Description
stringify bool

Whether to format the references as a string. If False, the references will be returned as a list of chunks.

format_references_func Callable[[list[str]], str]

A function that formats a list of formatted chunks as a string of references.

format_chunk_func Callable[[Chunk], str]

A function that formats a chunk as a reference.

streamable bool

A flag to indicate whether the formatted references will be streamed if an event emitter is provided.

Initializes a new instance of the BaseReferenceFormatter class.

Parameters:

Name Type Description Default
stringify bool

Whether to format the references as a string. If False, the references will be returned as a list of chunks. Defaults to True.

True
format_references_func Callable[[list[str]], str] | None

A function that formats a list of formatted chunks as a string of references. Defaults to None, in which case format_references_as_string will be used.

None
format_chunk_func Callable[[Chunk], str] | None

A function that formats a chunk as a reference. Defaults to None, in which case format_chunk_as_string will be used.

None
format_chunk_metadata_map dict[str, str] | None

A dictionary mapping the metadata keys needed by the format_chunk_func to the actual chunk metadata keys. The keys in the dictionary must match the metadata keys needed by the format_chunk_func. Defaults to None, in which case DEFAULT_FORMAT_CHUNK_METADATA_MAP will be used.

None
streamable bool

A flag to indicate whether the formatted references will be streamed if an event emitter is provided. Defaults to True.

True

format_reference(response, chunks, event_emitter=None) async

Formats references based on the synthesized response and retrieved chunks.

Parameters:

Name Type Description Default
response str

The synthesized response.

required
chunks list[Chunk]

A list of Chunk objects used to generate the references.

required
event_emitter EventEmitter | None

The optional event emitter to stream the formatted reference. Defaults to None.

None

Returns:

Type Description
str | list[Chunk]

str | list[Chunk]: The formatted references as a string or a list of chunks.

Raises:

Type Description
ValueError

If response or chunks are missing or invalid.

format_chunk_as_string(metadata_map)

Creates a function that formats a chunk as a string of reference.

Parameters:

Name Type Description Default
metadata_map dict[str, str]

A dictionary mapping the metadata keys to the chunk metadata keys.

required

Returns:

Type Description
Callable[[Chunk], str]

Callable[[Chunk], str]: A function that formats a chunk as a string of reference.

format_references_as_string(formatted_chunks)

Formats a list of formatted chunks as a string of references.

Parameters:

Name Type Description Default
formatted_chunks list[str]

A list of formatted chunks.

required

Returns:

Name Type Description
str str

A string of references.