refactor: 重构配置管理和截图功能,切换LLM提供商至智谱AI

重构配置管理器以支持打包环境路径处理
将LLM分析器从OpenAI迁移至智谱AI API
替换Playwright截图功能为Selenium实现
更新默认配置中的API端点和模型
This commit is contained in:
2026-01-27 11:11:24 +08:00
parent e8210b4d88
commit f256bd0852
5 changed files with 147 additions and 124 deletions

View File

@@ -1,12 +1,12 @@
"""
大模型分析模块 - 调用LLM API分析评论情感
支持 OpenAI 兼容 API包括 NVIDIA API
支持智谱AI API
"""
import json
import time
import re
from typing import Dict, Optional, Tuple, Any
from openai import OpenAI
from zai import ZhipuAiClient
from loguru import logger
@@ -36,8 +36,8 @@ class LLMAnalyzer:
def __init__(self, config: Dict):
self.config = config
self.base_url = config.get('base_url', '')
self.api_key = config.get('api_key', '')
self.base_url = config.get('base_url', '')
self.model = config.get('model', '')
self.timeout = config.get('timeout', 120)
self.retry_times = config.get('retry_times', 3)
@@ -47,34 +47,33 @@ class LLMAnalyzer:
logger.info(f"LLM分析器配置 - base_url: {self.base_url}, model: {self.model}, timeout: {self.timeout}s, retry: {self.retry_times}")
if self.base_url and self.api_key:
if self.api_key:
self._init_client()
else:
logger.warning("LLM API 未配置,base_url 或 api_key 为空")
logger.warning("LLM API 未配置api_key 为空")
def _init_client(self):
"""初始化OpenAI客户端"""
"""初始化智谱AI客户端"""
try:
logger.info(f"初始化LLM客户端: {self.base_url}")
self.client = OpenAI(
logger.info(f"初始化智谱AI客户端: {self.base_url}")
self.client = ZhipuAiClient(
api_key=self.api_key,
base_url=self.base_url,
timeout=self.timeout
base_url=self.base_url
)
logger.info("LLM客户端初始化成功")
logger.info("智谱AI客户端初始化成功")
except Exception as e:
logger.error(f"初始化LLM客户端失败: {e}")
logger.error(f"初始化智谱AI客户端失败: {e}")
def update_config(self, config: Dict):
"""更新配置"""
self.config.update(config)
self.base_url = config.get('base_url', self.base_url)
self.api_key = config.get('api_key', self.api_key)
self.base_url = config.get('base_url', self.base_url)
self.model = config.get('model', self.model)
self.timeout = config.get('timeout', self.timeout)
self.retry_times = config.get('retry_times', self.retry_times)
if self.base_url and self.api_key:
if self.api_key:
self._init_client()
def analyze(self, comment: str) -> Tuple[Optional[int], Optional[str]]:
@@ -97,16 +96,18 @@ class LLMAnalyzer:
try:
logger.info(f"API调用尝试 {attempt + 1}/{self.retry_times}")
logger.debug(f"发送请求到 {self.base_url}")
logger.debug("发送请求到智谱AI API")
response = self.client.chat.completions.create(
model=self.model,
model="glm-4.7-flash",
messages=[
{"role": "system", "content": self.SYSTEM_PROMPT},
{"role": "user", "content": f"请分析以下评论的情感倾向:\n\n{comment}"}
],
thinking={
"type": "disabled", # 禁用深度思考模式
},
temperature=0.3,
max_tokens=500,
timeout=self.timeout
max_tokens=500
)
# 处理 deepseek-r1 的特殊结构(可能有 reasoning_content