Memory¶
Memory
¶
Bases: MemoryMeta
Concrete implementation of MemoryMeta for storing and managing conversational memory. Memory is persisted in a JSON Lines file, with support for user-specific data and graph-based representations.
Methods:
Name | Description |
---|---|
save_memory |
Save a list of memory entries for a specific user to the memory file. |
load_all_memory |
Load all memory data from the memory file. |
load_memory_by_user |
Load memory data for a specific user from the memory file. |
save_short_term_memory |
Convert a message to a graph using a language model and update the user's memory. |
update_memory |
Update the user's memory by adding or updating graph entries, avoiding duplicates. |
save_memory
¶
load_all_memory
¶
Load all memory data from the memory file.
Returns:
Name | Type | Description |
---|---|---|
dict |
The entire memory data as a dictionary, with user IDs as keys and lists of memory entries as values. |
load_memory_by_user
¶
Load memory data for a specific user from the memory file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
load_type
|
Literal['list', 'string']
|
Format of the returned data ("list" or "string"). Defaults to "list". |
'list'
|
user_id
|
str
|
The user identifier. Defaults to "unknown_user". |
'unknown_user'
|
Returns:
Type | Description |
---|---|
Union[List[dict], str]: List of memory entries if load_type is "list", or a string representation if "string". |
save_short_term_memory
¶
save_short_term_memory(llm: Union[ChatTogether, BaseLanguageModel, BaseChatOpenAI], message: str, user_id: str = 'unknown_user', *args, **kwargs)
Convert a message to a graph using a language model and update the user's memory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
Union[ChatTogether, BaseLanguageModel, BaseChatOpenAI]
|
Language model for graph generation. |
required |
message
|
str
|
The message to convert and store. |
required |
user_id
|
str
|
The user identifier. Defaults to "unknown_user". |
'unknown_user'
|
*args,
|
**kwargs
|
Additional arguments for flexibility. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
The generated graph representation of the message. |
update_memory
¶
Update the user's memory by adding or updating graph entries, avoiding duplicates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
list
|
List of graph entries, each with head, head_type, relation, relation_properties, tail, and tail_type. |
required |
user_id
|
str
|
The user identifier. Defaults to "unknown_user". |
'unknown_user'
|
Returns:
Name | Type | Description |
---|---|---|
list |
The updated list of memory entries for the user. |