Files
Yi.Admin/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/Chat/AiChatService.cs

176 lines
6.1 KiB
C#
Raw Normal View History

2025-06-25 00:30:01 +08:00
using System.Collections.Concurrent;
using System.Text;
2025-06-21 21:40:51 +08:00
using Microsoft.AspNetCore.Authorization;
2025-06-21 01:08:14 +08:00
using Microsoft.AspNetCore.Http;
2025-06-21 21:40:51 +08:00
using Microsoft.AspNetCore.Mvc;
2025-06-21 01:08:14 +08:00
using Microsoft.Extensions.DependencyInjection;
2025-07-02 00:28:44 +08:00
using Microsoft.Extensions.Logging;
2025-06-21 01:08:14 +08:00
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using OpenAI.Chat;
2025-06-19 21:24:13 +08:00
using Volo.Abp.Application.Services;
2025-06-21 13:02:38 +08:00
using Volo.Abp.Users;
2025-06-19 21:24:13 +08:00
using Yi.Framework.AiHub.Application.Contracts.Dtos;
2025-06-25 17:12:09 +08:00
using Yi.Framework.AiHub.Domain.Entities;
2025-06-27 22:13:26 +08:00
using Yi.Framework.AiHub.Domain.Entities.Model;
2025-07-05 15:11:56 +08:00
using Yi.Framework.AiHub.Domain.Extensions;
2025-06-21 01:08:14 +08:00
using Yi.Framework.AiHub.Domain.Managers;
using Yi.Framework.AiHub.Domain.Shared.Consts;
2025-06-27 22:13:26 +08:00
using Yi.Framework.AiHub.Domain.Shared.Dtos;
2025-08-11 15:31:11 +08:00
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
2025-08-03 23:23:32 +08:00
using Yi.Framework.AiHub.Domain.Shared.Enums;
2025-06-21 21:40:51 +08:00
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Shared.Dtos;
2025-06-25 17:12:09 +08:00
using Yi.Framework.SqlSugarCore.Abstractions;
2025-06-19 21:24:13 +08:00
namespace Yi.Framework.AiHub.Application.Services;
2025-06-21 13:02:38 +08:00
/// <summary>
/// ai服务
/// </summary>
public class AiChatService : ApplicationService
2025-06-19 21:24:13 +08:00
{
2025-06-21 01:08:14 +08:00
private readonly IHttpContextAccessor _httpContextAccessor;
2025-06-25 17:12:09 +08:00
private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository;
private readonly AiBlacklistManager _aiBlacklistManager;
2025-07-02 00:28:44 +08:00
private readonly ILogger<AiChatService> _logger;
2025-07-05 15:11:56 +08:00
private readonly AiGateWayManager _aiGateWayManager;
private readonly PremiumPackageManager _premiumPackageManager;
2025-06-21 01:08:14 +08:00
2025-06-25 17:12:09 +08:00
public AiChatService(IHttpContextAccessor httpContextAccessor,
2025-07-05 15:11:56 +08:00
AiBlacklistManager aiBlacklistManager,
ISqlSugarRepository<AiModelEntity> aiModelRepository,
ILogger<AiChatService> logger, AiGateWayManager aiGateWayManager, PremiumPackageManager premiumPackageManager)
2025-06-21 01:08:14 +08:00
{
2025-07-05 15:11:56 +08:00
_httpContextAccessor = httpContextAccessor;
2025-06-25 17:12:09 +08:00
_aiBlacklistManager = aiBlacklistManager;
2025-06-25 22:45:57 +08:00
_aiModelRepository = aiModelRepository;
2025-07-02 00:28:44 +08:00
_logger = logger;
2025-07-05 15:11:56 +08:00
_aiGateWayManager = aiGateWayManager;
_premiumPackageManager = premiumPackageManager;
2025-06-21 01:08:14 +08:00
}
2025-06-19 21:24:13 +08:00
2025-06-21 21:40:51 +08:00
/// <summary>
/// 查询已登录的账户信息
/// </summary>
/// <returns></returns>
[Route("ai-chat/account")]
[Authorize]
public async Task<UserRoleMenuDto> GetAsync()
{
var accountService = LazyServiceProvider.GetRequiredService<IAccountService>();
var output = await accountService.GetAsync();
return output;
}
2025-06-19 21:24:13 +08:00
/// <summary>
/// 获取模型列表
/// </summary>
/// <returns></returns>
public async Task<List<ModelGetListOutput>> GetModelAsync()
{
2025-06-25 22:45:57 +08:00
var output = await _aiModelRepository._DbQueryable
2025-08-03 23:23:32 +08:00
.Where(x => x.ModelType == ModelTypeEnum.Chat)
2025-06-27 22:13:26 +08:00
.OrderByDescending(x => x.OrderNum)
2025-06-25 22:45:57 +08:00
.Select(x => new ModelGetListOutput
2025-06-27 22:13:26 +08:00
{
Id = x.Id,
Category = "chat",
2025-07-05 15:11:56 +08:00
ModelId = x.ModelId,
2025-06-27 22:13:26 +08:00
ModelName = x.Name,
ModelDescribe = x.Description,
ModelPrice = 0,
ModelType = "1",
ModelShow = "0",
SystemPrompt = null,
ApiHost = null,
ApiKey = null,
Remark = x.Description
}).ToListAsync();
2025-06-21 01:08:14 +08:00
return output;
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="input"></param>
2025-07-17 23:52:00 +08:00
/// <param name="sessionId"></param>
2025-06-21 01:41:05 +08:00
/// <param name="cancellationToken"></param>
2025-08-03 21:47:22 +08:00
[HttpPost("ai-chat/send")]
public async Task PostSendAsync([FromBody] ThorChatCompletionsRequest input, [FromQuery] Guid? sessionId,
2025-07-17 23:10:26 +08:00
CancellationToken cancellationToken)
2025-06-21 01:08:14 +08:00
{
2025-06-25 17:12:09 +08:00
//除了免费模型,其他的模型都要校验
2025-06-30 22:07:42 +08:00
if (!input.Model.Contains("DeepSeek-R1"))
2025-06-29 15:41:49 +08:00
{
//有token需要黑名单校验
if (CurrentUser.IsAuthenticated)
{
await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId());
2025-07-05 15:11:56 +08:00
if (!CurrentUser.IsAiVip())
2025-06-29 15:41:49 +08:00
{
throw new UserFriendlyException("该模型需要VIP用户才能使用请购买VIP后重新登录重试");
}
}
else
{
throw new UserFriendlyException("未登录用户只能使用未加速的DeepSeek-R1请登录后重试");
}
}
2025-08-03 23:23:32 +08:00
//如果是尊享包服务,需要校验是是否尊享包足够
if (CurrentUser.IsAuthenticated && PremiumPackageConst.ModeIds.Contains(input.Model))
{
// 检查尊享token包用量
var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(CurrentUser.GetId());
if (availableTokens <= 0)
{
throw new UserFriendlyException("尊享token包用量不足请先购买尊享token包");
}
}
2025-07-05 15:11:56 +08:00
//ai网关代理httpcontext
2025-07-17 23:10:26 +08:00
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
CurrentUser.Id, sessionId, cancellationToken);
2025-06-19 21:24:13 +08:00
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="input"></param>
/// <param name="cancellationToken"></param>
[HttpPost("ai-chat/FileMaster/send")]
public async Task PostFileMasterSendAsync([FromBody] ThorChatCompletionsRequest input,
CancellationToken cancellationToken)
{
if (!string.IsNullOrWhiteSpace(input.Model))
{
throw new BusinessException("当前接口不支持第三方使用");
}
if (CurrentUser.IsAuthenticated)
{
await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId());
if (CurrentUser.IsAiVip())
{
input.Model = "gpt-5-chat";
}
else
{
input.Model = "gpt-4.1-mini";
}
}
else
{
input.Model = "DeepSeek-R1-0528";
}
//ai网关代理httpcontext
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
CurrentUser.Id, null, cancellationToken);
}
2025-06-19 21:24:13 +08:00
}