Share via


ChatHistoryReducer Class

Defines a contract for reducing chat history.

Note: This class is marked as 'experimental' and may change in the future.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Constructor

ChatHistoryReducer(*, messages: list[ChatMessageContent] = None, system_message: str | None = None, target_count: Annotated[int, Gt(gt=0)], threshold_count: Annotated[int, Ge(ge=0)] = 0, auto_reduce: bool = False)

Keyword-Only Parameters

Name Description
messages
Required
system_message
Required
target_count
Required
threshold_count
Required
auto_reduce
Required

Methods

add_message_async

Add a message to the chat history.

If auto_reduce is enabled, the history will be reduced after adding the message.

reduce

Reduce the chat history in some way (e.g., truncate, summarize).

add_message_async

Add a message to the chat history.

If auto_reduce is enabled, the history will be reduced after adding the message.

async add_message_async(message: ChatMessageContent | dict[str, Any], encoding: str | None = None, metadata: dict[str, Any] | None = None) -> None

Parameters

Name Description
message
Required
encoding
Default value: None
metadata
Default value: None

reduce

Reduce the chat history in some way (e.g., truncate, summarize).

abstract async reduce() -> Self | None

Returns

Type Description

A possibly shorter list of messages, or None if no change is needed.

Attributes

auto_reduce

auto_reduce: bool

is_experimental

is_experimental = True

stage_status

stage_status = 'experimental'

target_count

target_count: int

threshold_count

threshold_count: int