TokenHubServiceExtensions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. using Microsoft.Extensions.Configuration;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Logging;
  4. namespace OASystem.API.OAMethodLib.TokenHubAI;
  5. public static class TokenHubServiceExtensions
  6. {
  7. /// <summary>
  8. /// 注册 TokenHub 服务到 DI 容器(使用 API Key + 配置委托)
  9. /// </summary>
  10. /// <param name="services">服务集合</param>
  11. /// <param name="apiKey">TokenHub API 密钥</param>
  12. /// <param name="baseUrl">API 端点地址(可选)</param>
  13. /// <param name="configure">配置委托</param>
  14. /// <returns>服务集合</returns>
  15. public static IServiceCollection AddTokenHubService(
  16. this IServiceCollection services,
  17. string apiKey,
  18. string? baseUrl = null,
  19. Action<TokenHubServiceOptions>? configure = null)
  20. {
  21. var options = new TokenHubServiceOptions();
  22. configure?.Invoke(options);
  23. services.AddSingleton<ITokenHubService>(provider =>
  24. {
  25. var logger = provider.GetService<ILogger<TokenHubService>>();
  26. var service = new TokenHubService(apiKey, baseUrl, logger);
  27. if (options.Timeout.HasValue)
  28. service.SetTimeout(options.Timeout.Value);
  29. if (options.MaxRetries > 0)
  30. {
  31. service.SetRetryPolicy(
  32. options.MaxRetries,
  33. options.InitialRetryDelay ?? TimeSpan.FromSeconds(1),
  34. options.MaxRetryDelay ?? TimeSpan.FromSeconds(30)
  35. );
  36. }
  37. return service;
  38. });
  39. return services;
  40. }
  41. /// <summary>
  42. /// 注册 TokenHub 服务到 DI 容器(使用配置对象)
  43. /// </summary>
  44. /// <param name="services">服务集合</param>
  45. /// <param name="apiKey">TokenHub API 密钥</param>
  46. /// <param name="baseUrl">API 端点地址(可选)</param>
  47. /// <param name="options">配置对象</param>
  48. /// <returns>服务集合</returns>
  49. public static IServiceCollection AddTokenHubService(
  50. this IServiceCollection services,
  51. string apiKey,
  52. string? baseUrl,
  53. TokenHubServiceOptions options)
  54. {
  55. services.AddSingleton<ITokenHubService>(provider =>
  56. {
  57. var logger = provider.GetService<ILogger<TokenHubService>>();
  58. var service = new TokenHubService(apiKey, baseUrl, logger);
  59. if (options.Timeout.HasValue)
  60. service.SetTimeout(options.Timeout.Value);
  61. if (options.MaxRetries > 0)
  62. {
  63. service.SetRetryPolicy(
  64. options.MaxRetries,
  65. options.InitialRetryDelay ?? TimeSpan.FromSeconds(1),
  66. options.MaxRetryDelay ?? TimeSpan.FromSeconds(30)
  67. );
  68. }
  69. return service;
  70. });
  71. return services;
  72. }
  73. /// <summary>
  74. /// 从 IConfiguration 注册 TokenHub 服务
  75. /// </summary>
  76. /// <param name="services">服务集合</param>
  77. /// <param name="configuration">应用程序配置</param>
  78. /// <param name="configSection">配置节名称,默认 "TokenHub"</param>
  79. /// <returns>服务集合</returns>
  80. /// <exception cref="InvalidOperationException">当未找到 ApiKey 时抛出</exception>
  81. public static IServiceCollection AddTokenHubService(
  82. this IServiceCollection services,
  83. IConfiguration configuration,
  84. string configSection = "TokenHub")
  85. {
  86. var config = configuration.GetSection(configSection);
  87. var tokenHubConfig = new TokenHubConfiguration();
  88. config.Bind(tokenHubConfig);
  89. if (string.IsNullOrWhiteSpace(tokenHubConfig.ApiKey))
  90. throw new InvalidOperationException(
  91. $"未找到 TokenHub API Key 配置: {configSection}:ApiKey");
  92. // 构建选项
  93. var options = new TokenHubServiceOptions
  94. {
  95. MaxRetries = tokenHubConfig.MaxRetries,
  96. Timeout = tokenHubConfig.TimeoutSeconds > 0
  97. ? TimeSpan.FromSeconds(tokenHubConfig.TimeoutSeconds)
  98. : null,
  99. InitialRetryDelay = tokenHubConfig.InitialRetryDelaySeconds > 0
  100. ? TimeSpan.FromSeconds(tokenHubConfig.InitialRetryDelaySeconds)
  101. : TimeSpan.FromSeconds(1),
  102. MaxRetryDelay = tokenHubConfig.MaxRetryDelaySeconds > 0
  103. ? TimeSpan.FromSeconds(tokenHubConfig.MaxRetryDelaySeconds)
  104. : TimeSpan.FromSeconds(30)
  105. };
  106. return services.AddTokenHubService(
  107. apiKey: tokenHubConfig.ApiKey,
  108. baseUrl: tokenHubConfig.BaseUrl,
  109. options: options
  110. );
  111. }
  112. }
  113. /// <summary>
  114. /// TokenHub 配置类,用于绑定 appsettings.json 中的配置
  115. /// </summary>
  116. /// <remarks>
  117. /// 配置示例:
  118. /// <code>
  119. /// {
  120. /// "TokenHub": {
  121. /// "ApiKey": "sk-xxxxxxxx",
  122. /// "BaseUrl": "https://tokenhub.tencentmaas.com/v1/chat/completions",
  123. /// "DefaultModel": "hunyuan-2.0-instruct-20251111",
  124. /// "MaxRetries": 3,
  125. /// "TimeoutSeconds": 120,
  126. /// "InitialRetryDelaySeconds": 1,
  127. /// "MaxRetryDelaySeconds": 30
  128. /// }
  129. /// }
  130. /// </code>
  131. /// </remarks>
  132. public class TokenHubConfiguration
  133. {
  134. // ============ 必填参数 ============
  135. /// <summary>
  136. /// 获取或设置 TokenHub API 密钥(必填)
  137. /// </summary>
  138. /// <value>从 TokenHub 控制台获取的 API 密钥</value>
  139. /// <remarks>
  140. /// 获取地址:https://console.cloud.tencent.com/tokenhub/
  141. /// 格式示例:sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  142. /// </remarks>
  143. public string ApiKey { get; set; } = string.Empty;
  144. // ============ 基础配置 ============
  145. /// <summary>
  146. /// 获取或设置 TokenHub API 端点地址(可选)
  147. /// </summary>
  148. /// <value>API 服务的完整 URL</value>
  149. /// <remarks>
  150. /// 默认值:https://tokenhub.tencentmaas.com/v1/chat/completions
  151. /// </remarks>
  152. public string? BaseUrl { get; set; }
  153. /// <summary>
  154. /// 获取或设置默认使用的 AI 模型(可选)
  155. /// </summary>
  156. /// <value>模型名称,默认 "hunyuan-2.0-instruct-20251111"</value>
  157. /// <remarks>
  158. /// 混元模型:
  159. /// - hunyuan-2.0-instruct-20251111: 混元 2.0 Instruct(推荐)
  160. /// - hunyuan-turbo: 混元 Turbo(速度快)
  161. /// - hunyuan-pro: 混元 Pro(复杂任务)
  162. /// - hunyuan-lite: 混元 Lite(轻量级)
  163. ///
  164. /// 第三方模型:
  165. /// - glm-5.1: 智谱 GLM-5.1
  166. /// - deepseek-v3: DeepSeek V3
  167. /// - deepseek-r1: DeepSeek R1
  168. /// - minimax-m2.7: MiniMax M2.7
  169. /// - qwen-2.5-72b: 通义千问 2.5 72B
  170. /// </remarks>
  171. public string DefaultModel { get; set; } = "hunyuan-2.0-instruct-20251111";
  172. // ============ 重试策略配置 ============
  173. /// <summary>
  174. /// 获取或设置请求失败时的最大重试次数(可选)
  175. /// </summary>
  176. /// <value>重试次数,默认 3,范围 0-10</value>
  177. /// <remarks>
  178. /// 0:不重试
  179. /// 建议生产环境设置为 3-5 次
  180. /// </remarks>
  181. public int MaxRetries { get; set; } = 3;
  182. /// <summary>
  183. /// 获取或设置请求超时时间(秒)(可选)
  184. /// </summary>
  185. /// <value>超时秒数,默认 120,范围 1-600</value>
  186. /// <remarks>
  187. /// 实时对话:30-60 秒
  188. /// 批量处理:120-300 秒
  189. /// 流式输出:60-120 秒
  190. /// </remarks>
  191. public int TimeoutSeconds { get; set; } = 120;
  192. /// <summary>
  193. /// 获取或设置首次重试的延迟时间(秒)(可选)
  194. /// </summary>
  195. /// <value>延迟秒数,默认 1,范围 0.1-60</value>
  196. /// <remarks>
  197. /// 使用指数退避策略:1s, 2s, 4s, 8s, 16s, 30s
  198. /// </remarks>
  199. public int InitialRetryDelaySeconds { get; set; } = 1;
  200. /// <summary>
  201. /// 获取或设置最大重试延迟时间(秒)(可选)
  202. /// </summary>
  203. /// <value>最大延迟秒数,默认 30,范围 1-300</value>
  204. public int MaxRetryDelaySeconds { get; set; } = 30;
  205. // ============ 日志配置 ============
  206. /// <summary>
  207. /// 获取或设置是否启用日志记录(可选)
  208. /// </summary>
  209. /// <value>默认 true</value>
  210. public bool EnableLogging { get; set; } = true;
  211. /// <summary>
  212. /// 获取或设置日志级别(可选)
  213. /// </summary>
  214. /// <value>默认 "Information"</value>
  215. /// <remarks>可选值:Debug, Information, Warning, Error</remarks>
  216. public string LogLevel { get; set; } = "Information";
  217. /// <summary>
  218. /// 获取或设置是否启用性能指标收集(可选)
  219. /// </summary>
  220. /// <value>默认 false</value>
  221. public bool EnableMetrics { get; set; } = false;
  222. /// <summary>
  223. /// 获取或设置是否记录请求内容(可选)
  224. /// </summary>
  225. /// <value>默认 false,仅开发环境开启</value>
  226. public bool LogRequestContent { get; set; } = false;
  227. /// <summary>
  228. /// 获取或设置是否记录响应内容(可选)
  229. /// </summary>
  230. /// <value>默认 false,仅开发环境开启</value>
  231. public bool LogResponseContent { get; set; } = false;
  232. // ============ 默认请求参数 ============
  233. /// <summary>
  234. /// 获取或设置默认最大输出 Token 数(可选)
  235. /// </summary>
  236. /// <value>Token 数量,默认 1024,范围 1-8192</value>
  237. public int DefaultMaxTokens { get; set; } = 1024;
  238. /// <summary>
  239. /// 获取或设置默认温度参数(可选)
  240. /// </summary>
  241. /// <value>温度值,默认 0.7,范围 0.0-2.0</value>
  242. /// <remarks>
  243. /// 0.0-0.3:确定性输出
  244. /// 0.4-0.6:平衡模式
  245. /// 0.7-1.0:创造性模式
  246. /// 1.0-2.0:高随机性
  247. /// </remarks>
  248. public double DefaultTemperature { get; set; } = 0.7;
  249. /// <summary>
  250. /// 获取或设置默认 Top-P 参数(可选)
  251. /// </summary>
  252. /// <value>Top-P 值,范围 0.0-1.0,null 表示不设置</value>
  253. public double? DefaultTopP { get; set; }
  254. /// <summary>
  255. /// 获取或设置默认频率惩罚(可选)
  256. /// </summary>
  257. /// <value>惩罚值,范围 -2.0 到 2.0,null 表示不设置</value>
  258. public double? DefaultFrequencyPenalty { get; set; }
  259. /// <summary>
  260. /// 获取或设置默认存在惩罚(可选)
  261. /// </summary>
  262. /// <value>惩罚值,范围 -2.0 到 2.0,null 表示不设置</value>
  263. public double? DefaultPresencePenalty { get; set; }
  264. // ============ 安全和权限配置 ============
  265. /// <summary>
  266. /// 获取或设置是否启用内容安全过滤(可选)
  267. /// </summary>
  268. /// <value>默认 true,建议保持开启</value>
  269. public bool EnableContentFilter { get; set; } = true;
  270. /// <summary>
  271. /// 获取或设置允许使用的模型列表(白名单)(可选)
  272. /// </summary>
  273. /// <value>模型名称列表,null 表示不限制</value>
  274. public List<string>? AllowedModels { get; set; }
  275. /// <summary>
  276. /// 获取或设置禁止使用的模型列表(黑名单)(可选)
  277. /// </summary>
  278. /// <value>模型名称列表,null 表示不限制</value>
  279. public List<string>? BlockedModels { get; set; }
  280. // ============ 缓存配置 ============
  281. /// <summary>
  282. /// 获取或设置是否启用响应缓存(可选)
  283. /// </summary>
  284. /// <value>默认 false</value>
  285. public bool EnableCache { get; set; } = false;
  286. /// <summary>
  287. /// 获取或设置缓存有效期(秒)(可选)
  288. /// </summary>
  289. /// <value>默认 300,范围 60-86400</value>
  290. public int CacheDurationSeconds { get; set; } = 300;
  291. /// <summary>
  292. /// 获取或设置最大缓存条目数(可选)
  293. /// </summary>
  294. /// <value>默认 100,范围 1-10000</value>
  295. public int MaxCacheSize { get; set; } = 100;
  296. // ============ 高级配置 ============
  297. /// <summary>
  298. /// 获取或设置自定义 HTTP 请求头(可选)
  299. /// </summary>
  300. public Dictionary<string, string>? CustomHeaders { get; set; }
  301. /// <summary>
  302. /// 获取或设置自定义请求参数(可选)
  303. /// </summary>
  304. public Dictionary<string, object>? CustomParameters { get; set; }
  305. /// <summary>
  306. /// 获取或设置代理服务器地址(可选)
  307. /// </summary>
  308. /// <example>"http://proxy.company.com:8080"</example>
  309. public string? ProxyUrl { get; set; }
  310. /// <summary>
  311. /// 获取或设置是否跳过 SSL 证书验证(可选)
  312. /// </summary>
  313. /// <value>默认 false,仅测试环境开启</value>
  314. public bool BypassSSLValidation { get; set; } = false;
  315. }
  316. /// <summary>
  317. /// TokenHub 服务选项(代码配置用)
  318. /// </summary>
  319. public class TokenHubServiceOptions
  320. {
  321. /// <summary>
  322. /// 最大重试次数
  323. /// </summary>
  324. public int MaxRetries { get; set; } = 3;
  325. /// <summary>
  326. /// 请求超时时间
  327. /// </summary>
  328. public TimeSpan? Timeout { get; set; }
  329. /// <summary>
  330. /// 首次重试延迟
  331. /// </summary>
  332. public TimeSpan? InitialRetryDelay { get; set; }
  333. /// <summary>
  334. /// 最大重试延迟
  335. /// </summary>
  336. public TimeSpan? MaxRetryDelay { get; set; }
  337. }