UNIVPLMDataIntegration/Admin.NET/Admin.NET.Core/Ai/Services/Chat/LLMChatService.cs

108 lines
4.3 KiB
C#
Raw Normal View History

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;
/// <summary>
/// LLM聊天补全服务
/// </summary>
[ApiDescriptionSettings(Description = "LLM聊天补全服务", Name = "LLMChat", Order = 100)]
public class LLMChatService : IDynamicApiController, ITransient
{
private readonly ILogger<LLMChatService> _logger;
private readonly UserManager _userManager;
private readonly SqlSugarRepository<SysUser> _userService;
private readonly SqlSugarRepository<LLMChatHistory> _chatHistoryService;
private readonly LLMOptionService _llmOptionService;
private readonly LLMChatCoreService _chatCoreService; //聊天核心服务
private readonly SqlSugarRepository<LLMChatSummaryHistory> _chatSummaryHistoryRep;
public LLMChatService(ILogger<LLMChatService> logger,
UserManager userManager,
SqlSugarRepository<SysUser> userService,
SqlSugarRepository<LLMChatHistory> chatHistoryService,
LLMOptionService llmOptionService,
SqlSugarRepository<LLMChatSummaryHistory> chatSummaryHistoryService,
LLMChatCoreService chatCoreService)
{
_logger = logger;
_userManager = userManager;
_userService = userService;
_chatHistoryService = chatHistoryService;
_llmOptionService = llmOptionService;
_chatCoreService = chatCoreService;
_chatSummaryHistoryRep = chatSummaryHistoryService;
}
/// <summary>
/// 聊天补全
/// </summary>
/// <param name="message"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Description = "聊天补全", Name = "Chat")]
public async Task<ChatOutput> ChatAsync(ChatInput message, CancellationToken cancellationToken) => await _chatCoreService.ChatAsync(message, cancellationToken);
/// <summary>
/// 删除所属摘要的所有聊天记录
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Description = "删除所属摘要的所有聊天记录", Name = "DeleteSummaryAll")]
public async Task<bool> DeleteSummaryAllAsync(ChatInput message) => await _chatCoreService.DeleteSummaryAllAsync(message);
/// <summary>
/// 重命名所属摘要的标签
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Description = "重命名所属摘要的标签", Name = "RenameSummaryLable")]
public async Task<bool> RenameSummaryLable(ChatInput message) => await _chatCoreService.RenameSummaryLable(message);
/// <summary>
/// 获取聊天列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[ApiDescriptionSettings(Description = "获取聊天列表", Name = "ChatList"), HttpPost]
public async Task<SqlSugarPagedList<ChatListOutput>> 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<SqlSugarPagedList<ChatListOutput>>();
return returnList;
}
/// <summary>
/// 获取模型列表
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
[HttpGet]
[ApiDescriptionSettings(Description = "获取模型列表", Name = "ModelList")]
public async Task<ModelListOutput> GetModelListAsync() => await _llmOptionService.GetModelListAsync();
/// <summary>
/// 切换模型
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
[ApiDescriptionSettings(Description = "切换模型", Name = "ChangeModel"), HttpPost]
public async Task<bool> ChangeModelAsync(ModelListChangeInput input) => await _llmOptionService.ChangeModelAsync(input);
}