using Admin.NET.Core.Ai.Entitys;
using Admin.NET.Core.Ai.Option;
using Admin.NET.Core.Ai.Services.Chat.Dto;
using Admin.NET.Core.Ai.Services.DataBase;
using Admin.NET.Core.Ai.Services.SSE;
namespace Admin.NET.Core.Ai.Services.Chat;
///
/// 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);
}