using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace OASystem.API.OAMethodLib.TokenHubAI; public static class TokenHubServiceExtensions { /// /// 注册 TokenHub 服务到 DI 容器(使用 API Key + 配置委托) /// /// 服务集合 /// TokenHub API 密钥 /// API 端点地址(可选) /// 配置委托 /// 服务集合 public static IServiceCollection AddTokenHubService( this IServiceCollection services, string apiKey, string? baseUrl = null, Action? configure = null) { var options = new TokenHubServiceOptions(); configure?.Invoke(options); services.AddSingleton(provider => { var logger = provider.GetService>(); var service = new TokenHubService(apiKey, baseUrl, logger); if (options.Timeout.HasValue) service.SetTimeout(options.Timeout.Value); if (options.MaxRetries > 0) { service.SetRetryPolicy( options.MaxRetries, options.InitialRetryDelay ?? TimeSpan.FromSeconds(1), options.MaxRetryDelay ?? TimeSpan.FromSeconds(30) ); } return service; }); return services; } /// /// 注册 TokenHub 服务到 DI 容器(使用配置对象) /// /// 服务集合 /// TokenHub API 密钥 /// API 端点地址(可选) /// 配置对象 /// 服务集合 public static IServiceCollection AddTokenHubService( this IServiceCollection services, string apiKey, string? baseUrl, TokenHubServiceOptions options) { services.AddSingleton(provider => { var logger = provider.GetService>(); var service = new TokenHubService(apiKey, baseUrl, logger); if (options.Timeout.HasValue) service.SetTimeout(options.Timeout.Value); if (options.MaxRetries > 0) { service.SetRetryPolicy( options.MaxRetries, options.InitialRetryDelay ?? TimeSpan.FromSeconds(1), options.MaxRetryDelay ?? TimeSpan.FromSeconds(30) ); } return service; }); return services; } /// /// 从 IConfiguration 注册 TokenHub 服务 /// /// 服务集合 /// 应用程序配置 /// 配置节名称,默认 "TokenHub" /// 服务集合 /// 当未找到 ApiKey 时抛出 public static IServiceCollection AddTokenHubService( this IServiceCollection services, IConfiguration configuration, string configSection = "TokenHub") { var config = configuration.GetSection(configSection); var tokenHubConfig = new TokenHubConfiguration(); config.Bind(tokenHubConfig); if (string.IsNullOrWhiteSpace(tokenHubConfig.ApiKey)) throw new InvalidOperationException( $"未找到 TokenHub API Key 配置: {configSection}:ApiKey"); // 构建选项 var options = new TokenHubServiceOptions { MaxRetries = tokenHubConfig.MaxRetries, Timeout = tokenHubConfig.TimeoutSeconds > 0 ? TimeSpan.FromSeconds(tokenHubConfig.TimeoutSeconds) : null, InitialRetryDelay = tokenHubConfig.InitialRetryDelaySeconds > 0 ? TimeSpan.FromSeconds(tokenHubConfig.InitialRetryDelaySeconds) : TimeSpan.FromSeconds(1), MaxRetryDelay = tokenHubConfig.MaxRetryDelaySeconds > 0 ? TimeSpan.FromSeconds(tokenHubConfig.MaxRetryDelaySeconds) : TimeSpan.FromSeconds(30) }; return services.AddTokenHubService( apiKey: tokenHubConfig.ApiKey, baseUrl: tokenHubConfig.BaseUrl, options: options ); } } /// /// TokenHub 配置类,用于绑定 appsettings.json 中的配置 /// /// /// 配置示例: /// /// { /// "TokenHub": { /// "ApiKey": "sk-xxxxxxxx", /// "BaseUrl": "https://tokenhub.tencentmaas.com/v1/chat/completions", /// "DefaultModel": "hunyuan-2.0-instruct-20251111", /// "MaxRetries": 3, /// "TimeoutSeconds": 120, /// "InitialRetryDelaySeconds": 1, /// "MaxRetryDelaySeconds": 30 /// } /// } /// /// public class TokenHubConfiguration { // ============ 必填参数 ============ /// /// 获取或设置 TokenHub API 密钥(必填) /// /// 从 TokenHub 控制台获取的 API 密钥 /// /// 获取地址:https://console.cloud.tencent.com/tokenhub/ /// 格式示例:sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx /// public string ApiKey { get; set; } = string.Empty; // ============ 基础配置 ============ /// /// 获取或设置 TokenHub API 端点地址(可选) /// /// API 服务的完整 URL /// /// 默认值:https://tokenhub.tencentmaas.com/v1/chat/completions /// public string? BaseUrl { get; set; } /// /// 获取或设置默认使用的 AI 模型(可选) /// /// 模型名称,默认 "hunyuan-2.0-instruct-20251111" /// /// 混元模型: /// - hunyuan-2.0-instruct-20251111: 混元 2.0 Instruct(推荐) /// - hunyuan-turbo: 混元 Turbo(速度快) /// - hunyuan-pro: 混元 Pro(复杂任务) /// - hunyuan-lite: 混元 Lite(轻量级) /// /// 第三方模型: /// - glm-5.1: 智谱 GLM-5.1 /// - deepseek-v3: DeepSeek V3 /// - deepseek-r1: DeepSeek R1 /// - minimax-m2.7: MiniMax M2.7 /// - qwen-2.5-72b: 通义千问 2.5 72B /// public string DefaultModel { get; set; } = "hunyuan-2.0-instruct-20251111"; // ============ 重试策略配置 ============ /// /// 获取或设置请求失败时的最大重试次数(可选) /// /// 重试次数,默认 3,范围 0-10 /// /// 0:不重试 /// 建议生产环境设置为 3-5 次 /// public int MaxRetries { get; set; } = 3; /// /// 获取或设置请求超时时间(秒)(可选) /// /// 超时秒数,默认 120,范围 1-600 /// /// 实时对话:30-60 秒 /// 批量处理:120-300 秒 /// 流式输出:60-120 秒 /// public int TimeoutSeconds { get; set; } = 120; /// /// 获取或设置首次重试的延迟时间(秒)(可选) /// /// 延迟秒数,默认 1,范围 0.1-60 /// /// 使用指数退避策略:1s, 2s, 4s, 8s, 16s, 30s /// public int InitialRetryDelaySeconds { get; set; } = 1; /// /// 获取或设置最大重试延迟时间(秒)(可选) /// /// 最大延迟秒数,默认 30,范围 1-300 public int MaxRetryDelaySeconds { get; set; } = 30; // ============ 日志配置 ============ /// /// 获取或设置是否启用日志记录(可选) /// /// 默认 true public bool EnableLogging { get; set; } = true; /// /// 获取或设置日志级别(可选) /// /// 默认 "Information" /// 可选值:Debug, Information, Warning, Error public string LogLevel { get; set; } = "Information"; /// /// 获取或设置是否启用性能指标收集(可选) /// /// 默认 false public bool EnableMetrics { get; set; } = false; /// /// 获取或设置是否记录请求内容(可选) /// /// 默认 false,仅开发环境开启 public bool LogRequestContent { get; set; } = false; /// /// 获取或设置是否记录响应内容(可选) /// /// 默认 false,仅开发环境开启 public bool LogResponseContent { get; set; } = false; // ============ 默认请求参数 ============ /// /// 获取或设置默认最大输出 Token 数(可选) /// /// Token 数量,默认 1024,范围 1-8192 public int DefaultMaxTokens { get; set; } = 1024; /// /// 获取或设置默认温度参数(可选) /// /// 温度值,默认 0.7,范围 0.0-2.0 /// /// 0.0-0.3:确定性输出 /// 0.4-0.6:平衡模式 /// 0.7-1.0:创造性模式 /// 1.0-2.0:高随机性 /// public double DefaultTemperature { get; set; } = 0.7; /// /// 获取或设置默认 Top-P 参数(可选) /// /// Top-P 值,范围 0.0-1.0,null 表示不设置 public double? DefaultTopP { get; set; } /// /// 获取或设置默认频率惩罚(可选) /// /// 惩罚值,范围 -2.0 到 2.0,null 表示不设置 public double? DefaultFrequencyPenalty { get; set; } /// /// 获取或设置默认存在惩罚(可选) /// /// 惩罚值,范围 -2.0 到 2.0,null 表示不设置 public double? DefaultPresencePenalty { get; set; } // ============ 安全和权限配置 ============ /// /// 获取或设置是否启用内容安全过滤(可选) /// /// 默认 true,建议保持开启 public bool EnableContentFilter { get; set; } = true; /// /// 获取或设置允许使用的模型列表(白名单)(可选) /// /// 模型名称列表,null 表示不限制 public List? AllowedModels { get; set; } /// /// 获取或设置禁止使用的模型列表(黑名单)(可选) /// /// 模型名称列表,null 表示不限制 public List? BlockedModels { get; set; } // ============ 缓存配置 ============ /// /// 获取或设置是否启用响应缓存(可选) /// /// 默认 false public bool EnableCache { get; set; } = false; /// /// 获取或设置缓存有效期(秒)(可选) /// /// 默认 300,范围 60-86400 public int CacheDurationSeconds { get; set; } = 300; /// /// 获取或设置最大缓存条目数(可选) /// /// 默认 100,范围 1-10000 public int MaxCacheSize { get; set; } = 100; // ============ 高级配置 ============ /// /// 获取或设置自定义 HTTP 请求头(可选) /// public Dictionary? CustomHeaders { get; set; } /// /// 获取或设置自定义请求参数(可选) /// public Dictionary? CustomParameters { get; set; } /// /// 获取或设置代理服务器地址(可选) /// /// "http://proxy.company.com:8080" public string? ProxyUrl { get; set; } /// /// 获取或设置是否跳过 SSL 证书验证(可选) /// /// 默认 false,仅测试环境开启 public bool BypassSSLValidation { get; set; } = false; } /// /// TokenHub 服务选项(代码配置用) /// public class TokenHubServiceOptions { /// /// 最大重试次数 /// public int MaxRetries { get; set; } = 3; /// /// 请求超时时间 /// public TimeSpan? Timeout { get; set; } /// /// 首次重试延迟 /// public TimeSpan? InitialRetryDelay { get; set; } /// /// 最大重试延迟 /// public TimeSpan? MaxRetryDelay { get; set; } }