Memory Module¶
erniebot_agent.memory
¶
Memory
¶
The base class of memory
Attributes:
| Name | Type | Description |
|---|---|---|
msg_manager |
MessageManager
|
the message manager of a conversation. |
Returns:
| Type | Description |
|---|---|
|
A memory object. |
Source code in erniebot-agent/src/erniebot_agent/memory/base.py
WholeMemory
¶
The type of memory that include all the messages.
Attributes:
| Name | Type | Description |
|---|---|---|
msg_manager |
MessageManager
|
the message manager of a conversation. |
Source code in erniebot-agent/src/erniebot_agent/memory/whole_memory.py
LimitTokensMemory
¶
The class of memory that limits the number of tokens. If number of tokens in the context >= max_token_limit, it will pop message from msg_manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_token_limit |
int
|
The maximum number of tokens in the context. |
3000
|
Attributes:
| Name | Type | Description |
|---|---|---|
max_token_limit |
int
|
The maximum number of tokens in the context. |
mem_token_count |
int
|
The number of tokens in the context. |
Examples:
.. code-block:: python
from erniebot_agent.memory import LimitTokensMemory
memory = LimitTokensMemory(max_token_limit=3000)
memory.add_message(AIMessage("Hello world!"))
Source code in erniebot-agent/src/erniebot_agent/memory/limit_tokens_memory.py
add_message(message)
¶
Add a message to memory. Prune the message if number of tokens in memory >= max_token_limit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message |
Message
|
The message to be added. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Source code in erniebot-agent/src/erniebot_agent/memory/limit_tokens_memory.py
prune_message()
¶
Prune the message if number of tokens in memory >= max_token_limit.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the message is empty after pruning. |
Returns:
| Type | Description |
|---|---|
|
None |
Source code in erniebot-agent/src/erniebot_agent/memory/limit_tokens_memory.py
SlidingWindowMemory
¶
This class controls max number of rounds of message using sliding window tactic. Each round contains a piece of human message and a piece of AI message.
Attributes:
| Name | Type | Description |
|---|---|---|
max_round(int) |
Max number of rounds. |
|
retained_round(int) |
The first number of rounds of memory will be preserverd. Default to 0. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If max_round is not positive integer. |
Source code in erniebot-agent/src/erniebot_agent/memory/sliding_window_memory.py
__init__(max_round, retained_round=0)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_round(int) |
Max number of rounds(round: human message and AI message). |
required | |
retained_round(int) |
The number remaining_memory rounds of memory to be retained. Default to 0. |
required |
Source code in erniebot-agent/src/erniebot_agent/memory/sliding_window_memory.py
add_message(message)
¶
prune_message()
¶
Prune memory to max_round if necessary.