feat(maizone): 为说说生成增加跨上下文聊天记录参考
现在,maizone插件可以利用指定用户的近期聊天记录作为上下文,来生成更加贴合近期对话内容和情绪的说说。 - 新增 `cross_context.user_id` 配置项,用于指定获取上下文的目标用户。 - 在生成说说前,通过 `cross_context_api` 获取并整合聊天记录到LLM的提示词中。 - 这使得生成的说说内容能够反映最近的讨论,更具个性化和时效性。
This commit is contained in:
@@ -83,6 +83,9 @@ class MaiZoneRefactoredPlugin(BasePlugin):
|
|||||||
"http_fallback_port": ConfigField(type=int, default=9999, description="备用Cookie获取服务的端口"),
|
"http_fallback_port": ConfigField(type=int, default=9999, description="备用Cookie获取服务的端口"),
|
||||||
"napcat_token": ConfigField(type=str, default="", description="Napcat服务的认证Token(可选)"),
|
"napcat_token": ConfigField(type=str, default="", description="Napcat服务的认证Token(可选)"),
|
||||||
},
|
},
|
||||||
|
"cross_context": {
|
||||||
|
"user_id": ConfigField(type=str, default="", description="用于获取互通上下文的目标用户QQ号"),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
permission_nodes: list[PermissionNodeField] = [
|
permission_nodes: list[PermissionNodeField] = [
|
||||||
|
|||||||
@@ -274,7 +274,7 @@ class ContentService:
|
|||||||
await asyncio.sleep(2)
|
await asyncio.sleep(2)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def generate_story_from_activity(self, activity: str) -> str:
|
async def generate_story_from_activity(self, activity: str, context: str | None = None) -> str:
|
||||||
"""
|
"""
|
||||||
根据当前的日程活动生成一条QQ空间说说。
|
根据当前的日程活动生成一条QQ空间说说。
|
||||||
|
|
||||||
@@ -350,6 +350,9 @@ class ContentService:
|
|||||||
- 鼓励你多描述日常生活相关的生产活动和消遣,展现真实,而不是浮在空中。
|
- 鼓励你多描述日常生活相关的生产活动和消遣,展现真实,而不是浮在空中。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# 如果有上下文,则加入到prompt中
|
||||||
|
if context:
|
||||||
|
prompt += f"\n作为参考,这里有一些最近的聊天记录:\n---\n{context}\n---"
|
||||||
# 添加历史记录避免重复
|
# 添加历史记录避免重复
|
||||||
prompt += "\n\n---历史说说记录---\n"
|
prompt += "\n\n---历史说说记录---\n"
|
||||||
history_block = await get_send_history(qq_account)
|
history_block = await get_send_history(qq_account)
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import orjson
|
|||||||
|
|
||||||
from src.common.logger import get_logger
|
from src.common.logger import get_logger
|
||||||
from src.plugin_system.apis import config_api, person_api
|
from src.plugin_system.apis import config_api, person_api
|
||||||
|
from src.plugin_system.apis import cross_context_api
|
||||||
|
|
||||||
from .content_service import ContentService
|
from .content_service import ContentService
|
||||||
from .cookie_service import CookieService
|
from .cookie_service import CookieService
|
||||||
@@ -60,10 +61,32 @@ class QZoneService:
|
|||||||
self.processing_comments = set()
|
self.processing_comments = set()
|
||||||
|
|
||||||
# --- Public Methods (High-Level Business Logic) ---
|
# --- Public Methods (High-Level Business Logic) ---
|
||||||
|
async def _get_cross_context(self) -> str:
|
||||||
|
"""获取并构建跨群聊上下文"""
|
||||||
|
context = ""
|
||||||
|
user_id = self.get_config("cross_context.user_id")
|
||||||
|
|
||||||
|
if user_id:
|
||||||
|
logger.info(f"检测到互通组用户ID: {user_id},准备获取上下文...")
|
||||||
|
try:
|
||||||
|
context = await cross_context_api.build_cross_context_for_user(
|
||||||
|
user_id=user_id,
|
||||||
|
platform="QQ", # 硬编码为QQ
|
||||||
|
limit_per_stream=10,
|
||||||
|
stream_limit=3,
|
||||||
|
)
|
||||||
|
if context:
|
||||||
|
logger.info("成功获取到互通组上下文。")
|
||||||
|
else:
|
||||||
|
logger.info("未获取到有效的互通组上下文。")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"获取互通组上下文时发生异常: {e}")
|
||||||
|
return context
|
||||||
|
|
||||||
async def send_feed(self, topic: str, stream_id: str | None) -> dict[str, Any]:
|
async def send_feed(self, topic: str, stream_id: str | None) -> dict[str, Any]:
|
||||||
"""发送一条说说"""
|
"""发送一条说说"""
|
||||||
story = await self.content_service.generate_story(topic, context=None)
|
cross_context = await self._get_cross_context()
|
||||||
|
story = await self.content_service.generate_story(topic, context=cross_context)
|
||||||
if not story:
|
if not story:
|
||||||
return {"success": False, "message": "生成说说内容失败"}
|
return {"success": False, "message": "生成说说内容失败"}
|
||||||
|
|
||||||
@@ -88,7 +111,8 @@ class QZoneService:
|
|||||||
|
|
||||||
async def send_feed_from_activity(self, activity: str) -> dict[str, Any]:
|
async def send_feed_from_activity(self, activity: str) -> dict[str, Any]:
|
||||||
"""根据日程活动发送一条说说"""
|
"""根据日程活动发送一条说说"""
|
||||||
story = await self.content_service.generate_story_from_activity(activity)
|
cross_context = await self._get_cross_context()
|
||||||
|
story = await self.content_service.generate_story_from_activity(activity, context=cross_context)
|
||||||
if not story:
|
if not story:
|
||||||
return {"success": False, "message": "根据活动生成说说内容失败"}
|
return {"success": False, "message": "根据活动生成说说内容失败"}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user