From d957aef2023bc7f85da8330a995a04a2677e2abd Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Mon, 2 Jun 2025 18:19:16 +0800 Subject: [PATCH] =?UTF-8?q?better=EF=BC=9A=E4=BC=98=E5=8C=96prompt?= =?UTF-8?q?=E4=B8=AD=E6=97=B6=E9=97=B4=E7=9A=84=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../focus_chat/planners/planner_simple.py | 10 +- .../focus_chat/replyer/default_replyer.py | 2 +- .../observation/chatting_observation.py | 4 +- src/chat/knowledge/knowledge_lib.py | 8 +- src/chat/utils/chat_message_builder.py | 4 +- src/chat/utils/utils.py | 111 +----------------- 6 files changed, 21 insertions(+), 118 deletions(-) diff --git a/src/chat/focus_chat/planners/planner_simple.py b/src/chat/focus_chat/planners/planner_simple.py index 09d5a2ed4..a40c07f4d 100644 --- a/src/chat/focus_chat/planners/planner_simple.py +++ b/src/chat/focus_chat/planners/planner_simple.py @@ -17,6 +17,7 @@ from src.individuality.individuality import individuality from src.chat.focus_chat.planners.action_manager import ActionManager from json_repair import repair_json from src.chat.focus_chat.planners.base_planner import BasePlanner +from datetime import datetime logger = get_logger("planner") @@ -31,6 +32,8 @@ def init_prompt(): {extra_info_block} {memory_str} +{time_block} + 你是群内的一员,你现在正在参与群内的闲聊,以下是群内的聊天内容: {chat_content_block} @@ -76,7 +79,8 @@ def init_prompt(): 动作名称:{action_name} 描述:{action_description} {action_parameters} - 使用该动作的场景:{action_require}""", + 使用该动作的场景: + {action_require}""", "action_prompt", ) @@ -372,11 +376,15 @@ class ActionPlanner(BasePlanner): # moderation_prompt_block = "请不要输出违法违规内容,不要输出色情,暴力,政治相关内容,如有敏感内容,请规避。" moderation_prompt_block = "" + + # 获取当前时间 + time_block = f"当前时间:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}" planner_prompt_template = await global_prompt_manager.get_prompt_async("planner_prompt") prompt = planner_prompt_template.format( self_info_block=self_info_block, memory_str=memory_str, + time_block=time_block, # bot_name=global_config.bot.nickname, prompt_personality=personality_block, chat_context_description=chat_context_description, diff --git a/src/chat/focus_chat/replyer/default_replyer.py b/src/chat/focus_chat/replyer/default_replyer.py index 26bdefbc6..02adc6869 100644 --- a/src/chat/focus_chat/replyer/default_replyer.py +++ b/src/chat/focus_chat/replyer/default_replyer.py @@ -293,7 +293,7 @@ class DefaultReplyer: message_list_before_now, replace_bot_name=True, merge_messages=True, - timestamp_mode="relative", + timestamp_mode="normal_no_YMD", read_mark=0.0, truncate=True, ) diff --git a/src/chat/heart_flow/observation/chatting_observation.py b/src/chat/heart_flow/observation/chatting_observation.py index 6fd180af5..c57e3a49c 100644 --- a/src/chat/heart_flow/observation/chatting_observation.py +++ b/src/chat/heart_flow/observation/chatting_observation.py @@ -227,7 +227,7 @@ class ChattingObservation(Observation): # print(f"压缩中:oldest_messages: {oldest_messages}") oldest_messages_str = await build_readable_messages( - messages=oldest_messages, timestamp_mode="normal", read_mark=0 + messages=oldest_messages, timestamp_mode="normal_no_YMD", read_mark=0 ) # --- Build prompt using template --- @@ -278,7 +278,7 @@ class ChattingObservation(Observation): # print(f"构建中:self.talking_message_str: {self.talking_message_str}") self.talking_message_str_truncate = await build_readable_messages( messages=self.talking_message, - timestamp_mode="normal", + timestamp_mode="normal_no_YMD", read_mark=last_obs_time_mark, truncate=True, ) diff --git a/src/chat/knowledge/knowledge_lib.py b/src/chat/knowledge/knowledge_lib.py index df82970a7..2fef6483b 100644 --- a/src/chat/knowledge/knowledge_lib.py +++ b/src/chat/knowledge/knowledge_lib.py @@ -25,8 +25,8 @@ logger.info("正在从文件加载Embedding库") try: embed_manager.load_from_file() except Exception as e: - logger.error("从文件加载Embedding库时发生错误:{}".format(e)) - logger.error("如果你是第一次导入知识,或者还未导入知识,请忽略此错误") + logger.warning("从文件加载Embedding库时,此问题不会影响正常使用:{}".format(e)) + # logger.warning("如果你是第一次导入知识,或者还未导入知识,请忽略此错误") logger.info("Embedding库加载完成") # 初始化KG kg_manager = KGManager() @@ -34,8 +34,8 @@ logger.info("正在从文件加载KG") try: kg_manager.load_from_file() except Exception as e: - logger.error("从文件加载KG时发生错误:{}".format(e)) - logger.error("如果你是第一次导入知识,或者还未导入知识,请忽略此错误") + logger.warning("从文件加载KG时,此问题不会影响正常使用:{}".format(e)) + # logger.warning("如果你是第一次导入知识,或者还未导入知识,请忽略此错误") logger.info("KG加载完成") logger.info(f"KG节点数量:{len(kg_manager.graph.get_node_list())}") diff --git a/src/chat/utils/chat_message_builder.py b/src/chat/utils/chat_message_builder.py index 46d603b5b..e896420aa 100644 --- a/src/chat/utils/chat_message_builder.py +++ b/src/chat/utils/chat_message_builder.py @@ -420,8 +420,8 @@ async def build_readable_messages( timestamp_mode, ) - readable_read_mark = translate_timestamp_to_human_readable(read_mark, mode=timestamp_mode) - read_mark_line = f"\n--- 以上消息是你已经思考过的内容已读 (标记时间: {readable_read_mark}) ---\n--- 请关注以下未读的新消息---\n" + # readable_read_mark = translate_timestamp_to_human_readable(read_mark, mode=timestamp_mode) + read_mark_line = "\n--- 以上消息是你已经看过---\n--- 请关注以下未读的新消息---\n" # 组合结果,确保空部分不引入多余的标记或换行 if formatted_before and formatted_after: diff --git a/src/chat/utils/utils.py b/src/chat/utils/utils.py index 98a992d11..4ed9174c6 100644 --- a/src/chat/utils/utils.py +++ b/src/chat/utils/utils.py @@ -616,6 +616,8 @@ def translate_timestamp_to_human_readable(timestamp: float, mode: str = "normal" """ if mode == "normal": return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp)) + if mode == "normal_no_YMD": + return time.strftime("%H:%M:%S", time.localtime(timestamp)) elif mode == "relative": now = time.time() diff = now - timestamp @@ -634,111 +636,4 @@ def translate_timestamp_to_human_readable(timestamp: float, mode: str = "normal" return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp)) + ":\n" else: # mode = "lite" or unknown # 只返回时分秒格式,喵~ - return time.strftime("%H:%M:%S", time.localtime(timestamp)) - - -def parse_text_timestamps(text: str, mode: str = "normal") -> str: - """解析文本中的时间戳并转换为可读时间格式 - - Args: - text: 包含时间戳的文本,时间戳应以[]包裹 - mode: 转换模式,传递给translate_timestamp_to_human_readable,"normal"或"relative" - - Returns: - str: 替换后的文本 - - 转换规则: - - normal模式: 将文本中所有时间戳转换为可读格式 - - lite模式: - - 第一个和最后一个时间戳必须转换 - - 以5秒为间隔划分时间段,每段最多转换一个时间戳 - - 不转换的时间戳替换为空字符串 - """ - # 匹配[数字]或[数字.数字]格式的时间戳 - pattern = r"\[(\d+(?:\.\d+)?)\]" - - # 找出所有匹配的时间戳 - matches = list(re.finditer(pattern, text)) - - if not matches: - return text - - # normal模式: 直接转换所有时间戳 - if mode == "normal": - result_text = text - for match in matches: - timestamp = float(match.group(1)) - readable_time = translate_timestamp_to_human_readable(timestamp, "normal") - # 由于替换会改变文本长度,需要使用正则替换而非直接替换 - pattern_instance = re.escape(match.group(0)) - result_text = re.sub(pattern_instance, readable_time, result_text, count=1) - return result_text - else: - # lite模式: 按5秒间隔划分并选择性转换 - result_text = text - - # 提取所有时间戳及其位置 - timestamps = [(float(m.group(1)), m) for m in matches] - timestamps.sort(key=lambda x: x[0]) # 按时间戳升序排序 - - if not timestamps: - return text - - # 获取第一个和最后一个时间戳 - first_timestamp, first_match = timestamps[0] - last_timestamp, last_match = timestamps[-1] - - # 将时间范围划分成5秒间隔的时间段 - time_segments = {} - - # 对所有时间戳按15秒间隔分组 - for ts, match in timestamps: - segment_key = int(ts // 15) # 将时间戳除以15取整,作为时间段的键 - if segment_key not in time_segments: - time_segments[segment_key] = [] - time_segments[segment_key].append((ts, match)) - - # 记录需要转换的时间戳 - to_convert = [] - - # 从每个时间段中选择一个时间戳进行转换 - for _, segment_timestamps in time_segments.items(): - # 选择这个时间段中的第一个时间戳 - to_convert.append(segment_timestamps[0]) - - # 确保第一个和最后一个时间戳在转换列表中 - first_in_list = False - last_in_list = False - - for ts, _ in to_convert: - if ts == first_timestamp: - first_in_list = True - if ts == last_timestamp: - last_in_list = True - - if not first_in_list: - to_convert.append((first_timestamp, first_match)) - if not last_in_list: - to_convert.append((last_timestamp, last_match)) - - # 创建需要转换的时间戳集合,用于快速查找 - to_convert_set = {match.group(0) for _, match in to_convert} - - # 首先替换所有不需要转换的时间戳为空字符串 - for _, match in timestamps: - if match.group(0) not in to_convert_set: - pattern_instance = re.escape(match.group(0)) - result_text = re.sub(pattern_instance, "", result_text, count=1) - - # 按照时间戳原始顺序排序,避免替换时位置错误 - to_convert.sort(key=lambda x: x[1].start()) - - # 执行替换 - # 由于替换会改变文本长度,从后向前替换 - to_convert.reverse() - for ts, match in to_convert: - readable_time = translate_timestamp_to_human_readable(ts, "relative") - pattern_instance = re.escape(match.group(0)) - result_text = re.sub(pattern_instance, readable_time, result_text, count=1) - - return result_text + return time.strftime("%H:%M:%S", time.localtime(timestamp)) \ No newline at end of file