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..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 @@ -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; } @@ -57,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 + .OrderByDescending(x=>x.OrderNum) + .Select(x => new ModelGetListOutput { Id = x.Id, Category = "chat", @@ -83,22 +87,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