refactor(maizone): 升级上下文检索为用户中心模式
此前的跨群聊上下文依赖于一个固定的互通组 (`maizone_context_group`),这种方式不够灵活且上下文相关性较弱。 本次重构将上下文获取逻辑全面切换至新引入的 `get_user_centric_context` API,以提升上下文的精准度和相关性。 - **内容生成 (`content_service`)**: 现在会检索机器人自身在所有聊天中的近期发言作为参考,使新生成的内容更贴近其当前活动与话题。 - **评论回复 (`qzone_service`)**: 在回复用户评论时,会检索该用户与机器人在其他聊天中的对话记录,从而生成更具个性化和情境感知能力的回复。
This commit is contained in:
@@ -13,11 +13,11 @@ import aiohttp
|
||||
from maim_message import UserInfo
|
||||
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
from src.plugin_system.apis.cross_context_api import get_user_centric_context
|
||||
from src.common.logger import get_logger
|
||||
from src.config.api_ada_configs import TaskConfig
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
from src.plugin_system.apis import config_api, generator_api, llm_api
|
||||
from src.plugin_system.apis.cross_context_api import get_chat_history_by_group_name
|
||||
|
||||
# 导入旧的工具函数,我们稍后会考虑是否也需要重构它
|
||||
from ..utils.history_utils import get_send_history
|
||||
@@ -89,9 +89,13 @@ class ContentService:
|
||||
prompt += f"\n作为参考,这里有一些最近的聊天记录:\n---\n{context}\n---"
|
||||
|
||||
# 添加跨群聊上下文
|
||||
cross_context = await get_chat_history_by_group_name("maizone_context_group")
|
||||
if cross_context and "找不到名为" not in cross_context:
|
||||
prompt += f"\n\n---跨群聊参考---\n{cross_context}\n---"
|
||||
bot_user_id = config_api.get_global_config("bot.qq_account")
|
||||
if bot_user_id:
|
||||
cross_context = await get_user_centric_context(
|
||||
user_id=bot_user_id, platform="qq", limit=5
|
||||
) # limit可以根据需要调整
|
||||
if cross_context:
|
||||
prompt += f"\n\n---跨群聊参考---\n{cross_context}\n---"
|
||||
|
||||
# 添加历史记录以避免重复
|
||||
prompt += "\n\n---历史说说记录---\n"
|
||||
|
||||
@@ -187,10 +187,28 @@ class QZoneService:
|
||||
|
||||
async def _get_intercom_context(self, stream_id: str) -> str | None:
|
||||
"""
|
||||
获取互通组的聊天上下文。
|
||||
获取用户的跨场景聊天上下文。
|
||||
"""
|
||||
# 实际的逻辑已迁移到 cross_context_api
|
||||
return await cross_context_api.get_intercom_group_context_by_name("maizone_context_group")
|
||||
if not stream_id:
|
||||
return None
|
||||
|
||||
from src.chat.message_receive.chat_stream import get_chat_manager
|
||||
chat_manager = get_chat_manager()
|
||||
stream = await chat_manager.get_stream(stream_id)
|
||||
|
||||
if not stream or not stream.user_info:
|
||||
return None
|
||||
|
||||
user_id = stream.user_info.user_id
|
||||
platform = stream.platform
|
||||
|
||||
# 调用新的以用户为中心的上下文获取函数
|
||||
return await cross_context_api.get_user_centric_context(
|
||||
user_id=user_id,
|
||||
platform=platform,
|
||||
limit=10, # 可以根据需要调整获取的消息数量
|
||||
exclude_chat_id=stream_id
|
||||
)
|
||||
|
||||
async def _reply_to_own_feed_comments(self, feed: dict, api_client: dict):
|
||||
"""处理对自己说说的评论并进行回复"""
|
||||
|
||||
Reference in New Issue
Block a user