
SemanticKerne.AiProvider.Unified基于 Microsoft Semantic Kernel 的统一 AI 服务提供者封装库支持多种 AI 服务商OpenAI、Ollama、DashScope提供流式聊天、MCP 插件、工具调用等功能。特性多服务商支持统一接口支持 OpenAI、Ollama、DashScope阿里云思考过程输出支持模型的思考过程流式输出reasoning_content工具调用支持 MCPModel Context Protocol插件和自定义工具⚙️灵活配置所有参数可通过配置文件或代码自定义开箱即用完整的依赖注入支持快速集成到 ASP.NET Core 项目真正流式基于 SSE 的实时流式输出低延迟安装dotnet add package SemanticKerne.AiProvider.Unified快速开始1. 配置文件appsettings.json{ SemanticKernel: { AiServiceType: dashscope, ModelId: qwen3.6-plus-2026-04-02, Endpoint: https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions, ApiKey: sk-your-api-key-here, HttpClientTimeout: 00:05:00, ExtensionData: { enable_thinking: true, preserve_thinking: true, temperature: 0.7, max_tokens: 4096 } }, Mcp: { Enabled: true, Servers: [ { Name: sql-mcp-http, Enabled: true, Description: Data API Builder MCP 服务, Transport: http, Endpoint: http://localhost:5000/mcp, TimeoutSeconds: 30 } ] } }2. 服务注册Program.csusing Microsoft.Extensions.Options; using SemanticKerne.AiProvider.Unified.Models; using SemanticKerne.AiProvider.Unified.Services; using SemanticKerne.AiProvider.Unified.Services.Mcp; var builder WebApplication.CreateBuilder(args); // 注册 SemanticKernelOptions 配置 builder.Services.ConfigureSemanticKernelOptions( builder.Configuration.GetSection(SemanticKernel)); builder.Services.AddSingletonISemanticKernelService, SemanticKernelService(); builder.Services.AddSingletonISessionManager, SessionManager(); builder.Services.AddSingletonBailianErrorHandler(); // 注册 MCP 服务 builder.Services.AddHttpClient(); builder.Services.AddSingletonIMcpClientService, McpClientService(sp { var httpClientFactory sp.GetRequiredServiceIHttpClientFactory(); var logger sp.GetRequiredServiceILoggerMcpClientService(); return new McpClientService(httpClientFactory, logger, builder.Configuration); }); var app builder.Build(); app.Run();3. 使用示例/// summary /// 聊天控制器 /// /summary [ApiController] [Route(api/[controller])] public class ChatController : ControllerBase { private readonly ISessionManager _sessionManager; private readonly ISemanticKernelService _kernelService; private readonly ILoggerChatController _logger; private readonly BailianErrorHandler _errorHandler; public ChatController( ISessionManager sessionManager, ISemanticKernelService kernelService, ILoggerChatController logger, BailianErrorHandler errorHandler) { _sessionManager sessionManager; _kernelService kernelService; _logger logger; _errorHandler errorHandler; } private string UserId User.FindFirst(ClaimTypes.Name)?.Value ?? test-user; /// summary /// 获取当前用户的所有会话 /// /summary [HttpGet(sessions)] public IActionResult GetSessions() { _logger.LogInformation(GetSessions called, UserId: {UserId}, AllClaims: {Claims}, UserId, string.Join(, , User.Claims.Select(c ${c.Type}{c.Value}))); var sessions _sessionManager.GetUserSessions(UserId); _logger.LogInformation(GetSessions result, count: {Count}, sessions.Count()); return Ok(sessions); } /// summary /// 创建新的聊天会话 /// /summary [HttpPost(sessions)] public IActionResult CreateSession() { _logger.LogInformation(CreateSession called, UserId: {UserId}, UserId); var session _sessionManager.CreateSession(UserId); _logger.LogInformation(CreateSession created, SessionId: {SessionId}, UserId: {UserId}, session.SessionId, UserId); return Ok(new CreateSessionResponse { SessionId session.SessionId, CreatedAt session.CreatedAt }); } /// summary /// 删除指定的聊天会话 /// /summary [HttpDelete(sessions/{sessionId})] public IActionResult DeleteSession(string sessionId) { _logger.LogInformation(DeleteSession called, UserId: {UserId}, SessionId: {SessionId}, UserId, sessionId); var result _sessionManager.DeleteSession(UserId, sessionId); if (!result) { _logger.LogWarning(DeleteSession failed, session not found, UserId: {UserId}, SessionId: {SessionId}, UserId, sessionId); return NotFound(new { message 会话不存在 }); } return NoContent(); } /// summary /// 停止会话当前正在进行的请求不删除会话 /// /summary [HttpPost(sessions/{sessionId}/stop)] public IActionResult StopSession(string sessionId) { var session _sessionManager.GetSession(UserId, sessionId); _logger.LogInformation(StopSession called, UserId: {UserId}, SessionId: {SessionId}, SessionExists: {Exists}, IsProcessing: {IsProcessing}, UserId, sessionId, session ! null, session?.IsProcessing); var result _sessionManager.StopSession(UserId, sessionId); _logger.LogInformation(StopSession result: {Result}, result); if (!result) { return NotFound(new { message 会话不存在或没有正在进行的请求 }); } return Ok(new { message 已停止当前请求 });