-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
195 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
from .chat_memory import ChatMemory, ChatMemoryEntry, DEFAULT_WINDOW_SIZE | ||
from .chat_memory import ( | ||
ChatMemory, | ||
ChatMemoryEntry, | ||
CHAT_MEMORY_DEFAULT_WINDOW_SIZE, | ||
) | ||
from .external_memory import ExternalMemory, ExternalMemoryLoadingType | ||
|
||
__all__ = ["ChatMemory", "ChatMemoryEntry", "DEFAULT_WINDOW_SIZE"] | ||
from .base_memory import MemoryType | ||
|
||
__all__ = [ | ||
"ChatMemory", | ||
"ChatMemoryEntry", | ||
"CHAT_MEMORY_DEFAULT_WINDOW_SIZE", | ||
"ExternalMemory", | ||
"ExternalMemoryLoadingType", | ||
"MemoryType", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from enum import Enum | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class MemoryType(Enum): | ||
"""The type of memory used by the model.""" | ||
|
||
CHAT = "chat" | ||
EXTERNAL = "external" | ||
|
||
|
||
class BaseMemory(ABC): | ||
"""Abstract representation of a model's memory.""" | ||
|
||
def __init__(self, memory_type: MemoryType) -> None: | ||
"""Create a new BaseMemory object. | ||
Args: | ||
memory_type (MemoryType): The type of memory managed by the model. | ||
""" | ||
self.memory_type = memory_type | ||
|
||
@abstractmethod | ||
def clear(self) -> None: | ||
"""Clears the model's memory.""" | ||
|
||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from enum import Enum | ||
|
||
from l2m2.memory.base_memory import BaseMemory, MemoryType | ||
|
||
|
||
class ExternalMemoryLoadingType(Enum): | ||
"""Represents how the model should load external memory.""" | ||
|
||
SYSTEM_PROMPT_APPEND = "system_prompt_append" | ||
USER_PROMPT_APPEND = "user_prompt_append" | ||
|
||
|
||
class ExternalMemory(BaseMemory): | ||
"""Represents custom memory that is managed completely externally to the model.""" | ||
|
||
def __init__( | ||
self, | ||
contents: str = "", | ||
loading_type: ExternalMemoryLoadingType = ExternalMemoryLoadingType.SYSTEM_PROMPT_APPEND, | ||
) -> None: | ||
"""Create a new ExternalMemory object. | ||
Args: | ||
contents (str, optional): The memory to pre-load. Defaults to "". | ||
loading_type (LoadingType, optional): How the model should load the memory – | ||
either in the system prompt, inserted as a user prompt, or appended to the | ||
most recent user prompt. Defaults to LoadingType.SYSTEM_PROMPT. | ||
""" | ||
|
||
super().__init__(MemoryType.EXTERNAL) | ||
self.contents: str = contents | ||
self.loading_type: ExternalMemoryLoadingType = loading_type | ||
|
||
def get_contents(self) -> str: | ||
"""Get the contents of the memory object. | ||
Returns: | ||
str: The entire memory contents | ||
""" | ||
|
||
return self.contents | ||
|
||
def set_contents(self, new_contents: str) -> None: | ||
"""Load the contents into the memory object, replacing the existing contents. | ||
Args: | ||
new_contents (str): The new contents to load. | ||
""" | ||
self.contents = new_contents | ||
|
||
def append_contents(self, new_contents: str) -> None: | ||
"""Append new contents to the memory object. | ||
Args: | ||
new_contents (str): The new contents to append. | ||
""" | ||
self.contents += new_contents | ||
|
||
def clear(self) -> None: | ||
"""Clear the memory.""" | ||
self.contents = "" |
Oops, something went wrong.