mirror of
https://github.com/Nezumi-2711/astrbot_plugin_qq_group_daily_analysis.git
synced 2026-09-22 13:38:43 +00:00
[fix] (LLM 提供商) 传递 unified_msg_origin 以获取正确的 LLM 提供商
This commit is contained in:
@@ -81,8 +81,6 @@ class LLMAnalyzer:
|
||||
# 确保使用当前指定的模型
|
||||
if provider is None:
|
||||
provider = self.context.get_using_provider(umo=umo)
|
||||
|
||||
# 安全地获取 provider ID
|
||||
provider_id = 'unknown'
|
||||
if provider:
|
||||
try:
|
||||
@@ -90,10 +88,7 @@ class LLMAnalyzer:
|
||||
provider_id = meta.id
|
||||
except Exception as e:
|
||||
logger.debug(f"获取提供商ID失败: {e}")
|
||||
|
||||
logger.info(f"获取到的 provider ID: {provider_id}")
|
||||
|
||||
# 如果获取失败,尝试使用全局默认提供商或者回退方案
|
||||
if not provider or provider_id == 'unknown':
|
||||
logger.warning(f"获取的提供商不正确 (Provider ID: {provider_id})")
|
||||
|
||||
@@ -118,7 +113,7 @@ class LLMAnalyzer:
|
||||
logger.error(f"LLM请求全部重试失败: {last_exc}")
|
||||
return None
|
||||
|
||||
async def analyze_topics(self, messages: List[Dict]) -> Tuple[List[SummaryTopic], TokenUsage]:
|
||||
async def analyze_topics(self, messages: List[Dict], umo: str = None) -> Tuple[List[SummaryTopic], TokenUsage]:
|
||||
"""使用LLM分析话题"""
|
||||
try:
|
||||
# 提取文本消息
|
||||
@@ -203,12 +198,7 @@ class LLMAnalyzer:
|
||||
"""
|
||||
|
||||
# 调用LLM
|
||||
provider = self.context.get_using_provider()
|
||||
if not provider:
|
||||
logger.warning("未配置LLM提供商,跳过话题分析")
|
||||
return [], TokenUsage()
|
||||
|
||||
response = await self._call_provider_with_retry(provider, prompt, max_tokens=10000, temperature=0.6)
|
||||
response = await self._call_provider_with_retry(None, prompt, max_tokens=10000, temperature=0.6, umo=umo)
|
||||
if response is None:
|
||||
logger.error("话题分析调用LLM失败: provider返回None(重试失败)")
|
||||
return [], TokenUsage()
|
||||
@@ -359,7 +349,7 @@ class LLMAnalyzer:
|
||||
logger.error(f"正则表达式提取失败: {e}")
|
||||
return []
|
||||
|
||||
async def analyze_user_titles(self, messages: List[Dict], user_analysis: Dict) -> Tuple[List[UserTitle], TokenUsage]:
|
||||
async def analyze_user_titles(self, messages: List[Dict], user_analysis: Dict, umo: str = None) -> Tuple[List[UserTitle], TokenUsage]:
|
||||
"""使用LLM分析用户称号"""
|
||||
try:
|
||||
# 准备用户数据
|
||||
@@ -430,12 +420,7 @@ class LLMAnalyzer:
|
||||
"""
|
||||
|
||||
# 调用LLM
|
||||
provider = self.context.get_using_provider()
|
||||
if not provider:
|
||||
logger.warning("未配置LLM提供商,跳过用户称号分析")
|
||||
return [], TokenUsage()
|
||||
|
||||
response = await self._call_provider_with_retry(provider, prompt, max_tokens=1500, temperature=0.5)
|
||||
response = await self._call_provider_with_retry(None, prompt, max_tokens=1500, temperature=0.5, umo=umo)
|
||||
if response is None:
|
||||
logger.error("用户称号分析调用LLM失败: provider返回None(重试失败)")
|
||||
return [], TokenUsage()
|
||||
@@ -477,7 +462,7 @@ class LLMAnalyzer:
|
||||
logger.error(f"用户称号分析失败: {e}")
|
||||
return [], TokenUsage()
|
||||
|
||||
async def analyze_golden_quotes(self, messages: List[Dict]) -> Tuple[List[GoldenQuote], TokenUsage]:
|
||||
async def analyze_golden_quotes(self, messages: List[Dict], umo: str = None) -> Tuple[List[GoldenQuote], TokenUsage]:
|
||||
"""使用LLM分析群聊金句"""
|
||||
try:
|
||||
# 提取有趣的文本消息
|
||||
@@ -538,12 +523,7 @@ class LLMAnalyzer:
|
||||
"""
|
||||
|
||||
# 调用LLM
|
||||
provider = self.context.get_using_provider()
|
||||
if not provider:
|
||||
logger.warning("未配置LLM提供商,跳过金句分析")
|
||||
return [], TokenUsage()
|
||||
|
||||
response = await self._call_provider_with_retry(provider, prompt, max_tokens=1500, temperature=0.7)
|
||||
response = await self._call_provider_with_retry(None, prompt, max_tokens=1500, temperature=0.7, umo=umo)
|
||||
if response is None:
|
||||
logger.error("金句分析调用LLM失败: provider返回None(重试失败)")
|
||||
return [], TokenUsage()
|
||||
|
||||
@@ -30,6 +30,21 @@ class AutoScheduler:
|
||||
"""设置bot QQ号(保持向后兼容)"""
|
||||
self.bot_manager.set_bot_qq_id(bot_qq_id)
|
||||
|
||||
def _get_platform_id(self):
|
||||
"""获取平台ID"""
|
||||
try:
|
||||
if hasattr(self.bot_manager, '_context') and self.bot_manager._context:
|
||||
context = self.bot_manager._context
|
||||
if hasattr(context, 'platform_manager') and hasattr(context.platform_manager, 'platform_insts'):
|
||||
platforms = context.platform_manager.platform_insts
|
||||
for platform in platforms:
|
||||
if hasattr(platform, 'metadata') and hasattr(platform.metadata, 'id'):
|
||||
platform_id = platform.metadata.id
|
||||
return platform_id
|
||||
return "aiocqhttp" # 默认值
|
||||
except Exception as e:
|
||||
return "aiocqhttp" # 默认值
|
||||
|
||||
async def start_scheduler(self):
|
||||
"""启动定时任务调度器"""
|
||||
if not self.config_manager.get_enable_auto_analysis():
|
||||
@@ -144,8 +159,10 @@ class AutoScheduler:
|
||||
|
||||
logger.info(f"群 {group_id} 获取到 {len(messages)} 条消息,开始分析")
|
||||
|
||||
# 进行分析
|
||||
analysis_result = await self.analyzer.analyze_messages(messages, group_id)
|
||||
# 进行分析 - 构造正确的 unified_msg_origin
|
||||
platform_id = self._get_platform_id()
|
||||
umo = f"{platform_id}:group:{group_id}" if platform_id else None
|
||||
analysis_result = await self.analyzer.analyze_messages(messages, group_id, umo)
|
||||
if not analysis_result:
|
||||
logger.error(f"群 {group_id} 分析失败")
|
||||
return
|
||||
|
||||
@@ -28,7 +28,7 @@ class MessageAnalyzer:
|
||||
else:
|
||||
await self.message_handler.set_bot_qq_id(bot_instance)
|
||||
|
||||
async def analyze_messages(self, messages: List[Dict], group_id: str) -> Dict:
|
||||
async def analyze_messages(self, messages: List[Dict], group_id: str, unified_msg_origin: str = None) -> Dict:
|
||||
"""完整的消息分析流程"""
|
||||
try:
|
||||
# 基础统计
|
||||
@@ -45,20 +45,20 @@ class MessageAnalyzer:
|
||||
|
||||
# 话题分析
|
||||
if self.config_manager.get_topic_analysis_enabled():
|
||||
topics, topic_tokens = await self.llm_analyzer.analyze_topics(messages)
|
||||
topics, topic_tokens = await self.llm_analyzer.analyze_topics(messages, umo=unified_msg_origin)
|
||||
total_token_usage.prompt_tokens += topic_tokens.prompt_tokens
|
||||
total_token_usage.completion_tokens += topic_tokens.completion_tokens
|
||||
total_token_usage.total_tokens += topic_tokens.total_tokens
|
||||
|
||||
# 用户称号分析
|
||||
if self.config_manager.get_user_title_analysis_enabled():
|
||||
user_titles, title_tokens = await self.llm_analyzer.analyze_user_titles(messages, user_analysis)
|
||||
user_titles, title_tokens = await self.llm_analyzer.analyze_user_titles(messages, user_analysis, umo=unified_msg_origin)
|
||||
total_token_usage.prompt_tokens += title_tokens.prompt_tokens
|
||||
total_token_usage.completion_tokens += title_tokens.completion_tokens
|
||||
total_token_usage.total_tokens += title_tokens.total_tokens
|
||||
|
||||
# 金句分析
|
||||
golden_quotes, quote_tokens = await self.llm_analyzer.analyze_golden_quotes(messages)
|
||||
golden_quotes, quote_tokens = await self.llm_analyzer.analyze_golden_quotes(messages, umo=unified_msg_origin)
|
||||
total_token_usage.prompt_tokens += quote_tokens.prompt_tokens
|
||||
total_token_usage.completion_tokens += quote_tokens.completion_tokens
|
||||
total_token_usage.total_tokens += quote_tokens.total_tokens
|
||||
|
||||
Reference in New Issue
Block a user