Files
zhenxun_bot/zhenxun/services/ai/llm/system/cache.py
T
922d092650 ♻️ refactor(core): 重构 AI 能力与定时任务调度系统 (#2148)
* ♻️ refactor(core): 重构 AI 能力与定时任务调度系统

- 【AI 能力与工具】重构 Capability 注册与管理机制,引入 CapabilityManager 统一管理
- 移除全局能力注册表,改用声明式装饰器 `@capability` 进行解耦注册
- 重构工具解析器链,使用统一的 BaseToolResolver 代替原有的多个特定解析器
- 增强工具查询过滤,支持通配符匹配、工具箱过滤和排除标签
- 【定时任务调度】重构定时任务管理器,引入 SchedulerRegistry 统一管理任务元数据
- 引入 JobConfig 聚合定时任务配置,支持用户维度的定时任务调度
- 重构执行分发器,支持并发限制、串行间隔和随机延迟打散
- 【运行上下文】引入 ScheduledDeps 以支持后台和定时任务环境下的依赖注入
- 优化 RunContext,支持从定时任务上下文快速构造,并提供 emit 辅助方法
- 【日志与监控】引入 AILoggerProxy,实现 AI 各模块的专属日志输出
- 将各模块的全局 logger 替换为对应的模块专属日志代理
- 【其他优化】修复 Pydantic V1 兼容层中 model_validator 的装饰器兼容性问题
- 在非交互式环境(如定时任务)中自动隐藏 HITL 交互工具以节省 Token

* ♻️ refactor(core): 优化内部导入路径并提升 Pydantic 兼容性

- 【重构】将 `services/ai` 模块内的绝对导入重构为相对导入,优化包结构
- 【重构】移除不必要的 `if TYPE_CHECKING` 保护,通过 `from __future__ import annotations` 直接导入类型
- 【清理】清理 `core/messages/types.py` 中未使用的 `AssistantContentUnion` 等联合类型定义
- 【优化】在 `utils/pydantic_compat.py` 中新增 `model_rebuild` 兼容函数,统一 Pydantic V1/V2 的模型重建逻辑
- 【优化】将部分函数内部的延迟导入提升至模块顶部,规范代码结构

* ♻️ refactor(imports): 优化导入路径为相对导入并清理冗余导入

- 【重构】将 AI 服务相关模块中的绝对导入路径修改为相对导入,提升模块内聚性与可移植性
- 【清理】移除多处函数内部或类方法中未使用的冗余导入,避免循环引用和资源浪费
- 【格式化】微调部分工具装饰器和返回语句的格式与尾随逗号

* 🚨 auto fix by pre-commit hooks

---------

Co-authored-by: webjoin111 <455457521@qq.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-07-10 09:14:06 +08:00

195 lines
7.4 KiB
Python

import hashlib
import time
from typing import Any
from zhenxun.services.ai.config import ProviderConfig, get_llm_config
from zhenxun.services.ai.core.exceptions import ConfigurationException, LLMException
from zhenxun.services.ai.core.models import ModelDetail, ModelModality
from zhenxun.services.ai.core.options import GenerationConfig
from zhenxun.services.ai.llm.builder import validate_override_params
from zhenxun.services.ai.llm.engine.service import LLMModel
from zhenxun.services.ai.utils.logger import log_llm as logger
from zhenxun.utils.pydantic_compat import dump_json_safely, model_copy, model_dump
from .capabilities import get_model_capabilities
from .network import health_manager, http_client_manager
_model_cache: dict[str, tuple[LLMModel, float]] = {}
_cache_ttl = 3600
_max_cache_size = 10
def _make_cache_key(
provider_name: str,
model_name: str,
override_config: dict | GenerationConfig | None,
) -> str:
"""生成缓存键"""
config_str = (
dump_json_safely(override_config, sort_keys=True) if override_config else "None"
)
key_data = f"{provider_name}/{model_name}:{config_str}"
return hashlib.md5(key_data.encode()).hexdigest()
def _get_cached_model(cache_key: str) -> LLMModel | None:
"""从缓存获取模型"""
if cache_key in _model_cache:
model, created_time = _model_cache[cache_key]
current_time = time.time()
if current_time - created_time > _cache_ttl:
del _model_cache[cache_key]
logger.debug(f"模型缓存已过期: {cache_key}")
return None
if model._is_closed:
logger.debug(
f"缓存的模型 {cache_key} ({model.provider_name}/{model.model_name}) "
f"处于_is_closed=True状态,重置为False以供复用。"
)
model._is_closed = False
logger.debug(
f"使用缓存的模型: {cache_key} -> {model.provider_name}/{model.model_name}"
)
return model
return None
def _cache_model(cache_key: str, model: LLMModel):
"""缓存模型实例"""
current_time = time.time()
if len(_model_cache) >= _max_cache_size:
oldest_key = min(_model_cache.keys(), key=lambda k: _model_cache[k][1])
del _model_cache[oldest_key]
_model_cache[cache_key] = (model, current_time)
def clear_model_cache():
"""仅清空内存中的模型实例缓存。"""
global _model_cache
_model_cache.clear()
logger.debug("已清空模型实例缓存")
async def get_or_create_model(
provider_config_found: ProviderConfig,
model_detail_found: ModelDetail,
override_config: dict[str, Any] | GenerationConfig | None = None,
) -> Any:
"""组装或获取底层 LLMModel 状态机实例 (不包含任何字符串解析)。"""
prov_name_str = provider_config_found.name
mod_name_str = model_detail_found.model_name
cache_key = _make_cache_key(prov_name_str, mod_name_str, override_config)
cached_model = _get_cached_model(cache_key)
def _get_clean_log_config(cfg: GenerationConfig) -> dict:
"""辅助函数:剔除超长 Schema 以防止日志刷屏"""
log_dict = model_dump(cfg, exclude_none=True)
if "output" in log_dict and isinstance(log_dict["output"], dict):
if "response_schema" in log_dict["output"]:
log_dict["output"]["response_schema"] = (
"<JSON Schema Hidden for brevity>"
)
return log_dict
if cached_model:
if override_config:
validated_override = validate_override_params(override_config)
if cached_model._generation_config != validated_override:
cached_model._generation_config = validated_override
cached_model.identity.generation_config = validated_override
logger.debug(
f"对缓存模型 {prov_name_str}/{mod_name_str} 应用新的覆盖配置: "
f"{_get_clean_log_config(validated_override)}"
)
return cached_model
capabilities = get_model_capabilities(model_detail_found.model_name)
capabilities = model_copy(capabilities, deep=True)
if model_detail_found.max_input_tokens is not None:
capabilities.max_input_tokens = model_detail_found.max_input_tokens
base_gen_config = GenerationConfig()
if provider_config_found.temperature is not None:
base_gen_config.common.temperature = provider_config_found.temperature
if provider_config_found.max_output_tokens is not None:
base_gen_config.common.max_tokens = provider_config_found.max_output_tokens
if model_detail_found.temperature is not None:
base_gen_config.common.temperature = model_detail_found.temperature
if model_detail_found.max_output_tokens is not None:
base_gen_config.common.max_tokens = model_detail_found.max_output_tokens
if model_detail_found.reasoning_effort is not None:
base_gen_config.common.reasoning_effort = model_detail_found.reasoning_effort
if model_detail_found.task_type == "image_generation":
capabilities.output_modalities.add(ModelModality.IMAGE)
capabilities.supports_tool_calling = False
llm_config = get_llm_config()
client_settings = llm_config.client_settings
default_timeout = (
provider_config_found.timeout
if provider_config_found.timeout is not None
else client_settings.timeout
)
config_for_http_client = ProviderConfig(
name=provider_config_found.name,
api_key=provider_config_found.api_key,
models=provider_config_found.models,
timeout=default_timeout,
api_base=provider_config_found.api_base,
api_type=provider_config_found.api_type,
temperature=provider_config_found.temperature,
max_output_tokens=provider_config_found.max_output_tokens,
)
shared_http_client = await http_client_manager.get_client(config_for_http_client)
try:
model_instance = LLMModel(
provider_config=config_for_http_client,
model_detail=model_detail_found,
health_manager=health_manager,
http_client=shared_http_client,
capabilities=capabilities,
config_override=base_gen_config,
)
if override_config:
validated_override_params = validate_override_params(override_config)
final_config = base_gen_config.merge_with(validated_override_params)
model_instance._generation_config = final_config
model_instance.identity.generation_config = final_config
logger.debug(
f"为新模型 {prov_name_str}/{mod_name_str} 应用配置覆盖: "
f"{_get_clean_log_config(validated_override_params)}"
)
else:
model_instance._generation_config = base_gen_config
model_instance.identity.generation_config = base_gen_config
_cache_model(cache_key, model_instance)
logger.debug(
f"创建并缓存了新模型: {cache_key} -> {prov_name_str}/{mod_name_str}"
)
return model_instance
except LLMException:
raise
except Exception as e:
logger.error(
f"实例化 LLMModel ({prov_name_str}/{mod_name_str}) 时发生内部错误: {e!s}",
e=e,
)
raise ConfigurationException(
f"初始化模型 '{prov_name_str}/{mod_name_str}' 失败: {e!s}",
cause=e,
)