diff --git a/config/config.yaml b/config/config.yaml index 7b99b1a3..81a51df6 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -11,21 +11,25 @@ memory_chat: memory_service: memory_chat_service: class: memory.base_memory_service - history_msg_count: 10 + history_msg_count: 32 + contextual_msg_count: 6 memory_operations: + read_user_message: + class: memory.operation.read_memory + workflow: dummy read_memory: - class: memory.operation.read_operation + class: memory.operation.read_memory workflow: dummy list_memory: - class: memory.operation.read_operation + class: memory.operation.read_memory workflow: dummy write_memory: - class: memory.operation.write_operation + class: memory.operation.write_memory workflow: dummy interval_time: 60 contextual_msg_count: 6 summary_memory: - class: memory.operation.summary_operation + class: memory.operation.summary_memory workflow: dummy interval_time: 300 models: diff --git a/memory_scope/memory/operation/read_operation.py b/memory_scope/memory/operation/read_memory.py similarity index 78% rename from memory_scope/memory/operation/read_operation.py rename to memory_scope/memory/operation/read_memory.py index 485a8f8c..8ffe24d2 100644 --- a/memory_scope/memory/operation/read_operation.py +++ b/memory_scope/memory/operation/read_memory.py @@ -6,19 +6,20 @@ from memory_scope.memory.operation.base_workflow import BaseWorkflow from memory_scope.scheme.message import Message -class ReadOperation(BaseWorkflow, BaseOperation): +class ReadMemory(BaseWorkflow, BaseOperation): operation_type: OPERATION_TYPE = "frontend" - def __init__(self, chat_messages: List[Message], his_msg_count: int = 0, **kwargs): + def __init__(self, chat_messages: List[Message], his_msg_count: int = 0, contextual_msg_count: int = 0, **kwargs): super().__init__(**kwargs) self.chat_messages: List[Message] = chat_messages self.his_msg_count: int = his_msg_count + self.contextual_msg_count: int = contextual_msg_count def init_workflow(self): self.init_workers() def run_operation(self): - max_count = 1 + self.his_msg_count + max_count = 1 + max(self.his_msg_count, self.contextual_msg_count) self.context[CHAT_MESSAGES] = [x.copy() for x in self.chat_messages[-max_count:]] self.run_workflow() result = self.context.get(RESULT) diff --git a/memory_scope/memory/operation/summary_operation.py b/memory_scope/memory/operation/summary_memory.py similarity index 89% rename from memory_scope/memory/operation/summary_operation.py rename to memory_scope/memory/operation/summary_memory.py index 1f36b679..3deba4ba 100644 --- a/memory_scope/memory/operation/summary_operation.py +++ b/memory_scope/memory/operation/summary_memory.py @@ -1,12 +1,11 @@ import time -from memory_scope.memory.base_workflow import BaseWorkflow - from memory_scope.chat_v2.global_context import G_CONTEXT from memory_scope.memory.operation.base_operation import BaseOperation, OPERATION_TYPE +from memory_scope.memory.operation.base_workflow import BaseWorkflow -class SummaryOperation(BaseWorkflow, BaseOperation): +class SummaryMemory(BaseWorkflow, BaseOperation): operation_type: OPERATION_TYPE = "backend" def __init__(self, interval_time: int = 300, **kwargs): diff --git a/memory_scope/memory/operation/write_operation.py b/memory_scope/memory/operation/write_memory.py similarity index 97% rename from memory_scope/memory/operation/write_operation.py rename to memory_scope/memory/operation/write_memory.py index 7523c969..9d6dce17 100644 --- a/memory_scope/memory/operation/write_operation.py +++ b/memory_scope/memory/operation/write_memory.py @@ -8,7 +8,7 @@ from memory_scope.memory.operation.base_workflow import BaseWorkflow from memory_scope.scheme.message import Message -class WriteOperation(BaseOperation, BaseWorkflow): +class WriteMemory(BaseOperation, BaseWorkflow): operation_type: OPERATION_TYPE = "backend" def __init__(self, diff --git a/memory_scope/memory/service/base_memory_service.py b/memory_scope/memory/service/base_memory_service.py index b74fa4e6..79299265 100644 --- a/memory_scope/memory/service/base_memory_service.py +++ b/memory_scope/memory/service/base_memory_service.py @@ -9,5 +9,5 @@ class BaseMemoryService(metaclass=ABCMeta): self.kwargs = kwargs @abstractmethod - def get_short_memory(self): + def do_operation(self, op_name: str): pass diff --git a/memory_scope/memory/service/chat_memory_service.py b/memory_scope/memory/service/chat_memory_service.py index 5fde8502..f822c0dd 100644 --- a/memory_scope/memory/service/chat_memory_service.py +++ b/memory_scope/memory/service/chat_memory_service.py @@ -19,6 +19,7 @@ class ChatMemoryService(BaseMemoryService): self.op_dict: Dict[str, BaseOperation] = self._init_operation(memory_operations) self.history_msg_count: int = history_msg_count self.contextual_msg_count: int = contextual_msg_count + assert self.history_msg_count >= self.contextual_msg_count self.chat_messages: List[Message] = [] self.message_lock = threading.Lock @@ -36,7 +37,10 @@ class ChatMemoryService(BaseMemoryService): contextual_msg_count=self.contextual_msg_count) return op_dict - def submit_message(self, messages: List[Message]): + def submit_messages(self, messages: List[Message] | Message): + if isinstance(messages, Message): + messages = [messages] + messages = sorted(messages, key=lambda x: x.time_created) self.chat_messages.extend(messages) if len(self.chat_messages) > self.history_msg_count: @@ -54,6 +58,4 @@ class ChatMemoryService(BaseMemoryService): if op_name not in self.op_dict: self.logger.warning(f"op_name={op_name} is not inited!") return - - operation = self.op_dict[op_name] - return operation.run_operation() + return self.op_dict[op_name].run_operation()