Refactor config system to use Pydantic validation
Refactored configuration classes to inherit from a new ValidatedConfigBase using Pydantic for robust validation and error reporting. Updated api_ada_configs.py, config.py, config_base.py, and official_configs.py to replace dataclasses with Pydantic models, add field validation, and improve error messages. This change enhances configuration reliability and developer feedback for misconfigurations. Also includes minor code cleanups and removal of unused variables in other modules.
This commit is contained in:
@@ -12,7 +12,6 @@ LLM反注入系统主模块
|
||||
"""
|
||||
|
||||
import time
|
||||
import asyncio
|
||||
import re
|
||||
from typing import Optional, Tuple, Dict, Any
|
||||
import datetime
|
||||
@@ -28,13 +27,7 @@ from .command_skip_list import should_skip_injection_detection, initialize_skip_
|
||||
# 数据库相关导入
|
||||
from src.common.database.sqlalchemy_models import BanUser, AntiInjectionStats, get_db_session
|
||||
|
||||
# 导入LLM API用于反击
|
||||
try:
|
||||
from src.plugin_system.apis import llm_api
|
||||
LLM_API_AVAILABLE = True
|
||||
except ImportError:
|
||||
llm_api = None
|
||||
LLM_API_AVAILABLE = False
|
||||
from src.plugin_system.apis import llm_api
|
||||
|
||||
logger = get_logger("anti_injector")
|
||||
|
||||
@@ -146,9 +139,6 @@ class AntiPromptInjector:
|
||||
生成的反击消息,如果生成失败则返回None
|
||||
"""
|
||||
try:
|
||||
if not LLM_API_AVAILABLE:
|
||||
logger.warning("LLM API不可用,无法生成反击消息")
|
||||
return None
|
||||
|
||||
# 获取可用的模型配置
|
||||
models = llm_api.get_available_models()
|
||||
|
||||
@@ -188,7 +188,7 @@ class CommandSkipListManager:
|
||||
return False, None
|
||||
|
||||
# 检查所有跳过模式
|
||||
for pattern_key, skip_pattern in self._skip_patterns.items():
|
||||
for _pattern_key, skip_pattern in self._skip_patterns.items():
|
||||
try:
|
||||
if skip_pattern.compiled_pattern.search(message_text):
|
||||
logger.debug(f"消息匹配跳过模式: {skip_pattern.pattern} ({skip_pattern.description})")
|
||||
|
||||
@@ -938,11 +938,10 @@ class EmojiManager:
|
||||
with get_db_session() as session:
|
||||
# from src.common.database.database_model_compat import Images
|
||||
|
||||
stmt = select(Images).where((Images.emoji_hash == image_hash) & (Images.type == "emoji"))
|
||||
existing_image = session.query(Images).filter((Images.emoji_hash == image_hash) & (Images.type == "emoji")).one_or_none()
|
||||
if existing_image and existing_image.description:
|
||||
existing_description = existing_image.description
|
||||
logger.info(f"[复用描述] 找到已有详细描述: {existing_description[:50]}...")
|
||||
existing_image = session.query(Images).filter((Images.emoji_hash == image_hash) & (Images.type == "emoji")).one_or_none()
|
||||
if existing_image and existing_image.description:
|
||||
existing_description = existing_image.description
|
||||
logger.info(f"[复用描述] 找到已有详细描述: {existing_description[:50]}...")
|
||||
except Exception as e:
|
||||
logger.debug(f"查询已有描述时出错: {e}")
|
||||
|
||||
|
||||
@@ -1615,7 +1615,6 @@ class ParahippocampalGyrus:
|
||||
|
||||
# 检查节点内是否有相似的记忆项需要整合
|
||||
if len(memory_items) > 1:
|
||||
merged_in_this_node = False
|
||||
items_to_remove = []
|
||||
|
||||
for i in range(len(memory_items)):
|
||||
@@ -1630,7 +1629,6 @@ class ParahippocampalGyrus:
|
||||
if shorter_item not in items_to_remove:
|
||||
items_to_remove.append(shorter_item)
|
||||
merged_count += 1
|
||||
merged_in_this_node = True
|
||||
logger.debug(f"[整合] 在节点 {node} 中合并相似记忆: {shorter_item[:30]}... -> {longer_item[:30]}...")
|
||||
|
||||
# 移除被合并的记忆项
|
||||
|
||||
@@ -169,7 +169,7 @@ class VideoAnalyzer:
|
||||
prompt += f"\n\n用户问题: {user_question}"
|
||||
|
||||
# 添加帧信息到提示词
|
||||
for i, (frame_base64, timestamp) in enumerate(frames):
|
||||
for i, (_frame_base64, timestamp) in enumerate(frames):
|
||||
if self.enable_frame_timing:
|
||||
prompt += f"\n\n第{i+1}帧 (时间: {timestamp:.2f}s):"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user