[feature] update the prompt and utils

This commit is contained in:
jinli.yl 2024-07-19 14:29:07 +08:00
commit 8c1facfc0b
9 changed files with 50 additions and 28 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -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> <None> <>
Thought: Sentence 6 expresses {user_name}'s fruit preference, liking to eat watermelon, which is information not present in any preceding sentences.
Judgment: <6> <None>
Judgment: <6> <None> <>
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> <None>
Judgment: <7> <None> <>
Example 2
Sentences:

View file

@ -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 = []

View file

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

View file

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

View file

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