From bf8fea15a2c34889cea4545ad9d542677b41e0d2 Mon Sep 17 00:00:00 2001
From: meng_xi_pan <1903647908@qq.com>
Date: Thu, 27 Mar 2025 07:20:31 +0800
Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E7=B3=BB=E7=B3=BB=E7=BB=9F=E6=94=B9?=
=?UTF-8?q?=E8=BF=9B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/common/logger.py | 19 ++++++
src/plugins/chat/bot.py | 2 +-
src/plugins/chat/llm_generator.py | 39 +++++++-----
src/plugins/chat/relationship_manager.py | 81 +++++++++++++++---------
src/plugins/moods/moods.py | 16 +++--
5 files changed, 104 insertions(+), 53 deletions(-)
diff --git a/src/common/logger.py b/src/common/logger.py
index 8a9d08926..c8e604df5 100644
--- a/src/common/logger.py
+++ b/src/common/logger.py
@@ -105,6 +105,24 @@ MOOD_STYLE_CONFIG = {
},
}
+# relationship
+RELATIONSHIP_STYLE_CONFIG = {
+ "advanced": {
+ "console_format": (
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
+ "{extra[module]: <12} | "
+ "关系 | "
+ "{message}"
+ ),
+ "file_format": ("{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 关系 | {message}"),
+ },
+ "simple": {
+ "console_format": ("{time:MM-DD HH:mm} | 关系 | {message}"),
+ "file_format": ("{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 关系 | {message}"),
+ },
+}
+
SENDER_STYLE_CONFIG = {
"advanced": {
"console_format": (
@@ -217,6 +235,7 @@ SENDER_STYLE_CONFIG = SENDER_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else SENDER
LLM_STYLE_CONFIG = LLM_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else LLM_STYLE_CONFIG["advanced"]
CHAT_STYLE_CONFIG = CHAT_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else CHAT_STYLE_CONFIG["advanced"]
MOOD_STYLE_CONFIG = MOOD_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else MOOD_STYLE_CONFIG["advanced"]
+RELATIONSHIP_STYLE_CONFIG = RELATIONSHIP_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else RELATIONSHIP_STYLE_CONFIG["advanced"]
SCHEDULE_STYLE_CONFIG = SCHEDULE_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else SCHEDULE_STYLE_CONFIG["advanced"]
HEARTFLOW_STYLE_CONFIG = HEARTFLOW_STYLE_CONFIG["simple"] if SIMPLE_OUTPUT else HEARTFLOW_STYLE_CONFIG["advanced"]
diff --git a/src/plugins/chat/bot.py b/src/plugins/chat/bot.py
index e89375217..0837347ef 100644
--- a/src/plugins/chat/bot.py
+++ b/src/plugins/chat/bot.py
@@ -298,7 +298,7 @@ class ChatBot:
)
# 使用情绪管理器更新情绪
- self.mood_manager.update_mood_from_emotion(emotion[0], global_config.mood_intensity_factor)
+ self.mood_manager.update_mood_from_emotion(emotion, global_config.mood_intensity_factor)
async def handle_notice(self, event: NoticeEvent, bot: Bot) -> None:
"""处理收到的通知"""
diff --git a/src/plugins/chat/llm_generator.py b/src/plugins/chat/llm_generator.py
index 7b032104a..d5a700988 100644
--- a/src/plugins/chat/llm_generator.py
+++ b/src/plugins/chat/llm_generator.py
@@ -144,18 +144,25 @@ class ResponseGenerator:
try:
# 构建提示词,结合回复内容、被回复的内容以及立场分析
prompt = f"""
- 请根据以下对话内容,完成以下任务:
- 1. 判断回复者的立场是"supportive"(支持)、"opposed"(反对)还是"neutrality"(中立)。
- 2. 从"happy,angry,sad,surprised,disgusted,fearful,neutral"中选出最匹配的1个情感标签。
- 3. 按照"立场-情绪"的格式输出结果,例如:"supportive-happy"。
+ 请严格根据以下对话内容,完成以下任务:
+ 1. 判断回复者对被回复者观点的直接立场:
+ - "支持":明确同意或强化被回复者观点
+ - "反对":明确反驳或否定被回复者观点
+ - "中立":不表达明确立场或无关回应
+ 2. 从"开心,愤怒,悲伤,惊讶,平静,害羞,恐惧,厌恶,困惑"中选出最匹配的1个情感标签
+ 3. 按照"立场-情绪"的格式直接输出结果,例如:"反对-愤怒"
- 被回复的内容:
- {processed_plain_text}
+ 对话示例:
+ 被回复:「A就是笨」
+ 回复:「A明明很聪明」 → 反对-愤怒
- 回复内容:
- {content}
+ 当前对话:
+ 被回复:「{processed_plain_text}」
+ 回复:「{content}」
- 请分析回复者的立场和情感倾向,并输出结果:
+ 输出要求:
+ - 只需输出"立场-情绪"结果,不要解释
+ - 严格基于文字直接表达的对立关系判断
"""
# 调用模型生成结果
@@ -165,18 +172,20 @@ class ResponseGenerator:
# 解析模型输出的结果
if "-" in result:
stance, emotion = result.split("-", 1)
- valid_stances = ["supportive", "opposed", "neutrality"]
- valid_emotions = ["happy", "angry", "sad", "surprised", "disgusted", "fearful", "neutral"]
+ valid_stances = ["支持", "反对", "中立"]
+ valid_emotions = ["开心", "愤怒", "悲伤", "惊讶", "害羞", "平静", "恐惧", "厌恶", "困惑"]
if stance in valid_stances and emotion in valid_emotions:
return stance, emotion # 返回有效的立场-情绪组合
else:
- return "neutrality", "neutral" # 默认返回中立-中性
+ logger.debug(f"无效立场-情感组合:{result}")
+ return "中立", "平静" # 默认返回中立-平静
else:
- return "neutrality", "neutral" # 格式错误时返回默认值
+ logger.debug(f"立场-情感格式错误:{result}")
+ return "中立", "平静" # 格式错误时返回默认值
except Exception as e:
- print(f"获取情感标签时出错: {e}")
- return "neutrality", "neutral" # 出错时返回默认值
+ logger.debug(f"获取情感标签时出错: {e}")
+ return "中立", "平静" # 出错时返回默认值
async def _process_response(self, content: str) -> Tuple[List[str], List[str]]:
"""处理响应内容,返回处理后的内容和情感标签"""
diff --git a/src/plugins/chat/relationship_manager.py b/src/plugins/chat/relationship_manager.py
index f4cda0662..5bc60cc80 100644
--- a/src/plugins/chat/relationship_manager.py
+++ b/src/plugins/chat/relationship_manager.py
@@ -1,6 +1,6 @@
import asyncio
from typing import Optional
-from src.common.logger import get_module_logger
+from src.common.logger import get_module_logger, LogConfig, RELATIONSHIP_STYLE_CONFIG
from ...common.database import db
from .message_base import UserInfo
@@ -8,7 +8,12 @@ from .chat_stream import ChatStream
import math
from bson.decimal128 import Decimal128
-logger = get_module_logger("rel_manager")
+relationship_config = LogConfig(
+ # 使用关系专用样式
+ console_format=RELATIONSHIP_STYLE_CONFIG["console_format"],
+ file_format=RELATIONSHIP_STYLE_CONFIG["file_format"],
+)
+logger = get_module_logger("rel_manager", config=relationship_config)
class Impression:
@@ -270,19 +275,21 @@ class RelationshipManager:
3.人维护关系的精力往往有限,所以当高关系值用户越多,对于中高关系值用户增长越慢
"""
stancedict = {
- "supportive": 0,
- "neutrality": 1,
- "opposed": 2,
+ "支持": 0,
+ "中立": 1,
+ "反对": 2,
}
valuedict = {
- "happy": 1.5,
- "angry": -3.0,
- "sad": -1.5,
- "surprised": 0.6,
- "disgusted": -4.5,
- "fearful": -2.1,
- "neutral": 0.3,
+ "开心": 1.5,
+ "愤怒": -3.5,
+ "悲伤": -1.5,
+ "惊讶": 0.6,
+ "害羞": 2.0,
+ "平静": 0.3,
+ "恐惧": -2,
+ "厌恶": -2.5,
+ "困惑": 0.5,
}
if self.get_relationship(chat_stream):
old_value = self.get_relationship(chat_stream).relationship_value
@@ -301,9 +308,12 @@ class RelationshipManager:
if old_value > 500:
high_value_count = 0
for _, relationship in self.relationships.items():
- if relationship.relationship_value >= 850:
+ if relationship.relationship_value >= 700:
high_value_count += 1
- value *= 3 / (high_value_count + 3)
+ if old_value >= 700:
+ value *= 3 / (high_value_count + 2) # 排除自己
+ else:
+ value *= 3 / (high_value_count + 3)
elif valuedict[label] < 0 and stancedict[stance] != 0:
value = value * math.exp(old_value / 1000)
else:
@@ -316,27 +326,20 @@ class RelationshipManager:
else:
value = 0
- logger.info(f"[关系变更] 立场:{stance} 标签:{label} 关系值:{value}")
+ level_num = self.calculate_level_num(old_value+value)
+ relationship_level = ["厌恶", "冷漠", "一般", "友好", "喜欢", "暧昧"]
+ logger.info(
+ f"当前关系: {relationship_level[level_num]}, "
+ f"关系值: {old_value:.2f}, "
+ f"当前立场情感: {stance}-{label}, "
+ f"变更: {value:+.5f}"
+ )
await self.update_relationship_value(chat_stream=chat_stream, relationship_value=value)
def build_relationship_info(self, person) -> str:
relationship_value = relationship_manager.get_relationship(person).relationship_value
- if -1000 <= relationship_value < -227:
- level_num = 0
- elif -227 <= relationship_value < -73:
- level_num = 1
- elif -73 <= relationship_value < 227:
- level_num = 2
- elif 227 <= relationship_value < 587:
- level_num = 3
- elif 587 <= relationship_value < 900:
- level_num = 4
- elif 900 <= relationship_value <= 1000:
- level_num = 5
- else:
- level_num = 5 if relationship_value > 1000 else 0
-
+ level_num = self.calculate_level_num(relationship_value)
relationship_level = ["厌恶", "冷漠", "一般", "友好", "喜欢", "暧昧"]
relation_prompt2_list = [
"冷漠回应",
@@ -356,6 +359,24 @@ class RelationshipManager:
f"你对昵称为'({person.user_info.user_id}){person.user_info.user_nickname}'的用户的态度为{relationship_level[level_num]},"
f"回复态度为{relation_prompt2_list[level_num]},关系等级为{level_num}。"
)
+
+ def calculate_level_num(self, relationship_value) -> int:
+ """关系等级计算"""
+ if -1000 <= relationship_value < -227:
+ level_num = 0
+ elif -227 <= relationship_value < -73:
+ level_num = 1
+ elif -73 <= relationship_value < 227:
+ level_num = 2
+ elif 227 <= relationship_value < 587:
+ level_num = 3
+ elif 587 <= relationship_value < 900:
+ level_num = 4
+ elif 900 <= relationship_value <= 1000:
+ level_num = 5
+ else:
+ level_num = 5 if relationship_value > 1000 else 0
+ return level_num
relationship_manager = RelationshipManager()
diff --git a/src/plugins/moods/moods.py b/src/plugins/moods/moods.py
index 986075da0..4cc115d3b 100644
--- a/src/plugins/moods/moods.py
+++ b/src/plugins/moods/moods.py
@@ -55,13 +55,15 @@ class MoodManager:
# 情绪词映射表 (valence, arousal)
self.emotion_map = {
- "happy": (0.8, 0.6), # 高愉悦度,中等唤醒度
- "angry": (-0.7, 0.7), # 负愉悦度,高唤醒度
- "sad": (-0.6, 0.3), # 负愉悦度,低唤醒度
- "surprised": (0.4, 0.8), # 中等愉悦度,高唤醒度
- "disgusted": (-0.8, 0.5), # 高负愉悦度,中等唤醒度
- "fearful": (-0.7, 0.6), # 负愉悦度,高唤醒度
- "neutral": (0.0, 0.5), # 中性愉悦度,中等唤醒度
+ "开心": (0.8, 0.6), # 高愉悦度,中等唤醒度
+ "愤怒": (-0.7, 0.7), # 负愉悦度,高唤醒度
+ "悲伤": (-0.6, 0.3), # 负愉悦度,低唤醒度
+ "惊讶": (0.2, 0.8), # 中等愉悦度,高唤醒度
+ "害羞": (0.5, 0.2), # 中等愉悦度,低唤醒度
+ "平静": (0.0, 0.5), # 中性愉悦度,中等唤醒度
+ "恐惧": (-0.7, 0.6), # 负愉悦度,高唤醒度
+ "厌恶": (-0.4, 0.4), # 负愉悦度,低唤醒度
+ "困惑": (0.0, 0.6), # 中性愉悦度,高唤醒度
}
# 情绪文本映射表