Merge branch 'dev' of https://github.com/MaiM-with-u/MaiBot into dev
This commit is contained in:
@@ -66,9 +66,9 @@
|
|||||||
|
|
||||||
## 💬 讨论
|
## 💬 讨论
|
||||||
|
|
||||||
- [一群](https://qm.qq.com/q/VQ3XZrWgMs) |
|
- [四群](https://qm.qq.com/q/wGePTl1UyY) |
|
||||||
[四群](https://qm.qq.com/q/wGePTl1UyY) |
|
[一群](https://qm.qq.com/q/VQ3XZrWgMs)(已满) |
|
||||||
[二群](https://qm.qq.com/q/RzmCiRtHEW) |
|
[二群](https://qm.qq.com/q/RzmCiRtHEW)(已满) |
|
||||||
[五群](https://qm.qq.com/q/JxvHZnxyec)(已满) |
|
[五群](https://qm.qq.com/q/JxvHZnxyec)(已满) |
|
||||||
[三群](https://qm.qq.com/q/wlH5eT8OmQ)(已满)
|
[三群](https://qm.qq.com/q/wlH5eT8OmQ)(已满)
|
||||||
|
|
||||||
|
|||||||
@@ -57,9 +57,9 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "8120:8080"
|
- "8120:8080"
|
||||||
volumes:
|
volumes:
|
||||||
- ./data/MaiMBot/MaiBot.db:/data/MaiBot.db
|
- ./data/MaiMBot:/data/MaiMBot
|
||||||
environment:
|
environment:
|
||||||
- SQLITE_DATABASE=MaiBot.db # 你的数据库文件
|
- SQLITE_DATABASE=MaiMBot/MaiBot.db # 你的数据库文件
|
||||||
networks:
|
networks:
|
||||||
- maim_bot
|
- maim_bot
|
||||||
networks:
|
networks:
|
||||||
|
|||||||
@@ -131,6 +131,12 @@ class ChatBot:
|
|||||||
message = MessageRecv(message_data)
|
message = MessageRecv(message_data)
|
||||||
group_info = message.message_info.group_info
|
group_info = message.message_info.group_info
|
||||||
user_info = message.message_info.user_info
|
user_info = message.message_info.user_info
|
||||||
|
if message.message_info.additional_config:
|
||||||
|
sent_message = message.message_info.additional_config.get("echo", False)
|
||||||
|
if sent_message: # 这一段只是为了在一切处理前劫持上报的自身消息,用于更新message_id,需要ada支持上报事件,实际测试中不会对正常使用造成任何问题
|
||||||
|
await MessageStorage.update_message(message)
|
||||||
|
return
|
||||||
|
|
||||||
get_chat_manager().register_message(message)
|
get_chat_manager().register_message(message)
|
||||||
|
|
||||||
# 创建聊天流
|
# 创建聊天流
|
||||||
|
|||||||
@@ -101,5 +101,33 @@ class MessageStorage:
|
|||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("删除撤回消息失败")
|
logger.exception("删除撤回消息失败")
|
||||||
|
|
||||||
|
# 如果需要其他存储相关的函数,可以在这里添加
|
||||||
|
@staticmethod
|
||||||
|
async def update_message(
|
||||||
|
message: MessageRecv,
|
||||||
|
) -> None: # 用于实时更新数据库的自身发送消息ID,目前能处理text,reply,image和emoji
|
||||||
|
"""更新最新一条匹配消息的message_id"""
|
||||||
|
try:
|
||||||
|
if message.message_segment.type == "notify":
|
||||||
|
mmc_message_id = message.message_segment.data.get("echo")
|
||||||
|
qq_message_id = message.message_segment.data.get("actual_id")
|
||||||
|
else:
|
||||||
|
logger.info(f"更新消息ID错误,seg类型为{message.message_segment.type}")
|
||||||
|
return
|
||||||
|
if not qq_message_id:
|
||||||
|
logger.info("消息不存在message_id,无法更新")
|
||||||
|
return
|
||||||
|
# 查询最新一条匹配消息
|
||||||
|
matched_message = (
|
||||||
|
Messages.select().where((Messages.message_id == mmc_message_id)).order_by(Messages.time.desc()).first()
|
||||||
|
)
|
||||||
|
|
||||||
# 如果需要其他存储相关的函数,可以在这里添加
|
if matched_message:
|
||||||
|
# 更新找到的消息记录
|
||||||
|
Messages.update(message_id=qq_message_id).where(Messages.id == matched_message.id).execute()
|
||||||
|
logger.info(f"更新消息ID成功: {matched_message.message_id} -> {qq_message_id}")
|
||||||
|
else:
|
||||||
|
logger.debug("未找到匹配的消息")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"更新消息ID失败: {e}")
|
||||||
|
|||||||
@@ -126,10 +126,9 @@ class PromptBuilder:
|
|||||||
(chat_stream.user_info.platform, chat_stream.user_info.user_id) if chat_stream.user_info else None,
|
(chat_stream.user_info.platform, chat_stream.user_info.user_id) if chat_stream.user_info else None,
|
||||||
limit=global_config.normal_chat.max_context_size,
|
limit=global_config.normal_chat.max_context_size,
|
||||||
)
|
)
|
||||||
elif chat_stream.user_info:
|
who_chat_in_group.append(
|
||||||
who_chat_in_group.append(
|
(chat_stream.user_info.platform, chat_stream.user_info.user_id, chat_stream.user_info.user_nickname)
|
||||||
(chat_stream.user_info.platform, chat_stream.user_info.user_id, chat_stream.user_info.user_nickname)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
relation_prompt = ""
|
relation_prompt = ""
|
||||||
if global_config.relationship.enable_relationship:
|
if global_config.relationship.enable_relationship:
|
||||||
|
|||||||
@@ -160,8 +160,7 @@ class DefaultReplyer:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
async def generate_reply_with_context(
|
async def generate_reply_with_context(
|
||||||
self,
|
self, reply_data: Dict[str, Any], enable_splitter: bool = True, enable_chinese_typo: bool = True
|
||||||
reply_data: Dict[str, Any],
|
|
||||||
) -> Tuple[bool, Optional[List[str]]]:
|
) -> Tuple[bool, Optional[List[str]]]:
|
||||||
"""
|
"""
|
||||||
回复器 (Replier): 核心逻辑,负责生成回复文本。
|
回复器 (Replier): 核心逻辑,负责生成回复文本。
|
||||||
@@ -191,7 +190,7 @@ class DefaultReplyer:
|
|||||||
logger.error(f"{self.log_prefix}LLM 生成失败: {llm_e}")
|
logger.error(f"{self.log_prefix}LLM 生成失败: {llm_e}")
|
||||||
return False, None # LLM 调用失败则无法生成回复
|
return False, None # LLM 调用失败则无法生成回复
|
||||||
|
|
||||||
processed_response = process_llm_response(content)
|
processed_response = process_llm_response(content, enable_splitter, enable_chinese_typo)
|
||||||
|
|
||||||
# 5. 处理 LLM 响应
|
# 5. 处理 LLM 响应
|
||||||
if not content:
|
if not content:
|
||||||
@@ -214,8 +213,7 @@ class DefaultReplyer:
|
|||||||
return False, None
|
return False, None
|
||||||
|
|
||||||
async def rewrite_reply_with_context(
|
async def rewrite_reply_with_context(
|
||||||
self,
|
self, reply_data: Dict[str, Any], enable_splitter: bool = True, enable_chinese_typo: bool = True
|
||||||
reply_data: Dict[str, Any],
|
|
||||||
) -> Tuple[bool, Optional[List[str]]]:
|
) -> Tuple[bool, Optional[List[str]]]:
|
||||||
"""
|
"""
|
||||||
表达器 (Expressor): 核心逻辑,负责生成回复文本。
|
表达器 (Expressor): 核心逻辑,负责生成回复文本。
|
||||||
@@ -252,7 +250,7 @@ class DefaultReplyer:
|
|||||||
logger.error(f"{self.log_prefix}LLM 生成失败: {llm_e}")
|
logger.error(f"{self.log_prefix}LLM 生成失败: {llm_e}")
|
||||||
return False, None # LLM 调用失败则无法生成回复
|
return False, None # LLM 调用失败则无法生成回复
|
||||||
|
|
||||||
processed_response = process_llm_response(content)
|
processed_response = process_llm_response(content, enable_splitter, enable_chinese_typo)
|
||||||
|
|
||||||
# 5. 处理 LLM 响应
|
# 5. 处理 LLM 响应
|
||||||
if not content:
|
if not content:
|
||||||
|
|||||||
@@ -321,7 +321,7 @@ def random_remove_punctuation(text: str) -> str:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def process_llm_response(text: str) -> list[str]:
|
def process_llm_response(text: str, enable_splitter: bool = True, enable_chinese_typo: bool = True) -> list[str]:
|
||||||
if not global_config.response_post_process.enable_response_post_process:
|
if not global_config.response_post_process.enable_response_post_process:
|
||||||
return [text]
|
return [text]
|
||||||
|
|
||||||
@@ -359,14 +359,14 @@ def process_llm_response(text: str) -> list[str]:
|
|||||||
word_replace_rate=global_config.chinese_typo.word_replace_rate,
|
word_replace_rate=global_config.chinese_typo.word_replace_rate,
|
||||||
)
|
)
|
||||||
|
|
||||||
if global_config.response_splitter.enable:
|
if global_config.response_splitter.enable and enable_splitter:
|
||||||
split_sentences = split_into_sentences_w_remove_punctuation(cleaned_text)
|
split_sentences = split_into_sentences_w_remove_punctuation(cleaned_text)
|
||||||
else:
|
else:
|
||||||
split_sentences = [cleaned_text]
|
split_sentences = [cleaned_text]
|
||||||
|
|
||||||
sentences = []
|
sentences = []
|
||||||
for sentence in split_sentences:
|
for sentence in split_sentences:
|
||||||
if global_config.chinese_typo.enable:
|
if global_config.chinese_typo.enable and enable_chinese_typo:
|
||||||
typoed_text, typo_corrections = typo_generator.create_typo_sentence(sentence)
|
typoed_text, typo_corrections = typo_generator.create_typo_sentence(sentence)
|
||||||
sentences.append(typoed_text)
|
sentences.append(typoed_text)
|
||||||
if typo_corrections:
|
if typo_corrections:
|
||||||
|
|||||||
@@ -73,6 +73,8 @@ async def generate_reply(
|
|||||||
chat_stream=None,
|
chat_stream=None,
|
||||||
action_data: Dict[str, Any] = None,
|
action_data: Dict[str, Any] = None,
|
||||||
chat_id: str = None,
|
chat_id: str = None,
|
||||||
|
enable_splitter: bool = True,
|
||||||
|
enable_chinese_typo: bool = True,
|
||||||
) -> Tuple[bool, List[Tuple[str, Any]]]:
|
) -> Tuple[bool, List[Tuple[str, Any]]]:
|
||||||
"""生成回复
|
"""生成回复
|
||||||
|
|
||||||
@@ -80,6 +82,8 @@ async def generate_reply(
|
|||||||
chat_stream: 聊天流对象(优先)
|
chat_stream: 聊天流对象(优先)
|
||||||
action_data: 动作数据
|
action_data: 动作数据
|
||||||
chat_id: 聊天ID(备用)
|
chat_id: 聊天ID(备用)
|
||||||
|
enable_splitter: 是否启用消息分割器
|
||||||
|
enable_chinese_typo: 是否启用错字生成器
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Tuple[bool, List[Tuple[str, Any]]]: (是否成功, 回复集合)
|
Tuple[bool, List[Tuple[str, Any]]]: (是否成功, 回复集合)
|
||||||
@@ -95,7 +99,7 @@ async def generate_reply(
|
|||||||
|
|
||||||
# 调用回复器生成回复
|
# 调用回复器生成回复
|
||||||
success, reply_set = await replyer.generate_reply_with_context(
|
success, reply_set = await replyer.generate_reply_with_context(
|
||||||
reply_data=action_data or {},
|
reply_data=action_data or {}, enable_splitter=enable_splitter, enable_chinese_typo=enable_chinese_typo
|
||||||
)
|
)
|
||||||
|
|
||||||
if success:
|
if success:
|
||||||
@@ -114,6 +118,8 @@ async def rewrite_reply(
|
|||||||
chat_stream=None,
|
chat_stream=None,
|
||||||
reply_data: Dict[str, Any] = None,
|
reply_data: Dict[str, Any] = None,
|
||||||
chat_id: str = None,
|
chat_id: str = None,
|
||||||
|
enable_splitter: bool = True,
|
||||||
|
enable_chinese_typo: bool = True,
|
||||||
) -> Tuple[bool, List[Tuple[str, Any]]]:
|
) -> Tuple[bool, List[Tuple[str, Any]]]:
|
||||||
"""重写回复
|
"""重写回复
|
||||||
|
|
||||||
@@ -121,6 +127,8 @@ async def rewrite_reply(
|
|||||||
chat_stream: 聊天流对象(优先)
|
chat_stream: 聊天流对象(优先)
|
||||||
reply_data: 回复数据
|
reply_data: 回复数据
|
||||||
chat_id: 聊天ID(备用)
|
chat_id: 聊天ID(备用)
|
||||||
|
enable_splitter: 是否启用消息分割器
|
||||||
|
enable_chinese_typo: 是否启用错字生成器
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Tuple[bool, List[Tuple[str, Any]]]: (是否成功, 回复集合)
|
Tuple[bool, List[Tuple[str, Any]]]: (是否成功, 回复集合)
|
||||||
@@ -136,7 +144,7 @@ async def rewrite_reply(
|
|||||||
|
|
||||||
# 调用回复器重写回复
|
# 调用回复器重写回复
|
||||||
success, reply_set = await replyer.rewrite_reply_with_context(
|
success, reply_set = await replyer.rewrite_reply_with_context(
|
||||||
reply_data=reply_data or {},
|
reply_data=reply_data or {}, enable_splitter=enable_splitter, enable_chinese_typo=enable_chinese_typo
|
||||||
)
|
)
|
||||||
|
|
||||||
if success:
|
if success:
|
||||||
|
|||||||
@@ -108,6 +108,8 @@ class BaseAction(ABC):
|
|||||||
# print(self.chat_stream.group_info)
|
# print(self.chat_stream.group_info)
|
||||||
if self.chat_stream.group_info:
|
if self.chat_stream.group_info:
|
||||||
self.is_group = True
|
self.is_group = True
|
||||||
|
self.user_id = str(self.chat_stream.user_info.user_id)
|
||||||
|
self.user_nickname = getattr(self.chat_stream.user_info, "user_nickname", None)
|
||||||
self.group_id = str(self.chat_stream.group_info.group_id)
|
self.group_id = str(self.chat_stream.group_info.group_id)
|
||||||
self.group_name = getattr(self.chat_stream.group_info, "group_name", None)
|
self.group_name = getattr(self.chat_stream.group_info, "group_name", None)
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user