feat: 完成模型api改造

This commit is contained in:
ccnetcore
2026-01-08 21:38:36 +08:00
parent 40aa47bb1e
commit c727aeed99
5 changed files with 48 additions and 35 deletions

View File

@@ -1,4 +1,6 @@
namespace Yi.Framework.AiHub.Application.Contracts.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Enums;
namespace Yi.Framework.AiHub.Application.Contracts.Dtos;
public class ModelGetListOutput
{
@@ -31,4 +33,14 @@ public class ModelGetListOutput
/// 是否为尊享包
/// </summary>
public bool IsPremiumPackage { get; set; }
/// <summary>
/// 是否免费模型
/// </summary>
public bool IsFree { get; set; }
/// <summary>
/// 模型Api类型现支持同一个模型id多种接口格式
/// </summary>
public ModelApiTypeEnum ModelApiType { get; set; }
}

View File

@@ -50,6 +50,7 @@ public class AiChatService : ApplicationService
private readonly IAccountService _accountService;
private readonly ISqlSugarRepository<AgentStoreAggregateRoot> _agentStoreRepository;
private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository;
private const string FreeModelId = "DeepSeek-V3";
public AiChatService(IHttpContextAccessor httpContextAccessor,
AiBlacklistManager aiBlacklistManager,
@@ -58,7 +59,8 @@ public class AiChatService : ApplicationService
ModelManager modelManager,
PremiumPackageManager premiumPackageManager,
ChatManager chatManager, TokenManager tokenManager, IAccountService accountService,
ISqlSugarRepository<AgentStoreAggregateRoot> agentStoreRepository, ISqlSugarRepository<AiModelEntity> aiModelRepository)
ISqlSugarRepository<AgentStoreAggregateRoot> agentStoreRepository,
ISqlSugarRepository<AiModelEntity> aiModelRepository)
{
_httpContextAccessor = httpContextAccessor;
_aiBlacklistManager = aiBlacklistManager;
@@ -94,9 +96,9 @@ public class AiChatService : ApplicationService
public async Task<List<ModelGetListOutput>> GetModelAsync()
{
var output = await _aiModelRepository._DbQueryable
.Where(x=>x.IsEnabled==true)
.Where(x => x.IsEnabled == true)
.Where(x => x.ModelType == ModelTypeEnum.Chat)
.Where(x => x.ModelApiType == ModelApiTypeEnum.OpenAi)
.Where(x => x.ModelApiType == ModelApiTypeEnum.Completions)
.OrderByDescending(x => x.OrderNum)
.Select(x => new ModelGetListOutput
{
@@ -105,8 +107,19 @@ public class AiChatService : ApplicationService
ModelName = x.Name,
ModelDescribe = x.Description,
Remark = x.Description,
IsPremiumPackage = x.IsPremium
IsPremiumPackage = x.IsPremium,
ModelApiType = x.ModelApiType,
}).ToListAsync();
output.ForEach(x =>
{
if (x.ModelId == FreeModelId)
{
x.IsPremiumPackage = false;
x.IsFree = true;
}
});
return output;
}
@@ -122,7 +135,7 @@ public class AiChatService : ApplicationService
CancellationToken cancellationToken)
{
//除了免费模型,其他的模型都要校验
if (!input.Model.Contains("DeepSeek-R1"))
if (input.Model!=FreeModelId)
{
//有token需要黑名单校验
if (CurrentUser.IsAuthenticated)
@@ -173,22 +186,10 @@ public class AiChatService : ApplicationService
{
throw new BusinessException("当前接口不支持第三方使用");
}
input.Model = "gpt-5-chat";
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

View File

@@ -4,15 +4,15 @@ namespace Yi.Framework.AiHub.Domain.Shared.Enums;
public enum ModelApiTypeEnum
{
[Description("OpenAI")]
OpenAi,
[Description("OpanAi Completions")]
Completions,
[Description("Claude")]
Claude,
[Description("Claude Messages")]
Messages,
[Description("Response")]
Response,
[Description("OpanAi Responses")]
Responses,
[Description("GenerateContent")]
[Description("Gemini GenerateContent")]
GenerateContent
}

View File

@@ -128,7 +128,7 @@ public class AiGateWayManager : DomainService
var response = httpContext.Response;
// 设置响应头,声明是 json
//response.ContentType = "application/json; charset=UTF-8";
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, request.Model);
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Completions, request.Model);
var chatService =
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
@@ -202,7 +202,7 @@ public class AiGateWayManager : DomainService
_specialCompatible.Compatible(request);
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, request.Model);
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Completions, request.Model);
var chatService =
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
@@ -351,7 +351,7 @@ public class AiGateWayManager : DomainService
var model = request.Model;
if (string.IsNullOrEmpty(model)) model = "dall-e-2";
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, model);
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Completions, model);
// 获取渠道指定的实现类型的服务
var imageService =
@@ -422,7 +422,7 @@ public class AiGateWayManager : DomainService
using var embedding =
Activity.Current?.Source.StartActivity("向量模型调用");
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, input.Model);
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Completions, input.Model);
// 获取渠道指定的实现类型的服务
var embeddingService =
@@ -536,7 +536,7 @@ public class AiGateWayManager : DomainService
var response = httpContext.Response;
// 设置响应头,声明是 json
//response.ContentType = "application/json; charset=UTF-8";
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Claude, request.Model);
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Messages, request.Model);
var sourceModelId = request.Model;
if (!string.IsNullOrEmpty(request.Model) &&
@@ -617,7 +617,7 @@ public class AiGateWayManager : DomainService
response.Headers.TryAdd("Connection", "keep-alive");
_specialCompatible.AnthropicCompatible(request);
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Claude, request.Model);
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Messages, request.Model);
var chatService =
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
@@ -726,7 +726,7 @@ public class AiGateWayManager : DomainService
var response = httpContext.Response;
// 设置响应头,声明是 json
//response.ContentType = "application/json; charset=UTF-8";
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Response, request.Model);
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Responses, request.Model);
var chatService =
LazyServiceProvider.GetRequiredKeyedService<IOpenAiResponseService>(modelDescribe.HandlerName);
@@ -803,7 +803,7 @@ public class AiGateWayManager : DomainService
response.Headers.TryAdd("Cache-Control", "no-cache");
response.Headers.TryAdd("Connection", "keep-alive");
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Response, request.Model);
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Responses, request.Model);
var chatService =
LazyServiceProvider.GetRequiredKeyedService<IOpenAiResponseService>(modelDescribe.HandlerName);
var sourceModelId = request.Model;

View File

@@ -82,7 +82,7 @@ public class ChatManager : DomainService
response.Headers.TryAdd("Cache-Control", "no-cache");
response.Headers.TryAdd("Connection", "keep-alive");
var modelDescribe = await _aiGateWayManager.GetModelAsync(ModelApiTypeEnum.OpenAi, modelId);
var modelDescribe = await _aiGateWayManager.GetModelAsync(ModelApiTypeEnum.Completions, modelId);
//token状态检查在应用层统一处理
var client = new OpenAIClient(new ApiKeyCredential(token),