From 4cce44bcedcfe76a659ec77a6478e56a2f111419 Mon Sep 17 00:00:00 2001 From: meng_xi_pan <1903647908@qq.com> Date: Wed, 2 Apr 2025 02:50:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E5=85=B3=E7=B3=BB=E7=9A=84pr?= =?UTF-8?q?ompt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/chat/prompt_builder.py | 26 +++++++++++++++++++++--- src/plugins/chat/relationship_manager.py | 23 ++++++++------------- src/plugins/chat/utils.py | 18 +++++++--------- 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/plugins/chat/prompt_builder.py b/src/plugins/chat/prompt_builder.py index cc048fc70..37da5dd4f 100644 --- a/src/plugins/chat/prompt_builder.py +++ b/src/plugins/chat/prompt_builder.py @@ -7,9 +7,10 @@ from ..memory_system.Hippocampus import HippocampusManager from ..moods.moods import MoodManager from ..schedule.schedule_generator import bot_schedule from ..config.config import global_config -from .utils import get_embedding, get_recent_group_detailed_plain_text +from .utils import get_embedding, get_recent_group_detailed_plain_text, get_recent_group_speaker from .chat_stream import chat_manager from src.common.logger import get_module_logger +from .relationship_manager import relationship_manager from src.heart_flow.heartflow import heartflow @@ -29,6 +30,25 @@ class PromptBuilder: # 开始构建prompt + # 关系 + who_chat_in_group = [(chat_stream.user_info.platform, + chat_stream.user_info.user_id, + chat_stream.user_info.user_nickname)] + who_chat_in_group += get_recent_group_speaker( + stream_id, + (chat_stream.user_info.platform, chat_stream.user_info.user_id), + limit=global_config.MAX_CONTEXT_SIZE, + ) + + relation_prompt = "" + for person in who_chat_in_group: + relation_prompt += await relationship_manager.build_relationship_info(person) + + relation_prompt_all = ( + f"{relation_prompt}关系等级越大,关系越好,请分析聊天记录," + f"根据你和说话者{sender_name}的关系和态度进行回复,明确你的立场和情感。" + ) + # 心情 mood_manager = MoodManager.get_instance() mood_prompt = mood_manager.get_prompt() @@ -116,14 +136,14 @@ class PromptBuilder: {chat_talking_prompt} 你刚刚脑子里在想: {current_mind_info} -现在"{sender_name}"说的:{message_txt}。引起了你的注意,你想要在群里发言发言或者回复这条消息。\n +现在"{sender_name}"说的:{message_txt}。引起了你的注意,你想要在群里发言发言或者回复这条消息。{relation_prompt_all}\n 你的网名叫{global_config.BOT_NICKNAME},有人也叫你{"/".join(global_config.BOT_ALIAS_NAMES)},{prompt_personality}。 你正在{chat_target_2},现在请你读读之前的聊天记录,然后给出日常且口语化的回复,平淡一些, 尽量简短一些。{keywords_reaction_prompt}请注意把握聊天内容,不要回复的太有条理,可以有个性。{prompt_ger} 请回复的平淡一些,简短一些,说中文,不要刻意突出自身学科背景,尽量不要说你说过的话 请注意不要输出多余内容(包括前后缀,冒号和引号,括号,表情等),只输出回复内容。 {moderation_prompt}不要输出多余内容(包括前后缀,冒号和引号,括号,表情包,at或 @等 )。""" - + return prompt def _build_initiative_prompt_select(self, group_id, probability_1=0.8, probability_2=0.1): diff --git a/src/plugins/chat/relationship_manager.py b/src/plugins/chat/relationship_manager.py index 2e58cd4b9..ddc172264 100644 --- a/src/plugins/chat/relationship_manager.py +++ b/src/plugins/chat/relationship_manager.py @@ -137,29 +137,24 @@ class RelationshipManager: await person_info_manager.update_one_field(person_id, "relationship_value", old_value + value, data) - def build_relationship_info(self, person) -> str: - person_id = person_info_manager.get_person_id(person.user_info.platform, person.user_info.user_id) - relationship_value = person_info_manager.get_value(person_id, "relationship_value") + async def build_relationship_info(self, person) -> str: + person_id = person_info_manager.get_person_id(person[0], person[1]) + relationship_value = await person_info_manager.get_value(person_id, "relationship_value") level_num = self.calculate_level_num(relationship_value) relationship_level = ["厌恶", "冷漠", "一般", "友好", "喜欢", "暧昧"] relation_prompt2_list = [ - "冷漠回应", + "厌恶回应", "冷淡回复", "保持理性", "愿意回复", "积极回复", "无条件支持", ] - if person.user_info.user_cardname: - return ( - f"你对昵称为'[({person.user_info.user_id}){person.user_info.user_nickname}]{person.user_info.user_cardname}'的用户的态度为{relationship_level[level_num]}," - f"回复态度为{relation_prompt2_list[level_num]},关系等级为{level_num}。" - ) - else: - return ( - f"你对昵称为'({person.user_info.user_id}){person.user_info.user_nickname}'的用户的态度为{relationship_level[level_num]}," - f"回复态度为{relation_prompt2_list[level_num]},关系等级为{level_num}。" - ) + + return ( + f"你对昵称为'({person[1]}){person[2]}'的用户的态度为{relationship_level[level_num]}," + f"回复态度为{relation_prompt2_list[level_num]},关系等级为{level_num}。" + ) def calculate_level_num(self, relationship_value) -> int: """关系等级计算""" diff --git a/src/plugins/chat/utils.py b/src/plugins/chat/utils.py index ecd67816a..bcf79063a 100644 --- a/src/plugins/chat/utils.py +++ b/src/plugins/chat/utils.py @@ -149,7 +149,6 @@ def get_recent_group_speaker(chat_stream_id: int, sender, limit: int = 12) -> li db.messages.find( {"chat_id": chat_stream_id}, { - "chat_info": 1, "user_info": 1, }, ) @@ -160,20 +159,17 @@ def get_recent_group_speaker(chat_stream_id: int, sender, limit: int = 12) -> li if not recent_messages: return [] - who_chat_in_group = [] # ChatStream列表 - - duplicate_removal = [] + who_chat_in_group = [] for msg_db_data in recent_messages: user_info = UserInfo.from_dict(msg_db_data["user_info"]) if ( - (user_info.user_id, user_info.platform) != sender - and (user_info.user_id, user_info.platform) != (global_config.BOT_QQ, "qq") - and (user_info.user_id, user_info.platform) not in duplicate_removal - and len(duplicate_removal) < 5 + (user_info.platform, user_info.user_id) != sender + and user_info.user_id != global_config.BOT_QQ + and (user_info.platform, user_info.user_id, user_info.user_nickname) not in who_chat_in_group + and len(who_chat_in_group) < 5 ): # 排除重复,排除消息发送者,排除bot(此处bot的平台强制为了qq,可能需要更改),限制加载的关系数目 - duplicate_removal.append((user_info.user_id, user_info.platform)) - chat_info = msg_db_data.get("chat_info", {}) - who_chat_in_group.append(ChatStream.from_dict(chat_info)) + who_chat_in_group.append((user_info.platform, user_info.user_id, user_info.user_nickname)) + return who_chat_in_group