// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。 // // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。 // // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任! namespace Admin.NET.Core.Ai.Service; /// /// LLM聊天补全服务 /// [ApiDescriptionSettings(Description = "LLM聊天补全服务", Name = "LLMChat", Order = 100)] public class LLMChatService : IDynamicApiController, ITransient { private readonly ILogger _logger; private readonly UserManager _userManager; private readonly SqlSugarRepository _userService; private readonly SqlSugarRepository _chatHistoryService; private readonly LLMOptionService _llmOptionService; private readonly LLMChatCoreService _chatCoreService; //聊天核心服务 private readonly SqlSugarRepository _chatSummaryHistoryRep; public LLMChatService(ILogger logger, UserManager userManager, SqlSugarRepository userService, SqlSugarRepository chatHistoryService, LLMOptionService llmOptionService, SqlSugarRepository chatSummaryHistoryService, LLMChatCoreService chatCoreService) { _logger = logger; _userManager = userManager; _userService = userService; _chatHistoryService = chatHistoryService; _llmOptionService = llmOptionService; _chatCoreService = chatCoreService; _chatSummaryHistoryRep = chatSummaryHistoryService; } /// /// 聊天补全 /// /// /// /// [HttpPost] [ApiDescriptionSettings(Description = "聊天补全", Name = "Chat")] public async Task ChatAsync(ChatInput message, CancellationToken cancellationToken) => await _chatCoreService.ChatAsync(message, cancellationToken); /// /// 删除所属摘要的所有聊天记录 /// /// /// [HttpPost] [ApiDescriptionSettings(Description = "删除所属摘要的所有聊天记录", Name = "DeleteSummaryAll")] public async Task DeleteSummaryAllAsync(ChatInput message) => await _chatCoreService.DeleteSummaryAllAsync(message); /// /// 重命名所属摘要的标签 /// /// /// [HttpPost] [ApiDescriptionSettings(Description = "重命名所属摘要的标签", Name = "RenameSummaryLable")] public async Task RenameSummaryLable(ChatInput message) => await _chatCoreService.RenameSummaryLable(message); /// /// 获取聊天列表 /// /// /// [ApiDescriptionSettings(Description = "获取聊天列表", Name = "ChatList"), HttpPost] public async Task> Page(ChatListInput input) { var userId = _userManager.UserId; if (userId == default) { throw new Exception("用户不存在"); } var list = await _chatSummaryHistoryRep.AsQueryable().Includes(u => u.Histories) .Where(u => u.UserId == userId) .OrderBy(u => u.UtcCreateTime, OrderByType.Desc) .ToPagedListAsync(input.Page, input.PageSize); var returnList = list.Adapt>(); return returnList; } /// /// 获取模型列表 /// /// /// [HttpGet] [ApiDescriptionSettings(Description = "获取模型列表", Name = "ModelList")] public async Task GetModelListAsync() => await _llmOptionService.GetModelListAsync(); /// /// 切换模型 /// /// /// /// [ApiDescriptionSettings(Description = "切换模型", Name = "ChangeModel"), HttpPost] public async Task ChangeModelAsync(ModelListChangeInput input) => await _llmOptionService.ChangeModelAsync(input); }