饥荒联机服务器搭建教程 支持主世界+洞穴
2026/6/25 23:41:46
基于 Microsoft Semantic Kernel 的统一 AI 服务提供者封装库,支持多种 AI 服务商(OpenAI、Ollama、DashScope),提供流式聊天、MCP 插件、工具调用等功能。
dotnet add package SemanticKerne.AiProvider.Unified{ "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 } ] } }using 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.Configure<SemanticKernelOptions>( builder.Configuration.GetSection("SemanticKernel")); builder.Services.AddSingleton<ISemanticKernelService, SemanticKernelService>(); builder.Services.AddSingleton<ISessionManager, SessionManager>(); builder.Services.AddSingleton<BailianErrorHandler>(); // 注册 MCP 服务 builder.Services.AddHttpClient(); builder.Services.AddSingleton<IMcpClientService, McpClientService>(sp => { var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>(); var logger = sp.GetRequiredService<ILogger<McpClientService>>(); return new McpClientService(httpClientFactory, logger, builder.Configuration); }); var app = builder.Build(); app.Run();/// <summary> /// 聊天控制器 /// </summary> [ApiController] [Route("api/[controller]")] public class ChatController : ControllerBase { private readonly ISessionManager _sessionManager; private readonly ISemanticKernelService _kernelService; private readonly ILogger<ChatController> _logger; private readonly BailianErrorHandler _errorHandler; public ChatController( ISessionManager sessionManager, ISemanticKernelService kernelService, ILogger<ChatController> 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 = "已停止当前请求" });