forked from autogenhub/autogen
-
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.
Initial chat memory implementation (autogenhub#59)
- Loading branch information
Showing
7 changed files
with
107 additions
and
24 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
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,5 @@ | ||
from ._base import ChatMemory | ||
from ._buffered import BufferedChatMemory | ||
from ._full import FullChatMemory | ||
|
||
__all__ = ["ChatMemory", "FullChatMemory", "BufferedChatMemory"] |
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,15 @@ | ||
from typing import Any, List, Mapping, Protocol | ||
|
||
from ..types import Message | ||
|
||
|
||
class ChatMemory(Protocol): | ||
def add_message(self, message: Message) -> None: ... | ||
|
||
def get_messages(self) -> List[Message]: ... | ||
|
||
def clear(self) -> None: ... | ||
|
||
def save_state(self) -> Mapping[str, Any]: ... | ||
|
||
def load_state(self, state: Mapping[str, Any]) -> None: ... |
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,29 @@ | ||
from typing import Any, List, Mapping | ||
|
||
from ..types import Message | ||
from ._base import ChatMemory | ||
|
||
|
||
class BufferedChatMemory(ChatMemory): | ||
def __init__(self, buffer_size: int) -> None: | ||
self._messages: List[Message] = [] | ||
self._buffer_size = buffer_size | ||
|
||
def add_message(self, message: Message) -> None: | ||
self._messages.append(message) | ||
|
||
def get_messages(self) -> List[Message]: | ||
return self._messages[-self._buffer_size :] | ||
|
||
def clear(self) -> None: | ||
self._messages = [] | ||
|
||
def save_state(self) -> Mapping[str, Any]: | ||
return { | ||
"messages": [message for message in self._messages], | ||
"buffer_size": self._buffer_size, | ||
} | ||
|
||
def load_state(self, state: Mapping[str, Any]) -> None: | ||
self._messages = state["messages"] | ||
self._buffer_size = state["buffer_size"] |
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,24 @@ | ||
from typing import Any, List, Mapping | ||
|
||
from ..types import Message | ||
from ._base import ChatMemory | ||
|
||
|
||
class FullChatMemory(ChatMemory): | ||
def __init__(self) -> None: | ||
self._messages: List[Message] = [] | ||
|
||
def add_message(self, message: Message) -> None: | ||
self._messages.append(message) | ||
|
||
def get_messages(self) -> List[Message]: | ||
return self._messages | ||
|
||
def clear(self) -> None: | ||
self._messages = [] | ||
|
||
def save_state(self) -> Mapping[str, Any]: | ||
return {"messages": [message for message in self._messages]} | ||
|
||
def load_state(self, state: Mapping[str, Any]) -> None: | ||
self._messages = state["messages"] |