Merge remote-tracking branch 'origin/ai-hub' into ai-hub

This commit is contained in:
Gsh
2026-01-04 00:33:14 +08:00
2 changed files with 33 additions and 42 deletions

View File

@@ -61,4 +61,9 @@ public class AiModelDescribe
/// 模型倍率 /// 模型倍率
/// </summary> /// </summary>
public decimal Multiplier { get; set; } public decimal Multiplier { get; set; }
/// <summary>
/// 是否为尊享模型
/// </summary>
public bool IsPremium { get; set; }
} }

View File

@@ -85,19 +85,22 @@ public class AiGateWayManager : DomainService
Description = model.Description, Description = model.Description,
AppExtraUrl = app.ExtraUrl, AppExtraUrl = app.ExtraUrl,
ModelExtraInfo = model.ExtraInfo, ModelExtraInfo = model.ExtraInfo,
Multiplier = model.Multiplier Multiplier = model.Multiplier,
IsPremium = model.IsPremium
}) })
.FirstAsync(); .FirstAsync();
if (aiModelDescribe is null) if (aiModelDescribe is null)
{ {
throw new UserFriendlyException($"【{modelId}】模型当前版本【{modelApiType}】格式不支持"); throw new UserFriendlyException($"【{modelId}】模型当前版本【{modelApiType}】格式不支持");
} }
// ✅ 统一处理 yi- 后缀(网关层模型规范化) // ✅ 统一处理 yi- 后缀(网关层模型规范化)
if (!string.IsNullOrEmpty(aiModelDescribe.ModelId) && if (!string.IsNullOrEmpty(aiModelDescribe.ModelId) &&
aiModelDescribe.ModelId.StartsWith("yi-", StringComparison.OrdinalIgnoreCase)) aiModelDescribe.ModelId.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
{ {
aiModelDescribe.ModelId = aiModelDescribe.ModelId[3..]; aiModelDescribe.ModelId = aiModelDescribe.ModelId[3..];
} }
return aiModelDescribe; return aiModelDescribe;
} }
@@ -126,14 +129,14 @@ public class AiGateWayManager : DomainService
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, request.Model); var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, request.Model);
var chatService = var chatService =
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName); LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
var sourceModelId = request.Model; var sourceModelId = request.Model;
if (!string.IsNullOrEmpty(request.Model) && if (!string.IsNullOrEmpty(request.Model) &&
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase)) request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
{ {
request.Model = request.Model[3..]; request.Model = request.Model[3..];
} }
var data = await chatService.CompleteChatAsync(modelDescribe, request, cancellationToken); var data = await chatService.CompleteChatAsync(modelDescribe, request, cancellationToken);
data.SupplementalMultiplier(modelDescribe.Multiplier); data.SupplementalMultiplier(modelDescribe.Multiplier);
if (userId is not null) if (userId is not null)
@@ -157,13 +160,7 @@ public class AiGateWayManager : DomainService
await _usageStatisticsManager.SetUsageAsync(userId.Value, sourceModelId, data.Usage, tokenId); await _usageStatisticsManager.SetUsageAsync(userId.Value, sourceModelId, data.Usage, tokenId);
// 扣减尊享token包用量 if (modelDescribe.IsPremium)
var isPremium = await _aiModelRepository._DbQueryable
.Where(x => x.ModelId == request.Model)
.Select(x => x.IsPremium)
.FirstAsync();
if (isPremium)
{ {
var totalTokens = data.Usage?.TotalTokens ?? 0; var totalTokens = data.Usage?.TotalTokens ?? 0;
if (totalTokens > 0) if (totalTokens > 0)
@@ -212,8 +209,8 @@ public class AiGateWayManager : DomainService
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase)) request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
{ {
request.Model = request.Model[3..]; request.Model = request.Model[3..];
} }
var completeChatResponse = chatService.CompleteChatStreamAsync(modelDescribe, request, cancellationToken); var completeChatResponse = chatService.CompleteChatStreamAsync(modelDescribe, request, cancellationToken);
var tokenUsage = new ThorUsageResponse(); var tokenUsage = new ThorUsageResponse();
@@ -322,12 +319,7 @@ public class AiGateWayManager : DomainService
// 扣减尊享token包用量 // 扣减尊享token包用量
if (userId is not null) if (userId is not null)
{ {
var isPremium = await _aiModelRepository._DbQueryable if (modelDescribe.IsPremium)
.Where(x => x.ModelId == request.Model)
.Select(x => x.IsPremium)
.FirstAsync();
if (isPremium)
{ {
var totalTokens = tokenUsage.TotalTokens ?? 0; var totalTokens = tokenUsage.TotalTokens ?? 0;
if (totalTokens > 0) if (totalTokens > 0)
@@ -390,21 +382,13 @@ public class AiGateWayManager : DomainService
await _usageStatisticsManager.SetUsageAsync(userId, model, response.Usage, tokenId); await _usageStatisticsManager.SetUsageAsync(userId, model, response.Usage, tokenId);
// 扣减尊享token包用量 // 直接扣减尊享token包用量
if (userId is not null) if (userId is not null)
{ {
var isPremium = await _aiModelRepository._DbQueryable var totalTokens = response.Usage.TotalTokens ?? 0;
.Where(x => x.ModelId == request.Model) if (totalTokens > 0)
.Select(x => x.IsPremium)
.FirstAsync();
if (isPremium)
{ {
var totalTokens = response.Usage.TotalTokens ?? 0; await PremiumPackageManager.TryConsumeTokensAsync(userId.Value, totalTokens);
if (totalTokens > 0)
{
await PremiumPackageManager.TryConsumeTokensAsync(userId.Value, totalTokens);
}
} }
} }
} }
@@ -558,11 +542,11 @@ public class AiGateWayManager : DomainService
{ {
request.Model = request.Model[3..]; request.Model = request.Model[3..];
} }
var chatService = var chatService =
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName); LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
var data = await chatService.ChatCompletionsAsync(modelDescribe, request, cancellationToken); var data = await chatService.ChatCompletionsAsync(modelDescribe, request, cancellationToken);
data.SupplementalMultiplier(modelDescribe.Multiplier); data.SupplementalMultiplier(modelDescribe.Multiplier);
if (userId is not null) if (userId is not null)
@@ -585,7 +569,7 @@ public class AiGateWayManager : DomainService
await _usageStatisticsManager.SetUsageAsync(userId.Value, sourceModelId, data.TokenUsage, tokenId); await _usageStatisticsManager.SetUsageAsync(userId.Value, sourceModelId, data.TokenUsage, tokenId);
// 扣减尊享token包用量 // 直接扣减尊享token包用量
var totalTokens = data.TokenUsage.TotalTokens ?? 0; var totalTokens = data.TokenUsage.TotalTokens ?? 0;
if (totalTokens > 0) if (totalTokens > 0)
{ {
@@ -632,7 +616,7 @@ public class AiGateWayManager : DomainService
{ {
request.Model = request.Model[3..]; request.Model = request.Model[3..];
} }
var completeChatResponse = chatService.StreamChatCompletionsAsync(modelDescribe, request, cancellationToken); var completeChatResponse = chatService.StreamChatCompletionsAsync(modelDescribe, request, cancellationToken);
ThorUsageResponse? tokenUsage = null; ThorUsageResponse? tokenUsage = null;
StringBuilder backupSystemContent = new StringBuilder(); StringBuilder backupSystemContent = new StringBuilder();
@@ -677,7 +661,7 @@ public class AiGateWayManager : DomainService
await _usageStatisticsManager.SetUsageAsync(userId, sourceModelId, tokenUsage, tokenId); await _usageStatisticsManager.SetUsageAsync(userId, sourceModelId, tokenUsage, tokenId);
// 扣减尊享token包用量 // 直接扣减尊享token包用量
if (userId.HasValue && tokenUsage is not null) if (userId.HasValue && tokenUsage is not null)
{ {
var totalTokens = tokenUsage.TotalTokens ?? 0; var totalTokens = tokenUsage.TotalTokens ?? 0;
@@ -719,7 +703,7 @@ public class AiGateWayManager : DomainService
{ {
request.Model = request.Model[3..]; request.Model = request.Model[3..];
} }
var data = await chatService.ResponsesAsync(modelDescribe, request, cancellationToken); var data = await chatService.ResponsesAsync(modelDescribe, request, cancellationToken);
data.SupplementalMultiplier(modelDescribe.Multiplier); data.SupplementalMultiplier(modelDescribe.Multiplier);
@@ -750,7 +734,7 @@ public class AiGateWayManager : DomainService
await _usageStatisticsManager.SetUsageAsync(userId.Value, sourceModelId, tokenUsage, tokenId); await _usageStatisticsManager.SetUsageAsync(userId.Value, sourceModelId, tokenUsage, tokenId);
// 扣减尊享token包用量 // 直接扣减尊享token包用量
var totalTokens = tokenUsage.TotalTokens ?? 0; var totalTokens = tokenUsage.TotalTokens ?? 0;
if (totalTokens > 0) if (totalTokens > 0)
{ {
@@ -795,6 +779,7 @@ public class AiGateWayManager : DomainService
{ {
request.Model = request.Model[3..]; request.Model = request.Model[3..];
} }
var completeChatResponse = chatService.ResponsesStreamAsync(modelDescribe, request, cancellationToken); var completeChatResponse = chatService.ResponsesStreamAsync(modelDescribe, request, cancellationToken);
ThorUsageResponse? tokenUsage = null; ThorUsageResponse? tokenUsage = null;
try try
@@ -848,7 +833,7 @@ public class AiGateWayManager : DomainService
await _usageStatisticsManager.SetUsageAsync(userId, sourceModelId, tokenUsage, tokenId); await _usageStatisticsManager.SetUsageAsync(userId, sourceModelId, tokenUsage, tokenId);
// 扣减尊享token包用量 // 直接扣减尊享token包用量
if (userId.HasValue && tokenUsage is not null) if (userId.HasValue && tokenUsage is not null)
{ {
var totalTokens = tokenUsage.TotalTokens ?? 0; var totalTokens = tokenUsage.TotalTokens ?? 0;
@@ -908,7 +893,7 @@ public class AiGateWayManager : DomainService
await _usageStatisticsManager.SetUsageAsync(userId.Value, modelId, tokenUsage, tokenId); await _usageStatisticsManager.SetUsageAsync(userId.Value, modelId, tokenUsage, tokenId);
// 扣减尊享token包用量 // 直接扣减尊享token包用量
var totalTokens = tokenUsage.TotalTokens ?? 0; var totalTokens = tokenUsage.TotalTokens ?? 0;
if (totalTokens > 0) if (totalTokens > 0)
{ {
@@ -992,7 +977,7 @@ public class AiGateWayManager : DomainService
await _usageStatisticsManager.SetUsageAsync(userId, modelId, tokenUsage, tokenId); await _usageStatisticsManager.SetUsageAsync(userId, modelId, tokenUsage, tokenId);
// 扣减尊享token包用量 // 直接扣减尊享token包用量
if (userId.HasValue && tokenUsage is not null) if (userId.HasValue && tokenUsage is not null)
{ {
var totalTokens = tokenUsage.TotalTokens ?? 0; var totalTokens = tokenUsage.TotalTokens ?? 0;
@@ -1004,6 +989,7 @@ public class AiGateWayManager : DomainService
} }
private const string ImageStoreHost = "https://ccnetcore.com/prod-api"; private const string ImageStoreHost = "https://ccnetcore.com/prod-api";
/// <summary> /// <summary>
/// Gemini 生成(Image)-非流式-缓存处理 /// Gemini 生成(Image)-非流式-缓存处理
/// 返回图片绝对路径 /// 返回图片绝对路径
@@ -1060,7 +1046,7 @@ public class AiGateWayManager : DomainService
await _usageStatisticsManager.SetUsageAsync(userId, modelId, tokenUsage, tokenId); await _usageStatisticsManager.SetUsageAsync(userId, modelId, tokenUsage, tokenId);
// 扣减尊享token包用量 // 直接扣减尊享token包用量
var totalTokens = tokenUsage.TotalTokens ?? 0; var totalTokens = tokenUsage.TotalTokens ?? 0;
if (totalTokens > 0) if (totalTokens > 0)
{ {