mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
* ✨ feat(llm): 全面重构LLM服务模块,增强多模态与工具支持 🚀 核心功能增强 - 多模型链式调用:新增 `pipeline_chat` 支持复杂任务流处理 - 扩展提供商支持:新增 ARK(火山方舟)、SiliconFlow(硅基流动) 适配器 - 多模态处理增强:支持URL媒体文件下载转换,提升输入灵活性 - 历史对话支持:AI.analyze 方法支持历史消息上下文和可选 UniMessage 参数 - 文本嵌入功能:新增 `embed`、`analyze_multimodal`、`search_multimodal` 等API - 模型能力系统:新增 `ModelCapabilities` 统一管理模型特性(多模态、工具调用等) 🔧 架构重构与优化 - MCP工具系统重构:配置独立化至 `data/llm/mcp_tools.json`,预置常用工具 - API调用逻辑统一:提取通用 `_perform_api_call` 方法,消除代码重复 - 跨平台兼容:Windows平台MCP工具npx命令自动包装处理 - HTTP客户端增强:兼容不同版本httpx代理配置(0.28+版本适配) 🛠️ API与配置完善 - 统一返回类型:`AI.analyze` 统一返回 `LLMResponse` 类型 - 消息转换工具:新增 `message_to_unimessage` 转换函数 - Gemini适配器增强:URL图片下载编码、动态安全阈值配置 - 缓存管理:新增模型实例缓存和管理功能 - 配置预设:扩展 CommonOverrides 预设配置选项 - 历史管理优化:支持多模态内容占位符替换,提升效率 📚 文档与开发体验 - README全面重写:新增完整使用指南、API参考和架构概览 - 文档内容扩充:补充嵌入模型、缓存管理、工具注册等功能说明 - 日志记录增强:支持详细调试信息输出 - API简化:移除冗余函数,优化接口设计 * 🎨 feat(llm): 统一LLM服务函数文档格式 * ✨ feat(llm): 添加新模型并简化提供者配置加载 * 🚨 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>
77 lines
2.1 KiB
Python
77 lines
2.1 KiB
Python
"""
|
|
LLM 适配器工厂类
|
|
"""
|
|
|
|
from typing import ClassVar
|
|
|
|
from ..types.exceptions import LLMErrorCode, LLMException
|
|
from .base import BaseAdapter
|
|
|
|
|
|
class LLMAdapterFactory:
|
|
"""LLM适配器工厂类"""
|
|
|
|
_adapters: ClassVar[dict[str, BaseAdapter]] = {}
|
|
_api_type_mapping: ClassVar[dict[str, str]] = {}
|
|
|
|
@classmethod
|
|
def initialize(cls) -> None:
|
|
"""初始化默认适配器"""
|
|
if cls._adapters:
|
|
return
|
|
|
|
from .gemini import GeminiAdapter
|
|
from .openai import OpenAIAdapter
|
|
|
|
cls.register_adapter(OpenAIAdapter())
|
|
cls.register_adapter(GeminiAdapter())
|
|
|
|
@classmethod
|
|
def register_adapter(cls, adapter: BaseAdapter) -> None:
|
|
"""注册适配器"""
|
|
adapter_key = adapter.api_type
|
|
cls._adapters[adapter_key] = adapter
|
|
|
|
for api_type in adapter.supported_api_types:
|
|
cls._api_type_mapping[api_type] = adapter_key
|
|
|
|
@classmethod
|
|
def get_adapter(cls, api_type: str) -> BaseAdapter:
|
|
"""获取适配器"""
|
|
cls.initialize()
|
|
|
|
adapter_key = cls._api_type_mapping.get(api_type)
|
|
if not adapter_key:
|
|
raise LLMException(
|
|
f"不支持的API类型: {api_type}",
|
|
code=LLMErrorCode.UNKNOWN_API_TYPE,
|
|
details={
|
|
"api_type": api_type,
|
|
"supported_types": list(cls._api_type_mapping.keys()),
|
|
},
|
|
)
|
|
|
|
return cls._adapters[adapter_key]
|
|
|
|
@classmethod
|
|
def list_supported_types(cls) -> list[str]:
|
|
"""列出所有支持的API类型"""
|
|
cls.initialize()
|
|
return list(cls._api_type_mapping.keys())
|
|
|
|
@classmethod
|
|
def list_adapters(cls) -> dict[str, BaseAdapter]:
|
|
"""列出所有注册的适配器"""
|
|
cls.initialize()
|
|
return cls._adapters.copy()
|
|
|
|
|
|
def get_adapter_for_api_type(api_type: str) -> BaseAdapter:
|
|
"""获取指定API类型的适配器"""
|
|
return LLMAdapterFactory.get_adapter(api_type)
|
|
|
|
|
|
def register_adapter(adapter: BaseAdapter) -> None:
|
|
"""注册新的适配器"""
|
|
LLMAdapterFactory.register_adapter(adapter)
|