mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-09-06 08:16:00 +00:00
[dev] rename dummy worker to normal worker
This commit is contained in:
parent
9e78eb69fd
commit
835a9d5752
8 changed files with 68 additions and 22 deletions
|
|
@ -16,8 +16,7 @@ memory_service:
|
|||
read_memory_key: read_memory
|
||||
memory_operations:
|
||||
read_message:
|
||||
class: memory.operation.read_memory
|
||||
workflow: dummy_worker
|
||||
class: memory.operation.read_message
|
||||
description: "read session messages of the user"
|
||||
read_memory:
|
||||
class: memory.operation.read_memory
|
||||
|
|
@ -25,7 +24,7 @@ memory_service:
|
|||
description: "read related memories of the user"
|
||||
list_memory:
|
||||
class: memory.operation.read_memory
|
||||
workflow: set_query_worker,retrieve_store_worker,
|
||||
workflow: set_query_worker,retrieve_store_worker,print_memory_worker
|
||||
description: "read all memories of the user"
|
||||
write_memory:
|
||||
class: memory.operation.write_memory
|
||||
|
|
|
|||
|
|
@ -14,19 +14,17 @@ class ReadMemory(BaseWorkflow, BaseOperation):
|
|||
description: str,
|
||||
chat_messages: List[Message],
|
||||
his_msg_count: int = 0, # supplement to the current query
|
||||
contextual_msg_count: int = 0, # for the current context dialogue
|
||||
**kwargs):
|
||||
super().__init__(name=name, **kwargs)
|
||||
BaseOperation.__init__(self, name=name, description=description)
|
||||
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, **kwargs):
|
||||
max_count = 1 + max(self.his_msg_count, self.contextual_msg_count)
|
||||
max_count = 1 + self.his_msg_count
|
||||
self.context[CHAT_MESSAGES] = [x.copy(deep=True) for x in self.chat_messages[-max_count:]]
|
||||
self.context[CHAT_KWARGS] = kwargs
|
||||
self.run_workflow()
|
||||
|
|
|
|||
21
memory_scope/memory/operation/read_message.py
Normal file
21
memory_scope/memory/operation/read_message.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
from typing import List
|
||||
|
||||
from memory_scope.memory.operation.base_operation import BaseOperation, OPERATION_TYPE
|
||||
from memory_scope.scheme.message import Message
|
||||
|
||||
|
||||
class ReadMessage(BaseOperation):
|
||||
operation_type: OPERATION_TYPE = "frontend"
|
||||
|
||||
def __init__(self,
|
||||
name: str,
|
||||
description: str,
|
||||
chat_messages: List[Message],
|
||||
contextual_msg_count: int = 6, # for the current context dialogue
|
||||
**kwargs):
|
||||
super().__init__(name=name, description=description, **kwargs)
|
||||
self.chat_messages: List[Message] = chat_messages
|
||||
self.contextual_msg_count: int = contextual_msg_count
|
||||
|
||||
def run_operation(self, **kwargs):
|
||||
return self.chat_messages[-self.contextual_msg_count:]
|
||||
|
|
@ -35,6 +35,7 @@ class BaseWorker(metaclass=ABCMeta):
|
|||
def gather_async_result(self):
|
||||
if self.is_multi_thread:
|
||||
raise RuntimeError(f"async_task is not allowed in multi_thread condition")
|
||||
|
||||
async def async_gather():
|
||||
return await asyncio.gather(*[fn(*args, **kwargs) for fn, args, kwargs in self.task_list])
|
||||
|
||||
|
|
|
|||
41
memory_scope/memory/worker/read/print_memory_worker.py
Normal file
41
memory_scope/memory/worker/read/print_memory_worker.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
from typing import List
|
||||
|
||||
from memory_scope.constants.common_constants import RETRIEVE_MEMORY_NODES, RESULT
|
||||
from memory_scope.enumeration.memory_type_enum import MemoryTypeEnum
|
||||
from memory_scope.memory.worker.memory_base_worker import MemoryBaseWorker
|
||||
from memory_scope.scheme.memory_node import MemoryNode
|
||||
from memory_scope.utils.datetime_handler import DatetimeHandler
|
||||
|
||||
|
||||
class PrintMemoryWorker(MemoryBaseWorker):
|
||||
|
||||
def _run(self):
|
||||
memory_node_list: List[MemoryNode] = self.get_context(RETRIEVE_MEMORY_NODES)
|
||||
memory_node_list = sorted(memory_node_list, key=lambda x: x.timestamp, reverse=True)
|
||||
|
||||
obs_content_list: List[str] = []
|
||||
insight_content_list: List[str] = []
|
||||
for i, node in enumerate(memory_node_list):
|
||||
|
||||
if MemoryTypeEnum(node.memory_type) in [MemoryTypeEnum.OBSERVATION, MemoryTypeEnum.OBS_CUSTOMIZED]:
|
||||
dt_handler = DatetimeHandler(node.timestamp)
|
||||
dt = dt_handler.datetime_format("%Y%m%d %H:%M:%S")
|
||||
line = f" {i} {dt} {node.content}"
|
||||
obs_content_list.append(line)
|
||||
|
||||
elif MemoryTypeEnum(node.memory_type) in [MemoryTypeEnum.INSIGHT, ]:
|
||||
line = f" {i} {node.content}"
|
||||
insight_content_list.append(line)
|
||||
|
||||
obs_content = "\n".join(obs_content_list)
|
||||
insight_content = "\n".join(insight_content_list)
|
||||
result: str = f"""
|
||||
The memories of {self.user_name} about {self.target_name}.
|
||||
|
||||
observation:
|
||||
{obs_content}
|
||||
|
||||
insight:
|
||||
{insight_content}
|
||||
""".strip()
|
||||
self.set_context(RESULT, result)
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
from typing import List
|
||||
|
||||
from memory_scope.constants.common_constants import RETRIEVE_MEMORY_NODES
|
||||
from memory_scope.memory.worker.memory_base_worker import MemoryBaseWorker
|
||||
from memory_scope.scheme.memory_node import MemoryNode
|
||||
|
||||
|
||||
class ReadAllMemory(MemoryBaseWorker):
|
||||
|
||||
def _run(self):
|
||||
memory_node_list: List[MemoryNode] = self.get_context(RETRIEVE_MEMORY_NODES)
|
||||
memory_node_list = sorted(memory_node_list, key=lambda x: x.timestamp, reverse=True)
|
||||
obs_content_list: List[str] = []
|
||||
|
||||
|
|
@ -60,7 +60,7 @@ class GetReflectionSubjectWorker(MemoryBaseWorker):
|
|||
return
|
||||
|
||||
# parse text & save
|
||||
new_insight_keys = ResponseTextParser(response.message.content).parse_v2("get_reflection")
|
||||
new_insight_keys = ResponseTextParser(response.message.content).parse_v2(self.__class__.__name__)
|
||||
if new_insight_keys:
|
||||
for insight_key in new_insight_keys:
|
||||
insight_nodes.append(self.new_insight_node(insight_key))
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ def init_instance_by_config(config: dict,
|
|||
**kwargs: Additional keyword arguments to pass to the class constructor.
|
||||
|
||||
Returns:
|
||||
object: An instance of the class initialized with the provided config and kwargs.
|
||||
instance: An instance of the class initialized with the provided config and kwargs.
|
||||
"""
|
||||
|
||||
config_copy = deepcopy(config)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue