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

362 lines
13 KiB
C#
Raw Normal View History

2025-06-25 00:30:01 +08:00
using System.Collections.Concurrent;
2025-12-22 00:17:10 +08:00
using System.Reflection;
2025-06-25 00:30:01 +08:00
using System.Text;
2025-12-22 00:17:10 +08:00
using System.Text.Encodings.Web;
using Dm.util;
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;
2025-12-22 00:17:10 +08:00
using ModelContextProtocol;
using ModelContextProtocol.Server;
2025-06-21 01:08:14 +08:00
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-12-24 00:22:46 +08:00
using Yi.Framework.AiHub.Application.Contracts.Dtos.Chat;
2025-12-22 00:17:10 +08:00
using Yi.Framework.AiHub.Domain;
2025-06-25 17:12:09 +08:00
using Yi.Framework.AiHub.Domain.Entities;
2025-12-24 14:17:32 +08:00
using Yi.Framework.AiHub.Domain.Entities.Chat;
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;
using System.Text.Json;
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 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 ModelManager _modelManager;
private readonly PremiumPackageManager _premiumPackageManager;
2025-12-23 00:49:17 +08:00
private readonly ChatManager _chatManager;
2025-12-24 12:18:33 +08:00
private readonly TokenManager _tokenManager;
private readonly IAccountService _accountService;
2025-12-24 14:17:32 +08:00
private readonly ISqlSugarRepository<AgentStoreAggregateRoot> _agentStoreRepository;
private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository;
private const string FreeModelId = "DeepSeek-V3-0324";
2025-12-24 14:17:32 +08:00
2025-06-25 17:12:09 +08:00
public AiChatService(IHttpContextAccessor httpContextAccessor,
2025-07-05 15:11:56 +08:00
AiBlacklistManager aiBlacklistManager,
2025-12-23 00:49:17 +08:00
ILogger<AiChatService> logger,
AiGateWayManager aiGateWayManager,
ModelManager modelManager,
2025-12-23 00:49:17 +08:00
PremiumPackageManager premiumPackageManager,
2025-12-24 14:17:32 +08:00
ChatManager chatManager, TokenManager tokenManager, IAccountService accountService,
2026-01-08 21:38:36 +08:00
ISqlSugarRepository<AgentStoreAggregateRoot> agentStoreRepository,
ISqlSugarRepository<AiModelEntity> aiModelRepository)
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-07-02 00:28:44 +08:00
_logger = logger;
2025-07-05 15:11:56 +08:00
_aiGateWayManager = aiGateWayManager;
_modelManager = modelManager;
_premiumPackageManager = premiumPackageManager;
2025-12-23 00:49:17 +08:00
_chatManager = chatManager;
2025-12-24 12:18:33 +08:00
_tokenManager = tokenManager;
_accountService = accountService;
2025-12-24 14:17:32 +08:00
_agentStoreRepository = agentStoreRepository;
_aiModelRepository = aiModelRepository;
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>
2026-01-02 19:26:09 +08:00
/// 获取对话模型列表
2025-06-19 21:24:13 +08:00
/// </summary>
/// <returns></returns>
public async Task<List<ModelGetListOutput>> GetModelAsync()
{
2025-06-25 22:45:57 +08:00
var output = await _aiModelRepository._DbQueryable
2026-01-08 21:38:36 +08:00
.Where(x => x.IsEnabled == true)
2025-08-03 23:23:32 +08:00
.Where(x => x.ModelType == ModelTypeEnum.Chat)
// .Where(x => x.ModelApiType == ModelApiTypeEnum.Completions)
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,
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,
Remark = x.Description,
2026-01-08 21:38:36 +08:00
IsPremiumPackage = x.IsPremium,
ModelApiType = x.ModelApiType,
IconUrl = x.IconUrl,
ProviderName = x.ProviderName
2025-06-27 22:13:26 +08:00
}).ToListAsync();
2026-01-08 21:38:36 +08:00
output.ForEach(x =>
{
if (x.ModelId == FreeModelId)
{
x.IsPremiumPackage = false;
x.IsFree = true;
}
});
2025-06-21 01:08:14 +08:00
return output;
}
// /// <summary>
// /// 发送消息
// /// </summary>
// /// <param name="input"></param>
// /// <param name="sessionId"></param>
// /// <param name="cancellationToken"></param>
// [HttpPost("ai-chat/send")]
// [Obsolete]
// public async Task PostSendAsync([FromBody] ThorChatCompletionsRequest input, [FromQuery] Guid? sessionId,
// CancellationToken cancellationToken)
// {
// //除了免费模型,其他的模型都要校验
// if (input.Model!=FreeModelId)
// {
// //有token需要黑名单校验
// if (CurrentUser.IsAuthenticated)
// {
// await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId());
// if (!CurrentUser.IsAiVip())
// {
// throw new UserFriendlyException("该模型需要VIP用户才能使用请购买VIP后重新登录重试");
// }
// }
// else
// {
// throw new UserFriendlyException("未登录用户只能使用未加速的DeepSeek-R1请登录后重试");
// }
// }
//
// //如果是尊享包服务,需要校验是是否尊享包足够
// if (CurrentUser.IsAuthenticated)
// {
// var isPremium = await _modelManager.IsPremiumModelAsync(input.Model);
//
// if (isPremium)
// {
// // 检查尊享token包用量
// var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(CurrentUser.GetId());
// if (availableTokens <= 0)
// {
// throw new UserFriendlyException("尊享token包用量不足请先购买尊享token包");
// }
// }
// }
//
// //ai网关代理httpcontext
// await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
// CurrentUser.Id, sessionId, null, CancellationToken.None);
// }
2025-12-24 00:22:46 +08:00
2025-12-24 14:17:32 +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("当前接口不支持第三方使用");
}
2026-01-08 21:38:36 +08:00
input.Model = "gpt-5-chat";
2025-12-24 14:17:32 +08:00
if (CurrentUser.IsAuthenticated)
{
await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId());
}
//ai网关代理httpcontext
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
CurrentUser.Id, null, null, CancellationToken.None);
2025-12-24 14:17:32 +08:00
}
/// <summary>
/// Agent 发送消息
/// </summary>
2025-12-23 17:08:42 +08:00
[HttpPost("ai-chat/agent/send")]
2025-12-24 00:22:46 +08:00
public async Task PostAgentSendAsync([FromBody] AgentSendInput input, CancellationToken cancellationToken)
2025-12-23 17:08:42 +08:00
{
2026-01-02 19:26:09 +08:00
var tokenValidation = await _tokenManager.ValidateTokenAsync(input.TokenId, input.ModelId);
2025-12-24 12:18:33 +08:00
await _aiBlacklistManager.VerifiyAiBlacklist(tokenValidation.UserId);
// 验证用户是否为VIP
var userInfo = await _accountService.GetAsync(null, null, tokenValidation.UserId);
if (userInfo == null)
{
throw new UserFriendlyException("用户信息不存在");
}
// 检查是否为VIP使用RoleCodes判断
if (!userInfo.RoleCodes.Contains(AiHubConst.VipRole) && userInfo.User.UserName != "cc")
{
throw new UserFriendlyException("该接口为尊享服务专用需要VIP权限才能使用");
}
2025-12-24 14:17:32 +08:00
2025-12-24 12:18:33 +08:00
//如果是尊享包服务,需要校验是是否尊享包足够
var isPremium = await _modelManager.IsPremiumModelAsync(input.ModelId);
if (isPremium)
2025-12-24 12:18:33 +08:00
{
// 检查尊享token包用量
var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(tokenValidation.UserId);
if (availableTokens <= 0)
{
throw new UserFriendlyException("尊享token包用量不足请先购买尊享token包");
}
}
2025-12-24 14:17:32 +08:00
2025-12-24 00:22:46 +08:00
await _chatManager.AgentCompleteChatStreamAsync(_httpContextAccessor.HttpContext,
input.SessionId,
input.Content,
2026-01-02 19:26:09 +08:00
tokenValidation.Token,
2025-12-24 12:18:33 +08:00
tokenValidation.TokenId,
2025-12-24 00:22:46 +08:00
input.ModelId,
2025-12-24 12:18:33 +08:00
tokenValidation.UserId,
2025-12-24 00:22:46 +08:00
input.Tools,
CancellationToken.None);
2025-06-19 21:24:13 +08:00
}
/// <summary>
2025-12-24 14:17:32 +08:00
/// 获取 Agent 工具
/// </summary>
2025-12-24 14:17:32 +08:00
/// <returns></returns>
[HttpPost("ai-chat/agent/tool")]
public List<AgentToolOutput> GetAgentToolAsync()
{
2025-12-24 14:17:32 +08:00
var agentTools = _chatManager.GetTools().Select(x => new AgentToolOutput
{
2025-12-24 14:17:32 +08:00
Code = x.Code,
Name = x.Name
}).ToList();
return agentTools;
}
2025-12-22 00:17:10 +08:00
2025-12-24 14:17:32 +08:00
/// <summary>
/// 获取 Agent 上下文
/// </summary>
/// <returns></returns>
[HttpPost("ai-chat/agent/context/{sessionId}")]
[Authorize]
public async Task<string?> GetAgentContextAsync([FromRoute] Guid sessionId)
2025-12-22 00:17:10 +08:00
{
2025-12-24 14:17:32 +08:00
var data = await _agentStoreRepository.GetFirstAsync(x => x.SessionId == sessionId);
return data?.Store;
2025-12-22 00:17:10 +08:00
}
/// <summary>
/// 统一发送消息 - 支持4种API类型
/// </summary>
/// <param name="apiType">API类型枚举</param>
/// <param name="input">原始请求体JsonElement</param>
/// <param name="modelId">模型IDGemini格式需要从URL传入</param>
/// <param name="sessionId">会话ID</param>
/// <param name="cancellationToken"></param>
[HttpPost("ai-chat/unified/send")]
public async Task PostUnifiedSendAsync(
[FromQuery] ModelApiTypeEnum apiType,
[FromBody] JsonElement input,
[FromQuery] string modelId,
[FromQuery] Guid? sessionId,
CancellationToken cancellationToken)
{
// 从请求体中提取模型ID如果未从URL传入
if (string.IsNullOrEmpty(modelId))
{
modelId = ExtractModelIdFromRequest(apiType, input);
}
// 除了免费模型,其他的模型都要校验
if (modelId != FreeModelId)
{
if (CurrentUser.IsAuthenticated)
{
await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId());
if (!CurrentUser.IsAiVip())
{
throw new UserFriendlyException("该模型需要VIP用户才能使用请购买VIP后重新登录重试");
}
}
else
{
throw new UserFriendlyException("未登录用户只能使用未加速的DeepSeek-R1请登录后重试");
}
}
// 如果是尊享包服务,需要校验是否尊享包足够
if (CurrentUser.IsAuthenticated)
{
var isPremium = await _modelManager.IsPremiumModelAsync(modelId);
if (isPremium)
{
var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(CurrentUser.GetId());
if (availableTokens <= 0)
{
throw new UserFriendlyException("尊享token包用量不足请先购买尊享token包");
}
}
}
// 调用统一流式处理
await _aiGateWayManager.UnifiedStreamForStatisticsAsync(
_httpContextAccessor.HttpContext!,
apiType,
input,
modelId,
CurrentUser.Id,
sessionId,
null,
CancellationToken.None);
}
/// <summary>
/// 从请求体中提取模型ID
/// </summary>
private string ExtractModelIdFromRequest(ModelApiTypeEnum apiType, JsonElement input)
{
try
{
if (input.TryGetProperty("model", out var modelProperty))
{
return modelProperty.GetString() ?? string.Empty;
}
}
catch
{
// 忽略解析错误
}
throw new UserFriendlyException("无法从请求中获取模型ID请在URL参数中指定modelId");
}
2025-06-19 21:24:13 +08:00
}