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
nmessages, iteration usingiter()and getting the number of messages usinglen().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 + messagecreates a new chat history with the message appended to the end, whilemessage + historycreates a new chat history with the message prepended to the start.- Bases:
HasExpressionRepr- Metaclasses:
- Parameters:
messages (
Optional[Iterable[ChatMessage]]) – the initial messages in the chat history (optional)max_length (
Optional[int]) – the maximum length of the chat history
Method summary
Add a message to the chat history.
Get the last
nmessages in the chat history, or all messages if there are fewer thannmessages or ifnisNone.Render this object as an expression.
Attribute summary
The messages in the chat history.
The maximum length of the chat history, or
Noneif 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:
- get_messages(n=None)[source]#
Get the last
nmessages in the chat history, or all messages if there are fewer thannmessages or ifnisNone.
- 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
Noneif there is no maximum length.
-
messages:
list[ChatMessage]# The messages in the chat history.