[dev] add memory service

This commit is contained in:
jinli.yl 2024-06-26 15:47:35 +08:00
parent d815ba988a
commit 08e15f2848
10 changed files with 105 additions and 29 deletions

View file

@ -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:

View file

@ -1,6 +0,0 @@
from abc import ABCMeta
class BaseMemoryService(metaclass=ABCMeta):
def __init__(self, **kwargs):
self.kwargs = kwargs

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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()

View file

View file

@ -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

View file

@ -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()

View file

@ -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!")