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 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 + message
creates a new chat history with the message appended to the end, whilemessage + history
creates 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
n
messages in the chat history, or all messages if there are fewer thann
messages or ifn
isNone
.Render this object as an expression.
Attribute summary
The messages in the chat history.
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:
- get_messages(n=None)[source]#
Get the last
n
messages in the chat history, or all messages if there are fewer thann
messages or ifn
isNone
.
- 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.