- ChatManager:
- 引入 System.Text.Json,用于将 agent thread 序列化与反序列化(示例:thread.Serialize(...) -> JsonSerializer.Deserialize -> agent.DeserializeThread)。
- 增加示例:创建 OpenAIClient、初始化 agent、运行流式响应并处理更新。
- 小幅格式和空行调整。
- AiChatService:
- 为 Agent 发送接口 PostAgentSendAsync 增加注释与路由标记 HttpPost("ai-chat/agent/send")。
注意:提交中出现了硬编码的 API Key,请尽快改为从配置或机密管理中读取以防泄露。
219 lines
7.6 KiB
C#
219 lines
7.6 KiB
C#
using System.Collections.Concurrent;
|
||
using System.Reflection;
|
||
using System.Text;
|
||
using System.Text.Encodings.Web;
|
||
using Dm.util;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Microsoft.Extensions.Logging;
|
||
using Microsoft.Extensions.Options;
|
||
using ModelContextProtocol;
|
||
using ModelContextProtocol.Server;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Serialization;
|
||
using OpenAI.Chat;
|
||
using Volo.Abp.Application.Services;
|
||
using Volo.Abp.Users;
|
||
using Yi.Framework.AiHub.Application.Contracts.Dtos;
|
||
using Yi.Framework.AiHub.Domain;
|
||
using Yi.Framework.AiHub.Domain.Entities;
|
||
using Yi.Framework.AiHub.Domain.Entities.Model;
|
||
using Yi.Framework.AiHub.Domain.Extensions;
|
||
using Yi.Framework.AiHub.Domain.Managers;
|
||
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
||
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||
|
||
namespace Yi.Framework.AiHub.Application.Services;
|
||
|
||
/// <summary>
|
||
/// ai服务
|
||
/// </summary>
|
||
public class AiChatService : ApplicationService
|
||
{
|
||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||
private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository;
|
||
private readonly AiBlacklistManager _aiBlacklistManager;
|
||
private readonly ILogger<AiChatService> _logger;
|
||
private readonly AiGateWayManager _aiGateWayManager;
|
||
private readonly PremiumPackageManager _premiumPackageManager;
|
||
private readonly ChatManager _chatManager;
|
||
|
||
public AiChatService(IHttpContextAccessor httpContextAccessor,
|
||
AiBlacklistManager aiBlacklistManager,
|
||
ISqlSugarRepository<AiModelEntity> aiModelRepository,
|
||
ILogger<AiChatService> logger,
|
||
AiGateWayManager aiGateWayManager,
|
||
PremiumPackageManager premiumPackageManager,
|
||
ChatManager chatManager)
|
||
{
|
||
_httpContextAccessor = httpContextAccessor;
|
||
_aiBlacklistManager = aiBlacklistManager;
|
||
_aiModelRepository = aiModelRepository;
|
||
_logger = logger;
|
||
_aiGateWayManager = aiGateWayManager;
|
||
_premiumPackageManager = premiumPackageManager;
|
||
_chatManager = chatManager;
|
||
}
|
||
|
||
|
||
/// <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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取模型列表
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public async Task<List<ModelGetListOutput>> GetModelAsync()
|
||
{
|
||
var output = await _aiModelRepository._DbQueryable
|
||
.Where(x => x.ModelType == ModelTypeEnum.Chat)
|
||
.Where(x => x.ModelApiType == ModelApiTypeEnum.OpenAi)
|
||
.OrderByDescending(x => x.OrderNum)
|
||
.Select(x => new ModelGetListOutput
|
||
{
|
||
Id = x.Id,
|
||
Category = "chat",
|
||
ModelId = x.ModelId,
|
||
ModelName = x.Name,
|
||
ModelDescribe = x.Description,
|
||
ModelPrice = 0,
|
||
ModelType = "1",
|
||
ModelShow = "0",
|
||
SystemPrompt = null,
|
||
ApiHost = null,
|
||
ApiKey = null,
|
||
Remark = x.Description,
|
||
IsPremiumPackage = PremiumPackageConst.ModeIds.Contains(x.ModelId)
|
||
}).ToListAsync();
|
||
return output;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 发送消息
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <param name="sessionId"></param>
|
||
/// <param name="cancellationToken"></param>
|
||
[HttpPost("ai-chat/send")]
|
||
public async Task PostSendAsync([FromBody] ThorChatCompletionsRequest input, [FromQuery] Guid? sessionId,
|
||
CancellationToken cancellationToken)
|
||
{
|
||
//除了免费模型,其他的模型都要校验
|
||
if (!input.Model.Contains("DeepSeek-R1"))
|
||
{
|
||
//有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 && PremiumPackageConst.ModeIds.Contains(input.Model))
|
||
{
|
||
// 检查尊享token包用量
|
||
var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(CurrentUser.GetId());
|
||
if (availableTokens <= 0)
|
||
{
|
||
throw new UserFriendlyException("尊享token包用量不足,请先购买尊享token包");
|
||
}
|
||
}
|
||
|
||
// 使用 ChatManager
|
||
}
|
||
|
||
/// <summary>
|
||
/// Agent 发送消息
|
||
/// </summary>
|
||
[HttpPost("ai-chat/agent/send")]
|
||
public async Task PostAgentSendAsync()
|
||
{
|
||
await _chatManager.CompleteChatStreamAsync();
|
||
}
|
||
|
||
/// <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, null, cancellationToken);
|
||
}
|
||
|
||
[HttpPost("ai-chat/tool")]
|
||
public string GetTool()
|
||
{
|
||
var toolClasses = typeof(YiFrameworkAiHubDomainModule).Assembly.GetTypes()
|
||
.Where(x => x.GetCustomAttribute<McpServerToolTypeAttribute>() is not null)
|
||
.ToList();
|
||
|
||
List<McpServerTool> mcpTools = new List<McpServerTool>();
|
||
foreach (var toolClass in toolClasses)
|
||
{
|
||
var instance = LazyServiceProvider.GetRequiredService(toolClass);
|
||
var toolMethods = toolClass.GetMethods()
|
||
.Where(y => y.GetCustomAttribute<McpServerToolAttribute>() is not null).ToList();
|
||
foreach (var toolMethod in toolMethods)
|
||
{
|
||
mcpTools.add(McpServerTool.Create(toolMethod, instance));
|
||
}
|
||
}
|
||
|
||
var json = System.Text.Json.JsonSerializer.Serialize(mcpTools.Select(x => x.ProtocolTool).ToList(),
|
||
McpJsonUtilities.DefaultOptions);
|
||
return json;
|
||
}
|
||
} |