artkit.model.llm.history.ChatHistory#

class artkit.model.llm.history.ChatHistory(messages=None, *, max_length=None)[source]#

A chat history, which consists of a sequence of messages.

Supports adding messages, getting the last n messages, iteration using iter() and getting the number of messages using len().

Chat messages can also be added using the + operator, which prepends or appends a message to the start or end of the chat history, respectively.

history + message creates a new chat history with the message appended to the end, while message + history creates a new chat history with the message prepended to the start.

Bases:

HasExpressionRepr

Metaclasses:

ABCMeta

Parameters:

Method summary

add_message

Add a message to the chat history.

get_messages

Get the last n messages in the chat history, or all messages if there are fewer than n messages or if n is None.

to_expression

Render this object as an expression.

Attribute summary

messages

The messages in the chat history.

max_length

The maximum length of the chat history, or None if there is no maximum length.

Definitions

add_message(message)[source]#

Add a message to the chat history.

Parameters:

message (ChatMessage) – the message to add

Return type:

None

get_messages(n=None)[source]#

Get the last n messages in the chat history, or all messages if there are fewer than n messages or if n is None.

Parameters:

n (Optional[int]) – the number of messages to get (optional)

Return type:

list[ChatMessage]

Returns:

the last n messages

to_expression()[source]#

Render this object as an expression.

Return type:

Expression

Returns:

the expression representing this object

max_length: Optional[int]#

The maximum length of the chat history, or None if there is no maximum length.

messages: list[ChatMessage]#

The messages in the chat history.