This commit is contained in:
tcmofashi
2025-04-17 15:53:26 +08:00
52 changed files with 299 additions and 336 deletions

View File

@@ -13,7 +13,8 @@ class MessageProcessor:
def __init__(self):
self.storage = MessageStorage()
def _check_ban_words(self, text: str, chat, userinfo) -> bool:
@staticmethod
def _check_ban_words(text: str, chat, userinfo) -> bool:
"""检查消息中是否包含过滤词"""
for word in global_config.ban_words:
if word in text:
@@ -24,7 +25,8 @@ class MessageProcessor:
return True
return False
def _check_ban_regex(self, text: str, chat, userinfo) -> bool:
@staticmethod
def _check_ban_regex(text: str, chat, userinfo) -> bool:
"""检查消息是否匹配过滤正则表达式"""
for pattern in global_config.ban_msgs_regex:
if pattern.search(text):

View File

@@ -37,7 +37,8 @@ class ReasoningChat:
self.mood_manager = MoodManager.get_instance()
self.mood_manager.start_mood_update()
async def _create_thinking_message(self, message, chat, userinfo, messageinfo):
@staticmethod
async def _create_thinking_message(message, chat, userinfo, messageinfo):
"""创建思考消息"""
bot_user_info = UserInfo(
user_id=global_config.BOT_QQ,
@@ -59,7 +60,8 @@ class ReasoningChat:
return thinking_id
async def _send_response_messages(self, message, chat, response_set: List[str], thinking_id) -> MessageSending:
@staticmethod
async def _send_response_messages(message, chat, response_set: List[str], thinking_id) -> MessageSending:
"""发送回复消息"""
container = message_manager.get_container(chat.stream_id)
thinking_message = None
@@ -104,7 +106,8 @@ class ReasoningChat:
return first_bot_msg
async def _handle_emoji(self, message, chat, response):
@staticmethod
async def _handle_emoji(message, chat, response):
"""处理表情包"""
if random() < global_config.emoji_chance:
emoji_raw = await emoji_manager.get_emoji_for_text(response)
@@ -192,21 +195,21 @@ class ReasoningChat:
if not buffer_result:
await willing_manager.bombing_buffer_message_handle(message.message_info.message_id)
willing_manager.delete(message.message_info.message_id)
F_type = "seglist"
f_type = "seglist"
if message.message_segment.type != "seglist":
F_type = message.message_segment.type
f_type = message.message_segment.type
else:
if (
isinstance(message.message_segment.data, list)
and all(isinstance(x, Seg) for x in message.message_segment.data)
and len(message.message_segment.data) == 1
):
F_type = message.message_segment.data[0].type
if F_type == "text":
f_type = message.message_segment.data[0].type
if f_type == "text":
logger.info(f"触发缓冲,已炸飞消息:{message.processed_plain_text}")
elif F_type == "image":
elif f_type == "image":
logger.info("触发缓冲,已炸飞表情包/图片")
elif F_type == "seglist":
elif f_type == "seglist":
logger.info("触发缓冲,已炸飞消息列")
return
@@ -291,7 +294,8 @@ class ReasoningChat:
# 意愿管理器注销当前message信息
willing_manager.delete(message.message_info.message_id)
def _check_ban_words(self, text: str, chat, userinfo) -> bool:
@staticmethod
def _check_ban_words(text: str, chat, userinfo) -> bool:
"""检查消息中是否包含过滤词"""
for word in global_config.ban_words:
if word in text:
@@ -302,7 +306,8 @@ class ReasoningChat:
return True
return False
def _check_ban_regex(self, text: str, chat, userinfo) -> bool:
@staticmethod
def _check_ban_regex(text: str, chat, userinfo) -> bool:
"""检查消息是否匹配过滤正则表达式"""
for pattern in global_config.ban_msgs_regex:
if pattern.search(text):

View File

@@ -69,8 +69,6 @@ class ResponseGenerator:
return None
async def _generate_response_with_model(self, message: MessageThinking, model: LLMRequest, 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:
@@ -188,7 +186,8 @@ class ResponseGenerator:
logger.debug(f"获取情感标签时出错: {e}")
return "中立", "平静" # 出错时返回默认值
async def _process_response(self, content: str) -> Tuple[List[str], List[str]]:
@staticmethod
async def _process_response(content: str) -> Tuple[List[str], List[str]]:
"""处理响应内容,返回处理后的内容和情感标签"""
if not content:
return None, []

View File

@@ -101,16 +101,14 @@ class PromptBuilder:
related_memory = await HippocampusManager.get_instance().get_memory_from_text(
text=message_txt, max_memory_num=2, max_memory_length=2, max_depth=3, fast_retrieval=False
)
related_memory_info = ""
if related_memory:
related_memory_info = ""
for memory in related_memory:
related_memory_info += memory[1]
# memory_prompt = f"你想起你之前见过的事情:{related_memory_info}。\n以上是你的回忆不一定是目前聊天里的人说的也不一定是现在发生的事情请记住。\n"
memory_prompt = await global_prompt_manager.format_prompt(
"memory_prompt", related_memory_info=related_memory_info
)
else:
related_memory_info = ""
# print(f"相关记忆:{related_memory_info}")
@@ -162,7 +160,6 @@ class PromptBuilder:
# 知识构建
start_time = time.time()
prompt_info = ""
prompt_info = await self.get_prompt_info(message_txt, threshold=0.38)
if prompt_info:
# prompt_info = f"""\n你有以下这些**知识**\n{prompt_info}\n请你**记住上面的知识**,之后可能会用到。\n"""
@@ -373,8 +370,9 @@ class PromptBuilder:
logger.info(f"知识库检索总耗时: {time.time() - start_time:.3f}")
return related_info
@staticmethod
def get_info_from_db(
self, query_embedding: list, limit: int = 1, threshold: float = 0.5, return_raw: bool = False
query_embedding: list, limit: int = 1, threshold: float = 0.5, return_raw: bool = False
) -> Union[str, list]:
if not query_embedding:
return "" if not return_raw else []

View File

@@ -40,7 +40,8 @@ class ThinkFlowChat:
self.mood_manager.start_mood_update()
self.tool_user = ToolUser()
async def _create_thinking_message(self, message, chat, userinfo, messageinfo):
@staticmethod
async def _create_thinking_message(message, chat, userinfo, messageinfo):
"""创建思考消息"""
bot_user_info = UserInfo(
user_id=global_config.BOT_QQ,
@@ -62,7 +63,8 @@ class ThinkFlowChat:
return thinking_id
async def _send_response_messages(self, message, chat, response_set: List[str], thinking_id) -> MessageSending:
@staticmethod
async def _send_response_messages(message, chat, response_set: List[str], thinking_id) -> MessageSending:
"""发送回复消息"""
container = message_manager.get_container(chat.stream_id)
thinking_message = None
@@ -108,7 +110,8 @@ class ThinkFlowChat:
message_manager.add_message(message_set)
return first_bot_msg
async def _handle_emoji(self, message, chat, response, send_emoji=""):
@staticmethod
async def _handle_emoji(message, chat, response, send_emoji=""):
"""处理表情包"""
if send_emoji:
emoji_raw = await emoji_manager.get_emoji_for_text(send_emoji)
@@ -204,21 +207,21 @@ class ThinkFlowChat:
if not buffer_result:
await willing_manager.bombing_buffer_message_handle(message.message_info.message_id)
willing_manager.delete(message.message_info.message_id)
F_type = "seglist"
f_type = "seglist"
if message.message_segment.type != "seglist":
F_type = message.message_segment.type
f_type = message.message_segment.type
else:
if (
isinstance(message.message_segment.data, list)
and all(isinstance(x, Seg) for x in message.message_segment.data)
and len(message.message_segment.data) == 1
):
F_type = message.message_segment.data[0].type
if F_type == "text":
f_type = message.message_segment.data[0].type
if f_type == "text":
logger.info(f"触发缓冲,已炸飞消息:{message.processed_plain_text}")
elif F_type == "image":
elif f_type == "image":
logger.info("触发缓冲,已炸飞表情包/图片")
elif F_type == "seglist":
elif f_type == "seglist":
logger.info("触发缓冲,已炸飞消息列")
return
@@ -461,7 +464,8 @@ class ThinkFlowChat:
# 意愿管理器注销当前message信息
willing_manager.delete(message.message_info.message_id)
def _check_ban_words(self, text: str, chat, userinfo) -> bool:
@staticmethod
def _check_ban_words(text: str, chat, userinfo) -> bool:
"""检查消息中是否包含过滤词"""
for word in global_config.ban_words:
if word in text:
@@ -472,7 +476,8 @@ class ThinkFlowChat:
return True
return False
def _check_ban_regex(self, text: str, chat, userinfo) -> bool:
@staticmethod
def _check_ban_regex(text: str, chat, userinfo) -> bool:
"""检查消息是否匹配过滤正则表达式"""
for pattern in global_config.ban_msgs_regex:
if pattern.search(text):

View File

@@ -236,7 +236,8 @@ class ResponseGenerator:
logger.debug(f"获取情感标签时出错: {e}")
return "中立", "平静" # 出错时返回默认值
async def _process_response(self, content: str) -> List[str]:
@staticmethod
async def _process_response(content: str) -> List[str]:
"""处理响应内容,返回处理后的内容和情感标签"""
if not content:
return None

View File

@@ -64,8 +64,9 @@ class PromptBuilder:
self.prompt_built = ""
self.activate_messages = ""
@staticmethod
async def _build_prompt(
self, chat_stream, message_txt: str, sender_name: str = "某人", stream_id: Optional[int] = None
chat_stream, message_txt: str, sender_name: str = "某人", stream_id: Optional[int] = None
) -> tuple[str, str]:
current_mind_info = heartflow.get_subheartflow(stream_id).current_mind
@@ -168,8 +169,9 @@ class PromptBuilder:
return prompt
@staticmethod
async def _build_prompt_simple(
self, chat_stream, message_txt: str, sender_name: str = "某人", stream_id: Optional[int] = None
chat_stream, message_txt: str, sender_name: str = "某人", stream_id: Optional[int] = None
) -> tuple[str, str]:
current_mind_info = heartflow.get_subheartflow(stream_id).current_mind
@@ -237,8 +239,8 @@ class PromptBuilder:
logger.info(f"生成回复的prompt: {prompt}")
return prompt
@staticmethod
async def _build_prompt_check_response(
self,
chat_stream,
message_txt: str,
sender_name: str = "某人",