feat:回复温度现在会受到:人格-情绪-temp的链条影响;顺便修改了情绪激活度的取值
feat:回复温度现在会受到:人格-情绪-temp的链条影响;顺便修改了情绪激活度的取值
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import time
|
||||
from random import random
|
||||
import re
|
||||
|
||||
from typing import List
|
||||
from ...memory_system.Hippocampus import HippocampusManager
|
||||
from ...moods.moods import MoodManager
|
||||
from ...config.config import global_config
|
||||
@@ -18,6 +18,7 @@ from src.common.logger import get_module_logger, CHAT_STYLE_CONFIG, LogConfig
|
||||
from ...chat.chat_stream import chat_manager
|
||||
from ...person_info.relationship_manager import relationship_manager
|
||||
from ...chat.message_buffer import message_buffer
|
||||
from src.plugins.respon_info_catcher.info_catcher import info_catcher_manager
|
||||
|
||||
# 定义日志配置
|
||||
chat_config = LogConfig(
|
||||
@@ -58,7 +59,11 @@ class ReasoningChat:
|
||||
|
||||
return thinking_id
|
||||
|
||||
async def _send_response_messages(self, message, chat, response_set, thinking_id):
|
||||
async def _send_response_messages(self,
|
||||
message,
|
||||
chat,
|
||||
response_set:List[str],
|
||||
thinking_id) -> MessageSending:
|
||||
"""发送回复消息"""
|
||||
container = message_manager.get_container(chat.stream_id)
|
||||
thinking_message = None
|
||||
@@ -77,6 +82,7 @@ class ReasoningChat:
|
||||
message_set = MessageSet(chat, thinking_id)
|
||||
|
||||
mark_head = False
|
||||
first_bot_msg = None
|
||||
for msg in response_set:
|
||||
message_segment = Seg(type="text", data=msg)
|
||||
bot_message = MessageSending(
|
||||
@@ -96,9 +102,12 @@ class ReasoningChat:
|
||||
)
|
||||
if not mark_head:
|
||||
mark_head = True
|
||||
first_bot_msg = bot_message
|
||||
message_set.add_message(bot_message)
|
||||
message_manager.add_message(message_set)
|
||||
|
||||
return first_bot_msg
|
||||
|
||||
async def _handle_emoji(self, message, chat, response):
|
||||
"""处理表情包"""
|
||||
if random() < global_config.emoji_chance:
|
||||
@@ -231,12 +240,19 @@ class ReasoningChat:
|
||||
thinking_id = await self._create_thinking_message(message, chat, userinfo, messageinfo)
|
||||
timer2 = time.time()
|
||||
timing_results["创建思考消息"] = timer2 - timer1
|
||||
|
||||
logger.debug(f"创建捕捉器,thinking_id:{thinking_id}")
|
||||
|
||||
info_catcher = info_catcher_manager.get_info_catcher(thinking_id)
|
||||
info_catcher.catch_decide_to_response(message)
|
||||
|
||||
# 生成回复
|
||||
timer1 = time.time()
|
||||
response_set = await self.gpt.generate_response(message)
|
||||
response_set = await self.gpt.generate_response(message,thinking_id)
|
||||
timer2 = time.time()
|
||||
timing_results["生成回复"] = timer2 - timer1
|
||||
|
||||
info_catcher.catch_after_generate_response(timing_results["生成回复"])
|
||||
|
||||
if not response_set:
|
||||
logger.info("为什么生成回复失败?")
|
||||
@@ -244,9 +260,14 @@ class ReasoningChat:
|
||||
|
||||
# 发送消息
|
||||
timer1 = time.time()
|
||||
await self._send_response_messages(message, chat, response_set, thinking_id)
|
||||
first_bot_msg = await self._send_response_messages(message, chat, response_set, thinking_id)
|
||||
timer2 = time.time()
|
||||
timing_results["发送消息"] = timer2 - timer1
|
||||
|
||||
info_catcher.catch_after_response(timing_results["发送消息"],response_set,first_bot_msg)
|
||||
|
||||
|
||||
info_catcher.done_catch()
|
||||
|
||||
# 处理表情包
|
||||
timer1 = time.time()
|
||||
|
||||
@@ -8,6 +8,7 @@ from ...chat.message import MessageThinking
|
||||
from .reasoning_prompt_builder import prompt_builder
|
||||
from ...chat.utils import process_llm_response
|
||||
from src.common.logger import get_module_logger, LogConfig, LLM_STYLE_CONFIG
|
||||
from src.plugins.respon_info_catcher.info_catcher import info_catcher_manager
|
||||
|
||||
# 定义日志配置
|
||||
llm_config = LogConfig(
|
||||
@@ -37,7 +38,7 @@ class ResponseGenerator:
|
||||
self.current_model_type = "r1" # 默认使用 R1
|
||||
self.current_model_name = "unknown model"
|
||||
|
||||
async def generate_response(self, message: MessageThinking) -> Optional[Union[str, List[str]]]:
|
||||
async def generate_response(self, message: MessageThinking,thinking_id:str) -> Optional[Union[str, List[str]]]:
|
||||
"""根据当前模型类型选择对应的生成函数"""
|
||||
# 从global_config中获取模型概率值并选择模型
|
||||
if random.random() < global_config.MODEL_R1_PROBABILITY:
|
||||
@@ -51,7 +52,7 @@ class ResponseGenerator:
|
||||
f"{self.current_model_type}思考:{message.processed_plain_text[:30] + '...' if len(message.processed_plain_text) > 30 else message.processed_plain_text}"
|
||||
) # noqa: E501
|
||||
|
||||
model_response = await self._generate_response_with_model(message, current_model)
|
||||
model_response = await self._generate_response_with_model(message, current_model,thinking_id)
|
||||
|
||||
# print(f"raw_content: {model_response}")
|
||||
|
||||
@@ -64,8 +65,11 @@ class ResponseGenerator:
|
||||
logger.info(f"{self.current_model_type}思考,失败")
|
||||
return None
|
||||
|
||||
async def _generate_response_with_model(self, message: MessageThinking, model: LLM_request):
|
||||
async def _generate_response_with_model(self, message: MessageThinking, model: LLM_request,thinking_id:str):
|
||||
sender_name = ""
|
||||
|
||||
info_catcher = info_catcher_manager.get_info_catcher(thinking_id)
|
||||
|
||||
if message.chat_stream.user_info.user_cardname and message.chat_stream.user_info.user_nickname:
|
||||
sender_name = (
|
||||
f"[({message.chat_stream.user_info.user_id}){message.chat_stream.user_info.user_nickname}]"
|
||||
@@ -90,6 +94,14 @@ class ResponseGenerator:
|
||||
|
||||
try:
|
||||
content, reasoning_content, self.current_model_name = await model.generate_response(prompt)
|
||||
|
||||
info_catcher.catch_after_llm_generated(
|
||||
prompt=prompt,
|
||||
response=content,
|
||||
reasoning_content=reasoning_content,
|
||||
model_name=self.current_model_name)
|
||||
|
||||
|
||||
except Exception:
|
||||
logger.exception("生成回复时出错")
|
||||
return None
|
||||
|
||||
@@ -10,6 +10,8 @@ from ...chat.utils import process_llm_response
|
||||
from src.common.logger import get_module_logger, LogConfig, LLM_STYLE_CONFIG
|
||||
from src.plugins.respon_info_catcher.info_catcher import info_catcher_manager
|
||||
|
||||
from src.plugins.moods.moods import MoodManager
|
||||
|
||||
# 定义日志配置
|
||||
llm_config = LogConfig(
|
||||
# 使用消息发送专用样式
|
||||
@@ -39,8 +41,12 @@ class ResponseGenerator:
|
||||
logger.info(
|
||||
f"思考:{message.processed_plain_text[:30] + '...' if len(message.processed_plain_text) > 30 else message.processed_plain_text}"
|
||||
)
|
||||
|
||||
|
||||
arousal_multiplier = MoodManager.get_instance().get_arousal_multiplier()
|
||||
|
||||
|
||||
current_model = self.model_normal
|
||||
current_model.temperature = 0.7 * arousal_multiplier #激活度越高,温度越高
|
||||
model_response = await self._generate_response_with_model(message, current_model,thinking_id)
|
||||
|
||||
# print(f"raw_content: {model_response}")
|
||||
|
||||
@@ -19,7 +19,7 @@ logger = get_module_logger("mood_manager", config=mood_config)
|
||||
@dataclass
|
||||
class MoodState:
|
||||
valence: float # 愉悦度 (-1.0 到 1.0),-1表示极度负面,1表示极度正面
|
||||
arousal: float # 唤醒度 (0.0 到 1.0),0表示完全平静,1表示极度兴奋
|
||||
arousal: float # 唤醒度 (-1.0 到 1.0),-1表示抑制,1表示兴奋
|
||||
text: str # 心情文本描述
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ class MoodManager:
|
||||
self._initialized = True
|
||||
|
||||
# 初始化心情状态
|
||||
self.current_mood = MoodState(valence=0.0, arousal=0.5, text="平静")
|
||||
self.current_mood = MoodState(valence=0.0, arousal=0.0, text="平静")
|
||||
|
||||
# 从配置文件获取衰减率
|
||||
self.decay_rate_valence = 1 - global_config.mood_decay_rate # 愉悦度衰减率
|
||||
@@ -71,21 +71,21 @@ class MoodManager:
|
||||
# 情绪文本映射表
|
||||
self.mood_text_map = {
|
||||
# 第一象限:高唤醒,正愉悦
|
||||
(0.5, 0.7): "兴奋",
|
||||
(0.3, 0.8): "快乐",
|
||||
(0.2, 0.65): "满足",
|
||||
(0.5, 0.4): "兴奋",
|
||||
(0.3, 0.6): "快乐",
|
||||
(0.2, 0.3): "满足",
|
||||
# 第二象限:高唤醒,负愉悦
|
||||
(-0.5, 0.7): "愤怒",
|
||||
(-0.3, 0.8): "焦虑",
|
||||
(-0.2, 0.65): "烦躁",
|
||||
(-0.5, 0.4): "愤怒",
|
||||
(-0.3, 0.6): "焦虑",
|
||||
(-0.2, 0.3): "烦躁",
|
||||
# 第三象限:低唤醒,负愉悦
|
||||
(-0.5, 0.3): "悲伤",
|
||||
(-0.3, 0.35): "疲倦",
|
||||
(-0.4, 0.15): "疲倦",
|
||||
(-0.5, -0.4): "悲伤",
|
||||
(-0.3, -0.3): "疲倦",
|
||||
(-0.4, -0.7): "疲倦",
|
||||
# 第四象限:低唤醒,正愉悦
|
||||
(0.2, 0.45): "平静",
|
||||
(0.3, 0.4): "安宁",
|
||||
(0.5, 0.3): "放松",
|
||||
(0.2, -0.1): "平静",
|
||||
(0.3, -0.2): "安宁",
|
||||
(0.5, -0.4): "放松",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
@@ -164,15 +164,15 @@ class MoodManager:
|
||||
-decay_rate_negative * time_diff * neuroticism_factor
|
||||
)
|
||||
|
||||
# Arousal 向中性(0.5)回归
|
||||
arousal_target = 0.5
|
||||
# Arousal 向中性(0)回归
|
||||
arousal_target = 0
|
||||
self.current_mood.arousal = arousal_target + (self.current_mood.arousal - arousal_target) * math.exp(
|
||||
-self.decay_rate_arousal * time_diff * neuroticism_factor
|
||||
)
|
||||
|
||||
# 确保值在合理范围内
|
||||
self.current_mood.valence = max(-1.0, min(1.0, self.current_mood.valence))
|
||||
self.current_mood.arousal = max(0.0, min(1.0, self.current_mood.arousal))
|
||||
self.current_mood.arousal = max(-1.0, min(1.0, self.current_mood.arousal))
|
||||
|
||||
self.last_update = current_time
|
||||
|
||||
@@ -184,7 +184,7 @@ class MoodManager:
|
||||
|
||||
# 限制范围
|
||||
self.current_mood.valence = max(-1.0, min(1.0, self.current_mood.valence))
|
||||
self.current_mood.arousal = max(0.0, min(1.0, self.current_mood.arousal))
|
||||
self.current_mood.arousal = max(-1.0, min(1.0, self.current_mood.arousal))
|
||||
|
||||
self._update_mood_text()
|
||||
|
||||
@@ -217,7 +217,7 @@ class MoodManager:
|
||||
|
||||
# 限制范围
|
||||
self.current_mood.valence = max(-1.0, min(1.0, self.current_mood.valence))
|
||||
self.current_mood.arousal = max(0.0, min(1.0, self.current_mood.arousal))
|
||||
self.current_mood.arousal = max(-1.0, min(1.0, self.current_mood.arousal))
|
||||
|
||||
self._update_mood_text()
|
||||
|
||||
@@ -232,12 +232,22 @@ class MoodManager:
|
||||
elif self.current_mood.valence < -0.5:
|
||||
base_prompt += "你现在心情不太好,"
|
||||
|
||||
if self.current_mood.arousal > 0.7:
|
||||
if self.current_mood.arousal > 0.4:
|
||||
base_prompt += "情绪比较激动。"
|
||||
elif self.current_mood.arousal < 0.3:
|
||||
elif self.current_mood.arousal < -0.4:
|
||||
base_prompt += "情绪比较平静。"
|
||||
|
||||
return base_prompt
|
||||
|
||||
def get_arousal_multiplier(self) -> float:
|
||||
"""根据当前情绪状态返回唤醒度乘数"""
|
||||
if self.current_mood.arousal > 0.4:
|
||||
multiplier = 1 + min(0.15,(self.current_mood.arousal - 0.4)/3)
|
||||
return multiplier
|
||||
elif self.current_mood.arousal < -0.4:
|
||||
multiplier = 1 - min(0.15,((0 - self.current_mood.arousal) - 0.4)/3)
|
||||
return multiplier
|
||||
return 1.0
|
||||
|
||||
def get_current_mood(self) -> MoodState:
|
||||
"""获取当前情绪状态"""
|
||||
@@ -278,7 +288,7 @@ class MoodManager:
|
||||
|
||||
# 限制范围
|
||||
self.current_mood.valence = max(-1.0, min(1.0, self.current_mood.valence))
|
||||
self.current_mood.arousal = max(0.0, min(1.0, self.current_mood.arousal))
|
||||
self.current_mood.arousal = max(-1.0, min(1.0, self.current_mood.arousal))
|
||||
|
||||
self._update_mood_text()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user