Chat
ChatHistory#
The ChatHistory
class is where all the conversation between the user and the LLM gets stored. A ChatHistory
object will transparently be instanciated in the Brain
every time you create one.
At each interaction with Brain.ask_streaming
both your message and the LLM's response are added to this chat history. It's super handy because this history is used in the Retrieval-Augmented Generation (RAG) process to give the LLM more context, working as form of memory between the user and the system and helping it generate better responses by looking at what’s already been said.
You can also get some cool info about the brain by printing its details with the print_info()
method, which shows things like how many chats are stored, the current chat history, and more. This makes it easy to keep track of what’s going on in your conversations and manage the context being sent to the LLM!
ChatHistory #
ChatHistory is a class that maintains a record of chat conversations. Each message
in the history is represented by an instance of the ChatMessage
class, and the
chat history is stored internally as a list of these ChatMessage
objects.
The class provides methods to retrieve, append, iterate, and manipulate the chat
history, as well as utilities to convert the messages into specific formats
and support deep copying.
__init__ #
__init__(chat_id, brain_id)
Init a new ChatHistory object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chat_id |
UUID
|
A unique identifier for the chat session. |
required |
brain_id |
UUID | None
|
An optional identifier for the brain associated with the chat. |
required |
append #
append(langchain_msg, metadata={})
Appends a new message to the chat history.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
langchain_msg |
AIMessage | HumanMessage
|
The message content (either an AI or Human message). |
required |
metadata |
dict[str, Any]
|
Additional metadata related to the message. Defaults to an empty dictionary. |
{}
|
get_chat_history #
get_chat_history(newest_first=False)
Retrieves the chat history, optionally sorted in reverse chronological order.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
newest_first |
bool
|
If True, returns the messages in reverse order (newest first). Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
List[ChatMessage]
|
List[ChatMessage]: A sorted list of chat messages. |
iter_pairs #
iter_pairs()
Iterates over the chat history in pairs, returning a HumanMessage followed by an AIMessage.
Yields:
Type | Description |
---|---|
HumanMessage
|
Tuple[HumanMessage, AIMessage]: Pairs of human and AI messages. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the messages in the pair are not in the expected order (i.e., a HumanMessage followed by an AIMessage). |
to_list #
to_list()
Converts the chat history into a list of raw HumanMessage or AIMessage objects.
Returns:
Type | Description |
---|---|
List[HumanMessage | AIMessage]
|
list[HumanMessage | AIMessage]: A list of messages in their raw form, without metadata. |
options: heading_level: 2