优化 ChatGPT 和 DeepSeek 类的消息处理逻辑,添加发送者名字到用户消息中,提升消息格式化的可读性。同时修复注释格式,确保代码整洁性。

This commit is contained in:
Zylan
2025-04-24 13:16:53 +08:00
parent 8227802eb0
commit 7d50b6b94d
2 changed files with 18 additions and 8 deletions

View File

@@ -75,7 +75,7 @@ class ChatGPT():
if self.message_summary and self.bot_wxid:
history = self.message_summary.get_messages(wxid)
# -限制历史消息数量
# -限制历史消息数量
if self.max_history_messages is not None and self.max_history_messages > 0:
history = history[-self.max_history_messages:] # 取最新的 N 条
elif self.max_history_messages == 0: # 如果设置为0则不包含历史
@@ -83,9 +83,14 @@ class ChatGPT():
for msg in history:
role = "assistant" if msg.get("sender_wxid") == self.bot_wxid else "user"
formatted_content = msg.get('content', '')
if formatted_content: # 避免添加空内容
api_messages.append({"role": role, "content": formatted_content})
content = msg.get('content', '')
if content: # 避免添加空内容
if role == "user":
sender_name = msg.get('sender', '未知用户') # 获取发送者名字,如果不存在则使用默认值
formatted_content = f"{sender_name}: {content}" # 格式化内容,加入发送者名字
api_messages.append({"role": role, "content": formatted_content})
else: # 如果是助手(机器人自己)的消息,则不加名字
api_messages.append({"role": role, "content": content})
else:
self.LOG.warning(f"无法为 wxid={wxid} 获取历史记录,因为 message_summary 或 bot_wxid 未设置。")

View File

@@ -71,7 +71,7 @@ class DeepSeek():
if self.message_summary and self.bot_wxid:
history = self.message_summary.get_messages(wxid)
# 限制历史消息数量
# 限制历史消息数量
if self.max_history_messages is not None and self.max_history_messages > 0:
history = history[-self.max_history_messages:] # 取最新的 N 条
elif self.max_history_messages == 0: # 如果设置为0则不包含历史
@@ -79,9 +79,14 @@ class DeepSeek():
for msg in history:
role = "assistant" if msg.get("sender_wxid") == self.bot_wxid else "user"
formatted_content = msg.get('content', '')
if formatted_content:
api_messages.append({"role": role, "content": formatted_content})
content = msg.get('content', '')
if content:
if role == "user":
sender_name = msg.get('sender', '未知用户') # 获取发送者名字
formatted_content = f"{sender_name}: {content}" # 格式化内容
api_messages.append({"role": role, "content": formatted_content})
else: # 助手消息
api_messages.append({"role": role, "content": content})
else:
self.LOG.warning(f"无法为 wxid={wxid} 获取历史记录,因为 message_summary 或 bot_wxid 未设置。")