diff --git a/config/demo_config_cn.yaml b/config/demo_config_cn.yaml index 9b639ade..331f3494 100644 --- a/config/demo_config_cn.yaml +++ b/config/demo_config_cn.yaml @@ -143,6 +143,7 @@ worker: get_reflection_subject: class: memory.worker.summary.get_reflection_subject_worker generation_model: dashscope_generation + reflect_obs_cnt_threshold: 10 generation_model_kwargs: top_k: 1 update_insight: diff --git a/config/demo_config_en.yaml b/config/demo_config_en.yaml index 36cc3b93..f84c3e37 100644 --- a/config/demo_config_en.yaml +++ b/config/demo_config_en.yaml @@ -143,6 +143,7 @@ worker: get_reflection_subject: class: memory.worker.summary.get_reflection_subject_worker generation_model: dashscope_generation + reflect_obs_cnt_threshold: 10 generation_model_kwargs: top_k: 1 update_insight: diff --git a/memory_scope/memory/worker/frontend/extract_time_worker.py b/memory_scope/memory/worker/frontend/extract_time_worker.py index ef93e726..76189925 100644 --- a/memory_scope/memory/worker/frontend/extract_time_worker.py +++ b/memory_scope/memory/worker/frontend/extract_time_worker.py @@ -41,7 +41,7 @@ class ExtractTimeWorker(MemoryBaseWorker): # Prepare the prompt with necessary contextual details query_time_str = DatetimeHandler(dt=query_timestamp).string_format(self.prompt_handler.time_string_format) system_prompt = self.prompt_handler.extract_time_system - few_shot = self.prompt_handler.extract_time_few_shot.format(user_name=self.target_name) + few_shot = self.prompt_handler.extract_time_few_shot user_query = self.prompt_handler.extract_time_user_query.format(query=query, query_time_str=query_time_str) extract_time_message = prompt_to_msg(system_prompt=system_prompt, few_shot=few_shot, user_query=user_query) self.logger.info(f"extract_time_message={extract_time_message}") diff --git a/memory_scope/memory/worker/frontend/print_memory_worker.py b/memory_scope/memory/worker/frontend/print_memory_worker.py index 228042eb..943e323a 100644 --- a/memory_scope/memory/worker/frontend/print_memory_worker.py +++ b/memory_scope/memory/worker/frontend/print_memory_worker.py @@ -7,6 +7,18 @@ 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 +PRINT_TEMPLATE = """ +The memories of {user_name} about {target_name}. + +{obs_content} + + +{insight_content} + + +{expired_content} +""" + class PrintMemoryWorker(MemoryBaseWorker): @@ -44,15 +56,11 @@ class PrintMemoryWorker(MemoryBaseWorker): obs_content = "\n".join(obs_content_list) insight_content = "\n".join(insight_content_list) expired_content = "\n".join(expired_content_list) - result: str = f""" -The memories of {self.user_name} about {self.target_name}. - -{obs_content} - - -{insight_content} - - -{expired_content} - """.strip() + result: str = PRINT_TEMPLATE.format( + user_name=self.user_name, + target_name=self.target_name, + obs_content=obs_content, + insight_content=insight_content, + expired_content=expired_content, + ).strip() self.set_context(RESULT, result) diff --git a/memory_scope/memory/worker/summary/long_contra_repeat_worker.yaml b/memory_scope/memory/worker/summary/long_contra_repeat_worker.yaml index 4d41123f..ef318358 100644 --- a/memory_scope/memory/worker/summary/long_contra_repeat_worker.yaml +++ b/memory_scope/memory/worker/summary/long_contra_repeat_worker.yaml @@ -39,9 +39,9 @@ long_contra_repeat_few_shot: 思考:第5句中陈伟业是{user_name}的领导的信息被前面序号中第3句的信息包含,但新增了陈伟业是银行分行行长的信息,故不是被完全包含。 判断:<5> <无> <> 思考:第6句中表达了{user_name}的水果偏好,喜欢吃西瓜,信息没有在前面序号句子中出现。 - 判断:<6> <无> + 判断:<6> <无> <> 思考:第7句也表达了{user_name}的水果偏好,喜欢吃桃子,和前面序号中的第6句不冲突,喜好可以同时存在。 - 判断:<7> <无> + 判断:<7> <无> <> 示例2 句子: @@ -87,9 +87,9 @@ long_contra_repeat_few_shot: Thought: The information that Charles is {user_name}'s supervisor in the fifth sentence is contained within the information of the third sentence, but the new information that Charles is the branch manager of a bank is not, so it is not contained. Judgment: <5> <> Thought: Sentence 6 expresses {user_name}'s fruit preference, liking to eat watermelon, which is information not present in any preceding sentences. - Judgment: <6> + Judgment: <6> <> Thought: Sentence 7 also expresses {user_name}'s fruit preference, liking to eat apples; it does not conflict with sentence 6, and both preferences can coexist. - Judgment: <7> + Judgment: <7> <> Example 2 Sentences: diff --git a/memory_scope/memory/worker/summary/update_insight_worker.py b/memory_scope/memory/worker/summary/update_insight_worker.py index 2e6f7777..7fc0e976 100644 --- a/memory_scope/memory/worker/summary/update_insight_worker.py +++ b/memory_scope/memory/worker/summary/update_insight_worker.py @@ -166,14 +166,14 @@ class UpdateInsightWorker(MemoryBaseWorker): # Process active insight nodes with corresponding not updated nodes for node in insight_nodes: - if node.action_status == ActionStatusEnum.NONE.value: - self.submit_thread_task(fn=self.filter_obs_nodes, - insight_node=node, - obs_nodes=not_updated_nodes) - else: + if node.action_status == ActionStatusEnum.NEW.value: self.submit_thread_task(fn=self.filter_obs_nodes, insight_node=node, obs_nodes=not_reflected_nodes) + else: + self.submit_thread_task(fn=self.filter_obs_nodes, + insight_node=node, + obs_nodes=not_updated_nodes) # select top n result_list = [] diff --git a/memory_scope/memory/worker/write/contra_repeat_worker.py b/memory_scope/memory/worker/write/contra_repeat_worker.py index da5d74fb..c312ea34 100644 --- a/memory_scope/memory/worker/write/contra_repeat_worker.py +++ b/memory_scope/memory/worker/write/contra_repeat_worker.py @@ -63,8 +63,7 @@ class ContraRepeatWorker(MemoryBaseWorker): system_prompt = self.prompt_handler.contra_repeat_system.format(num_obs=len(user_query_list), user_name=self.target_name) few_shot = self.prompt_handler.contra_repeat_few_shot.format(user_name=self.target_name) - user_query = self.prompt_handler.contra_repeat_user_query.format(user_query="\n".join(user_query_list), - user_name=self.target_name) + user_query = self.prompt_handler.contra_repeat_user_query.format(user_query="\n".join(user_query_list)) contra_repeat_message = prompt_to_msg(system_prompt=system_prompt, few_shot=few_shot, user_query=user_query) self.logger.info(f"contra_repeat_message={contra_repeat_message}") diff --git a/memory_scope/memory/worker/write/info_filter_worker.py b/memory_scope/memory/worker/write/info_filter_worker.py index 7ef811dd..751c7edb 100644 --- a/memory_scope/memory/worker/write/info_filter_worker.py +++ b/memory_scope/memory/worker/write/info_filter_worker.py @@ -63,8 +63,7 @@ class InfoFilterWorker(MemoryBaseWorker): system_prompt = self.prompt_handler.info_filter_system.format(batch_size=len(info_messages), user_name=self.target_name) few_shot = self.prompt_handler.info_filter_few_shot.format(user_name=self.target_name) - user_query = self.prompt_handler.info_filter_user_query.format(user_query="\n".join(user_query_list), - user_name=self.target_name) + user_query = self.prompt_handler.info_filter_user_query.format(user_query="\n".join(user_query_list)) info_filter_message = prompt_to_msg(system_prompt=system_prompt, few_shot=few_shot, user_query=user_query) self.logger.info(f"info_filter_message={info_filter_message}") diff --git a/memory_scope/utils/tool_functions.py b/memory_scope/utils/tool_functions.py index 20403ece..ca41abfc 100644 --- a/memory_scope/utils/tool_functions.py +++ b/memory_scope/utils/tool_functions.py @@ -87,7 +87,7 @@ def init_instance_by_config(config: dict, return getattr(module, cls_name)(**config_copy) -def prompt_to_msg(system_prompt: str, few_shot: str, user_query: str) -> List[Message]: +def prompt_to_msg(system_prompt: str, few_shot: str, user_query: str, concat_system_prompt: bool = True) -> List[Message]: """ Converts input strings into a structured list of message objects suitable for AI interactions. @@ -95,14 +95,28 @@ def prompt_to_msg(system_prompt: str, few_shot: str, user_query: str) -> List[Me system_prompt (str): The system-level instruction or context. few_shot (str): An example or demonstration input, often used for illustrating expected behavior. user_query (str): The actual user query or prompt to be processed. + concat_system_prompt(bool): Concat system prompt again or not in the user message. + A simple method to improve the effectiveness for some LLMs. Returns: List[Message]: A list of Message objects, each representing a part of the conversation setup. """ + if concat_system_prompt: + user_message = Message( + role=MessageRoleEnum.USER.value, + content="\n".join( + [x.strip() for x in [few_shot, system_prompt, user_query]] + ), + ) + else: + user_message = Message( + role=MessageRoleEnum.USER.value, + content="\n".join([x.strip() for x in [few_shot, user_query]]), + ) + return [ Message(role=MessageRoleEnum.SYSTEM.value, content=system_prompt.strip()), # System message - Message(role=MessageRoleEnum.USER.value, - content="\n".join([x.strip() for x in [few_shot, system_prompt, user_query]])) + user_message # User message combining few shot, system prompt, and user query ]