diff --git a/config/config.yaml b/config/config.yaml index c0279fc7..7b99b1a3 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -11,26 +11,22 @@ memory_chat: memory_service: memory_chat_service: class: memory.base_memory_service - history_msg_count: 5 + history_msg_count: 10 memory_operations: read_memory: - class: memory.workflow.base_workflow - workflow: parse_params,load_profile,extract_time,es_similar,es_keyword,semantic_rank,fuse_rerank - work_type: frontend + class: memory.operation.read_operation + workflow: dummy list_memory: - class: memory.workflow.base_workflow + class: memory.operation.read_operation workflow: dummy - work_type: frontend - extract_memory: - class: memory.workflow.base_workflow + write_memory: + class: memory.operation.write_operation workflow: dummy - work_type: backend interval_time: 60 - min_count: 5 - reflect_memory: - class: memory.workflow.base_workflow + contextual_msg_count: 6 + summary_memory: + class: memory.operation.summary_operation workflow: dummy - work_type: backend interval_time: 300 models: dashscope_generation: diff --git a/memory_scope/memory/base_memory_service.py b/memory_scope/memory/base_memory_service.py deleted file mode 100644 index d05652a3..00000000 --- a/memory_scope/memory/base_memory_service.py +++ /dev/null @@ -1,6 +0,0 @@ -from abc import ABCMeta - - -class BaseMemoryService(metaclass=ABCMeta): - def __init__(self, **kwargs): - self.kwargs = kwargs diff --git a/memory_scope/memory/operation/base_operation.py b/memory_scope/memory/operation/base_operation.py index 26a9b9ca..70d1d103 100644 --- a/memory_scope/memory/operation/base_operation.py +++ b/memory_scope/memory/operation/base_operation.py @@ -7,6 +7,9 @@ OPERATION_TYPE = Literal["frontend", "backend"] class BaseOperation(metaclass=ABCMeta): operation_type: OPERATION_TYPE = "frontend" + def init_workflow(self): + pass + @abstractmethod def run_operation(self): raise NotImplementedError diff --git a/memory_scope/memory/operation/read_operation.py b/memory_scope/memory/operation/read_operation.py index 9f8f294f..485a8f8c 100644 --- a/memory_scope/memory/operation/read_operation.py +++ b/memory_scope/memory/operation/read_operation.py @@ -9,13 +9,16 @@ from memory_scope.scheme.message import Message class ReadOperation(BaseWorkflow, BaseOperation): operation_type: OPERATION_TYPE = "frontend" - def __init__(self, chat_messages: List[Message], max_his_msg_count: int = 0, **kwargs): + def __init__(self, chat_messages: List[Message], his_msg_count: int = 0, **kwargs): super().__init__(**kwargs) self.chat_messages: List[Message] = chat_messages - self.max_his_msg_count: int = max_his_msg_count + self.his_msg_count: int = his_msg_count + + def init_workflow(self): + self.init_workers() def run_operation(self): - max_count = 1 + self.max_his_msg_count + max_count = 1 + self.his_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_operation.py index 68d98c41..1f36b679 100644 --- a/memory_scope/memory/operation/summary_operation.py +++ b/memory_scope/memory/operation/summary_operation.py @@ -17,6 +17,9 @@ class SummaryOperation(BaseWorkflow, BaseOperation): self._operation_status_run: bool = False self._loop_switch: bool = False + def init_workflow(self): + self.init_workers() + def run_operation(self): if self._operation_status_run: return diff --git a/memory_scope/memory/operation/write_operation.py b/memory_scope/memory/operation/write_operation.py index efd5d4d1..7523c969 100644 --- a/memory_scope/memory/operation/write_operation.py +++ b/memory_scope/memory/operation/write_operation.py @@ -13,18 +13,18 @@ class WriteOperation(BaseOperation, BaseWorkflow): def __init__(self, chat_messages: List[Message], - max_his_msg_count: int = 0, + his_msg_count: int = 0, message_lock=None, interval_time: int = 60, - min_count: int = 5, + contextual_msg_count: int = 6, **kwargs): super().__init__(**kwargs) self.chat_messages: List[Message] = chat_messages - self.max_his_msg_count: int = max_his_msg_count + self.his_msg_count: int = his_msg_count self.message_lock = message_lock self.interval_time: int = interval_time - self.min_count: int = min_count + self.contextual_msg_count: int = contextual_msg_count self._operation_status_run: bool = False self._loop_switch: bool = False @@ -39,16 +39,19 @@ class WriteOperation(BaseOperation, BaseWorkflow): for msg in self.chat_messages: msg.memorized = True + def init_workflow(self): + self.init_workers() + def run_operation(self): if self._operation_status_run: return self._operation_status_run = True not_memorized_size = self.not_memorized_size - if not_memorized_size < self.min_count: + if not_memorized_size < self.contextual_msg_count: return - max_count = not_memorized_size + self.max_his_msg_count + max_count = not_memorized_size + self.his_msg_count self.context[CHAT_MESSAGES] = [x.copy() for x in self.chat_messages[-max_count:]] self.run_workflow() self.context.clear() diff --git a/memory_scope/memory/service/__init__.py b/memory_scope/memory/service/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/memory_scope/memory/service/base_memory_service.py b/memory_scope/memory/service/base_memory_service.py new file mode 100644 index 00000000..b74fa4e6 --- /dev/null +++ b/memory_scope/memory/service/base_memory_service.py @@ -0,0 +1,13 @@ +from abc import ABCMeta, abstractmethod + +from memory_scope.utils.logger import Logger + + +class BaseMemoryService(metaclass=ABCMeta): + def __init__(self, **kwargs): + self.logger = Logger.get_logger() + self.kwargs = kwargs + + @abstractmethod + def get_short_memory(self): + pass diff --git a/memory_scope/memory/service/chat_memory_service.py b/memory_scope/memory/service/chat_memory_service.py new file mode 100644 index 00000000..5fde8502 --- /dev/null +++ b/memory_scope/memory/service/chat_memory_service.py @@ -0,0 +1,59 @@ +import threading +from typing import List, Dict + +from memory_scope.memory.operation.base_operation import BaseOperation +from memory_scope.memory.service.base_memory_service import BaseMemoryService +from memory_scope.scheme.message import Message +from memory_scope.utils.tool_functions import init_instance_by_config + + +class ChatMemoryService(BaseMemoryService): + + def __init__(self, + memory_operations: Dict[str, dict], + history_msg_count: int = 32, + contextual_msg_count: int = 6, + **kwargs): + super().__init__(**kwargs) + + 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 + + self.chat_messages: List[Message] = [] + self.message_lock = threading.Lock + + def _init_operation(self, memory_operations: Dict[str, dict]): + op_dict: Dict[str, BaseOperation] = {} + for name, operation_config in memory_operations.items(): + if name in self.op_dict: + self.logger.warning(f"memory operation={name} is repeated!") + continue + self.op_dict[name] = init_instance_by_config(config=operation_config, + name=name, + chat_messages=self.chat_messages, + message_lock=self.message_lock, + contextual_msg_count=self.contextual_msg_count) + return op_dict + + def submit_message(self, messages: List[Message]): + messages = sorted(messages, key=lambda x: x.time_created) + self.chat_messages.extend(messages) + if len(self.chat_messages) > self.history_msg_count: + gap_size = len(self.chat_messages) - self.history_msg_count + for _ in range(gap_size): + self.chat_messages.pop(0) + + def prepare_service(self): + for _, operation in self.op_dict.items(): + operation.init_workflow() + if operation.operation_type == "backend": + operation.run_operation_backend() + + def do_operation(self, op_name: str): + 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() diff --git a/memory_scope/memory/worker/dummy_worker.py b/memory_scope/memory/worker/dummy_worker.py index 6b446b0c..9513bc79 100644 --- a/memory_scope/memory/worker/dummy_worker.py +++ b/memory_scope/memory/worker/dummy_worker.py @@ -1,6 +1,8 @@ +from memory_scope.constants.common_constants import RESULT from memory_scope.memory.worker.base_worker import BaseWorker class DummyWorker(BaseWorker): def _run(self): - self.logger.info("enter dummy worker!") + self.set_context(RESULT, ["test 123"]) + self.logger.info("enter dummy worker!") \ No newline at end of file