feat: 完成ai网关改造

This commit is contained in:
ccnetcore
2025-06-25 22:41:32 +08:00
parent 695aaedfba
commit c5037ea397
5 changed files with 32 additions and 31 deletions

View File

@@ -83,22 +83,22 @@ public class AiChatService : ApplicationService
public async Task PostSendAsync(SendMessageInput input, CancellationToken cancellationToken)
{
//除了免费模型,其他的模型都要校验
if (input.Model != "DeepSeek-R1-0528")
{
//有token需要黑名单校验
if (CurrentUser.IsAuthenticated)
{
await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId());
if (!CurrentUser.Roles.Contains("YiXinAi-Vip"))
{
throw new UserFriendlyException("该模型需要VIP用户才能使用请购买VIP后重新登录重试");
}
}
else
{
throw new UserFriendlyException("未登录用户只能使用未加速的DeepSeek-R1请登录后重试");
}
}
// if (input.Model != "DeepSeek-R1-0528")
// {
// //有token需要黑名单校验
// if (CurrentUser.IsAuthenticated)
// {
// await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId());
// if (!CurrentUser.Roles.Contains("YiXinAi-Vip"))
// {
// throw new UserFriendlyException("该模型需要VIP用户才能使用请购买VIP后重新登录重试");
// }
// }
// else
// {
// throw new UserFriendlyException("未登录用户只能使用未加速的DeepSeek-R1请登录后重试");
// }
// }
//前面都是校验,后面才是真正的调用
var httpContext = this._httpContextAccessor.HttpContext;

View File

@@ -25,7 +25,10 @@ public class AzureChatService : IChatService
new AzureKeyCredential(apiKey));
ChatClient chatClient = azureClient.GetChatClient(deploymentName);
var response = chatClient.CompleteChatStreamingAsync(messages, cancellationToken: cancellationToken);
var response = chatClient.CompleteChatStreamingAsync(messages, new ChatCompletionOptions()
{
MaxOutputTokenCount = 2048
}, cancellationToken: cancellationToken);
await foreach (StreamingChatCompletionUpdate update in response)
{

View File

@@ -19,14 +19,6 @@ public class AzureRestChatService : IChatService
{
// 设置API URL
var apiUrl = $"{aiModelDescribe.Endpoint}models/chat/completions";
var ss = messages.Select(x => new
{
role = x.GetRoleAsString(),
content = x.Content.FirstOrDefault()?.Text
}).ToList();
// 准备请求内容
var requestBody = new
{

View File

@@ -20,10 +20,15 @@ public class UsageStatisticsAggregateRoot : FullAuditedAggregateRoot<Guid>
public string ModelId { get; set; }
/// <summary>
/// token使用
/// 输入使用token使用
/// </summary>
public decimal TotalTokens { get; set; }
public decimal InputTokens { get; set; }
/// <summary>
/// 输出使用token使用
/// </summary>
public decimal OutputTokens { get; set; }
/// <summary>
/// 对话次数
/// </summary>

View File

@@ -1,5 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Domain;
using Yi.Framework.AiHub.Domain.AiChat;
using Yi.Framework.AiHub.Domain.AiChat.Impl;
using Yi.Framework.AiHub.Domain.Shared;
using Yi.Framework.Mapster;
@@ -16,17 +18,16 @@ namespace Yi.Framework.AiHub.Domain
{
var configuration = context.Services.GetConfiguration();
var services = context.Services;
// Configure<AiGateWayOptions>(configuration.GetSection("AiGateWay"));
//
// services.AddKeyedTransient<IChatService, AzureChatService>(nameof(AzureChatService));
// services.AddKeyedTransient<IChatService, AzureRestChatService>(nameof(AzureRestChatService));
services.AddKeyedTransient<IChatService, AzureChatService>(nameof(AzureChatService));
services.AddKeyedTransient<IChatService, AzureRestChatService>(nameof(AzureRestChatService));
}
public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
{
var service = context.ServiceProvider;
}
}
}