From c5037ea39752254859070682992369ecf3e9bce5 Mon Sep 17 00:00:00 2001 From: ccnetcore Date: Wed, 25 Jun 2025 22:41:32 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90ai=E7=BD=91?= =?UTF-8?q?=E5=85=B3=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/AiChatService.cs | 32 +++++++++---------- .../AiChat/Impl/AzureChatService.cs | 5 ++- .../AiChat/Impl/AzureRestChatService.cs | 8 ----- .../Entities/UsageStatisticsAggregateRoot.cs | 9 ++++-- .../YiFrameworkAiHubDomainModule.cs | 9 +++--- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs index 9b5e0808..a80a5496 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs @@ -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; diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureChatService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureChatService.cs index 6b8a4c6b..a45f2182 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureChatService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureChatService.cs @@ -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) { diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureRestChatService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureRestChatService.cs index c1419710..fe894b17 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureRestChatService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureRestChatService.cs @@ -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 { diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/UsageStatisticsAggregateRoot.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/UsageStatisticsAggregateRoot.cs index be212d3e..8c9b8b07 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/UsageStatisticsAggregateRoot.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/UsageStatisticsAggregateRoot.cs @@ -20,10 +20,15 @@ public class UsageStatisticsAggregateRoot : FullAuditedAggregateRoot public string ModelId { get; set; } /// - /// 总token使用 + /// 输入使用token使用 /// - public decimal TotalTokens { get; set; } + public decimal InputTokens { get; set; } + /// + /// 输出使用token使用 + /// + public decimal OutputTokens { get; set; } + /// /// 对话次数 /// diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/YiFrameworkAiHubDomainModule.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/YiFrameworkAiHubDomainModule.cs index 3c135ca5..17b15647 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/YiFrameworkAiHubDomainModule.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/YiFrameworkAiHubDomainModule.cs @@ -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(configuration.GetSection("AiGateWay")); // - // services.AddKeyedTransient(nameof(AzureChatService)); - // services.AddKeyedTransient(nameof(AzureRestChatService)); + services.AddKeyedTransient(nameof(AzureChatService)); + services.AddKeyedTransient(nameof(AzureRestChatService)); } public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context) { var service = context.ServiceProvider; - } } } \ No newline at end of file From f0ae27a50b92ed95485746c5876682cc25038973 Mon Sep 17 00:00:00 2001 From: ccnetcore Date: Wed, 25 Jun 2025 22:45:57 +0800 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Yi.Framework.AiHub.Application/Services/AiChatService.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs index a80a5496..58c9b489 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs @@ -30,11 +30,13 @@ public class AiChatService : ApplicationService private readonly AiBlacklistManager _aiBlacklistManager; public AiChatService(IHttpContextAccessor httpContextAccessor, - AiMessageManager aiMessageManager, AiBlacklistManager aiBlacklistManager) + AiMessageManager aiMessageManager, AiBlacklistManager aiBlacklistManager, + ISqlSugarRepository aiModelRepository) { this._httpContextAccessor = httpContextAccessor; _aiMessageManager = aiMessageManager; _aiBlacklistManager = aiBlacklistManager; + _aiModelRepository = aiModelRepository; } From 2732df24af81acc91599ffed96bf0e6a4e3d6018 Mon Sep 17 00:00:00 2001 From: ccnetcore Date: Wed, 25 Jun 2025 22:45:57 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Yi.Framework.AiHub.Application/Services/AiChatService.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs index 58c9b489..ec75cb75 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs @@ -59,7 +59,9 @@ public class AiChatService : ApplicationService /// public async Task> GetModelAsync() { - var output = await _aiModelRepository._DbQueryable.Select(x => new ModelGetListOutput + var output = await _aiModelRepository._DbQueryable + .OrderBy(x=>x.OrderNum) + .Select(x => new ModelGetListOutput { Id = x.Id, Category = "chat", From a5dd3946f8d795fd4a30cca6c0551a306cc7125d Mon Sep 17 00:00:00 2001 From: ccnetcore Date: Wed, 25 Jun 2025 22:45:57 +0800 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Yi.Framework.AiHub.Application/Services/AiChatService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs index ec75cb75..1fefc569 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs @@ -60,7 +60,7 @@ public class AiChatService : ApplicationService public async Task> GetModelAsync() { var output = await _aiModelRepository._DbQueryable - .OrderBy(x=>x.OrderNum) + .OrderByDescending(x=>x.OrderNum) .Select(x => new ModelGetListOutput { Id = x.Id,