Skip to content

Chat History Manager

Modules concerning chat history in Gen AI applications.

ChatHistoryManager(data_store, order_by=SchemaKey.CREATED_TIME, processor=None, fallback_to_original_history=True)

Bases: Component

A class for managing chat history in Gen AI applications.

This class provides functionality for storing and retrieving chat history with optional processing via a ChatHistoryProcessor.

Currently, this module only supports storing chat history in SQL databases. Database interactions are handled through the SQLAlchemy ORM via the SQLAlchemyDataStore class.

Attributes:

Name Type Description
data_store BaseSQLDataStore

Data store for storing and retrieving chat history.

order_by str

The column to order the chat history by.

processor BaseChatHistoryProcessor

Processor for chat history transformation.

fallback_to_original_history bool

Whether to fallback to the original history if the history processing fails or returns an empty list.

Initialize the chat history manager.

Parameters:

Name Type Description Default
data_store BaseSQLDataStore

Data store for storing and retrieving chat history. Currently only supports SQL-based data store.

required
order_by str

The column to order the chat history by. Defaults to "created_time".

CREATED_TIME
processor BaseChatHistoryProcessor | None

Processor for chat history transformation. Defaults to None.

None
fallback_to_original_history bool

Whether to fallback to the original history if the history processing fails or returns an empty list. Defaults to True.

True

retrieve(conversation_id, user_message=None, pair_limit=DEFAULT_PAIR_LIMIT, last_message_id=None) async

Retrieves the chat history of a given conversation ID.

This method returns the chat history in the format of a MultimodalPrompt.

Parameters:

Name Type Description Default
conversation_id str

The ID of the conversation.

required
user_message str | None

The user message. Defaults to None.

None
pair_limit int

The number of user-assistant message pairs to retrieve. Defaults to DEFAULT_PAIR_LIMIT.

DEFAULT_PAIR_LIMIT
last_message_id str | None

The ID of the last message to be retrieved in case of branching. Defaults to None, in which case the most recent message will be used as the last message.

None

Returns:

Name Type Description
MultimodalPrompt MultimodalPrompt | None

The retrieved and processed chat history.

store(conversation_id, user_message, assistant_message, user_message_id=None, assistant_message_id=None, parent_id=None, is_active=True, feedback=None, source=None, user_metadata=None, assistant_metadata=None) async

Stores the chat history of a given conversation ID.

Parameters:

Name Type Description Default
conversation_id str

The ID of the conversation.

required
user_message str

The user message.

required
assistant_message str

The assistant message.

required
user_message_id str | None

The ID of the user message. Defaults to None, in which case a new UUID will be generated.

None
assistant_message_id str | None

The ID of the assistant message. Defaults to None, in which case a new UUID will be generated.

None
parent_id str | None

The parent ID of the user message. Defaults to None, in which case the conversation ID will be used as the parent ID.

None
is_active bool

Whether the message is active. Defaults to True.

True
feedback str | None

The feedback for the assistant message. Defaults to None.

None
source str | None

The source of the assistant message. Defaults to None.

None
user_metadata dict[str, Any] | None

Additional data to store for the user message. Defaults to None.

None
assistant_metadata dict[str, Any] | None

Additional data to store for the assistant message. Defaults to None.

None