diff --git a/src/chat/focus_chat/info_processors/relationship_processor.py b/src/chat/focus_chat/info_processors/relationship_processor.py index 3fa662ee0..8db062493 100644 --- a/src/chat/focus_chat/info_processors/relationship_processor.py +++ b/src/chat/focus_chat/info_processors/relationship_processor.py @@ -26,6 +26,7 @@ from src.chat.utils.chat_message_builder import ( ) import os import pickle +import random # 消息段清理配置 @@ -36,9 +37,55 @@ SEGMENT_CLEANUP_CONFIG = { "cleanup_interval_hours": 1, # 清理间隔(小时) } +# 用于随机生成prompt示例的资源池 +USER_EXAMPLE_KEYS = ["用户A", "小明", "Alice", "陈皮", "老王", "Bob", "张三", "李四"] +USER_EXAMPLE_VALUES = [ + "ta的昵称", "ta对你的态度", "你对ta的印象", "ta最近心情如何", + "你们的关系", "ta的身份", "ta的兴趣爱好", "ta和你的共同点", "ta的习惯","你们最近做的事","你对ta的语气","你们的互动方式","给你的第一印象","你们最近聊过什么" +] +BOT_EXAMPLE_VALUES = ["身份", "性格", "你的原则", "你的知识", "你的目标", "你的爱好", "你最近在做什么","头像","年龄","性别","职业","兴趣爱好","习惯","目标","原则","知识","爱好"] + + logger = get_logger("processor") +def _generate_random_prompt_example() -> str: + """动态生成一个随机的、符合规则的JSON示例字符串""" + + bot_nickname = global_config.bot.nickname + bot_aliases = list(global_config.bot.alias_names) + + # 确定示例数量 + num_user_examples = random.randint(1, 2) + num_bot_examples = random.randint(1, 2) + + example_dict = {} + + # 1. 生成用户提取示例 + user_keys = random.sample(USER_EXAMPLE_KEYS, min(num_user_examples, len(USER_EXAMPLE_KEYS))) + user_values = random.sample(USER_EXAMPLE_VALUES, min(num_user_examples, len(USER_EXAMPLE_VALUES))) + for i in range(len(user_keys)): + example_dict[user_keys[i]] = user_values[i] + + # 2. 生成bot自身示例 (使用昵称和别名避免key重复) + bot_name_pool = [bot_nickname] + bot_aliases + random.shuffle(bot_name_pool) + bot_values = random.sample(BOT_EXAMPLE_VALUES, min(num_bot_examples, len(BOT_EXAMPLE_VALUES))) + + for i in range(min(num_bot_examples, len(bot_name_pool), len(bot_values))): + example_dict[bot_name_pool[i]] = bot_values[i] + + # 3. 添加固定示例 + example_dict["person_name"] = "其他信息" + + # 随机化顺序并格式化为JSON字符串 + items = list(example_dict.items()) + random.shuffle(items) + shuffled_dict = dict(items) + + return json.dumps(shuffled_dict, ensure_ascii=False, indent=4) + + def init_prompt(): relationship_prompt = """ <聊天记录> @@ -57,13 +104,7 @@ def init_prompt(): 请以json格式输出,例如: -{{ - "用户A": "ta的昵称", - "用户B": "ta对你的态度", - "用户D": "你对ta的印象", - "{bot_name}": "身份", - "person_name": "其他信息", -}} +{example_json} 请严格按照json输出格式,不要输出多余内容,可以同时查询多个人的信息: @@ -561,12 +602,15 @@ class RelationshipProcessor(BaseProcessor): f"你已经调取了[{info_fetching['person_name']}]的[{info_fetching['info_type']}]信息\n" ) + example_json = _generate_random_prompt_example() + prompt = (await global_prompt_manager.get_prompt_async("relationship_prompt")).format( name_block=name_block, bot_name=global_config.bot.nickname, time_now=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), chat_observe_info=chat_observe_info, info_cache_block=info_cache_block, + example_json=example_json, ) try: diff --git a/src/individuality/individuality.py b/src/individuality/individuality.py index beb7422cb..9ccfea0ed 100644 --- a/src/individuality/individuality.py +++ b/src/individuality/individuality.py @@ -6,13 +6,7 @@ import random import json import os import hashlib -import traceback from rich.traceback import install -from json_repair import repair_json -from src.chat.utils.prompt_builder import Prompt, global_prompt_manager -from src.manager.async_task_manager import AsyncTask -from src.llm_models.utils_model import LLMRequest -from src.config.config import global_config from src.common.logger import get_logger from src.person_info.person_info import get_person_info_manager @@ -85,7 +79,7 @@ class Individuality: "nickname": self.name, } await person_info_manager.update_one_field(self.bot_person_id, "impression", impression_text, data=update_data) - logger.info(f"已将完整人设更新到bot的impression中") + logger.info("已将完整人设更新到bot的impression中") await self.express_style.extract_and_store_personality_expressions() diff --git a/src/plugins/built_in/core_actions/plugin.py b/src/plugins/built_in/core_actions/plugin.py index 8ff8c3c8d..d0b73e09e 100644 --- a/src/plugins/built_in/core_actions/plugin.py +++ b/src/plugins/built_in/core_actions/plugin.py @@ -38,7 +38,8 @@ class ReplyAction(BaseAction): action_description = "参与聊天回复,发送文本进行表达" # 动作参数定义 - action_parameters = {"reply_to": "你要回复的对方的发言内容,格式:(用户名:发言内容),可以为none"} + action_parameters = {"reply_to": "你要回复的对方的发言内容,格式:(用户名:发言内容),可以为none", + "reason": "回复的原因"} # 动作使用场景 action_require = ["你想要闲聊或者随便附和", "有人提到你", "如果你刚刚进行了回复,不要对同一个话题重复回应"] @@ -106,8 +107,8 @@ class ReplyAction(BaseAction): class NoReplyAction(BaseAction): """不回复动作,继承时会等待新消息或超时""" - # focus_activation_type = ActionActivationType.ALWAYS - focus_activation_type = ActionActivationType.RANDOM + focus_activation_type = ActionActivationType.ALWAYS + # focus_activation_type = ActionActivationType.RANDOM normal_activation_type = ActionActivationType.NEVER mode_enable = ChatMode.FOCUS parallel_action = False @@ -122,7 +123,7 @@ class NoReplyAction(BaseAction): # 连续no_reply计数器 _consecutive_count = 0 - random_activation_probability = 0.2 + # random_activation_probability = 0.2 # 分级等待时间 _waiting_stages = [10, 60, 600] # 第1、2、3次的等待时间