feat: 完成模型api改造
This commit is contained in:
@@ -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
|
public class ModelGetListOutput
|
||||||
{
|
{
|
||||||
@@ -31,4 +33,14 @@ public class ModelGetListOutput
|
|||||||
/// 是否为尊享包
|
/// 是否为尊享包
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsPremiumPackage { get; set; }
|
public bool IsPremiumPackage { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否免费模型
|
||||||
|
/// </summary>
|
||||||
|
public bool IsFree { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 模型Api类型,现支持同一个模型id,多种接口格式
|
||||||
|
/// </summary>
|
||||||
|
public ModelApiTypeEnum ModelApiType { get; set; }
|
||||||
}
|
}
|
||||||
@@ -50,6 +50,7 @@ public class AiChatService : ApplicationService
|
|||||||
private readonly IAccountService _accountService;
|
private readonly IAccountService _accountService;
|
||||||
private readonly ISqlSugarRepository<AgentStoreAggregateRoot> _agentStoreRepository;
|
private readonly ISqlSugarRepository<AgentStoreAggregateRoot> _agentStoreRepository;
|
||||||
private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository;
|
private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository;
|
||||||
|
private const string FreeModelId = "DeepSeek-V3";
|
||||||
|
|
||||||
public AiChatService(IHttpContextAccessor httpContextAccessor,
|
public AiChatService(IHttpContextAccessor httpContextAccessor,
|
||||||
AiBlacklistManager aiBlacklistManager,
|
AiBlacklistManager aiBlacklistManager,
|
||||||
@@ -58,7 +59,8 @@ public class AiChatService : ApplicationService
|
|||||||
ModelManager modelManager,
|
ModelManager modelManager,
|
||||||
PremiumPackageManager premiumPackageManager,
|
PremiumPackageManager premiumPackageManager,
|
||||||
ChatManager chatManager, TokenManager tokenManager, IAccountService accountService,
|
ChatManager chatManager, TokenManager tokenManager, IAccountService accountService,
|
||||||
ISqlSugarRepository<AgentStoreAggregateRoot> agentStoreRepository, ISqlSugarRepository<AiModelEntity> aiModelRepository)
|
ISqlSugarRepository<AgentStoreAggregateRoot> agentStoreRepository,
|
||||||
|
ISqlSugarRepository<AiModelEntity> aiModelRepository)
|
||||||
{
|
{
|
||||||
_httpContextAccessor = httpContextAccessor;
|
_httpContextAccessor = httpContextAccessor;
|
||||||
_aiBlacklistManager = aiBlacklistManager;
|
_aiBlacklistManager = aiBlacklistManager;
|
||||||
@@ -94,9 +96,9 @@ public class AiChatService : ApplicationService
|
|||||||
public async Task<List<ModelGetListOutput>> GetModelAsync()
|
public async Task<List<ModelGetListOutput>> GetModelAsync()
|
||||||
{
|
{
|
||||||
var output = await _aiModelRepository._DbQueryable
|
var output = await _aiModelRepository._DbQueryable
|
||||||
.Where(x=>x.IsEnabled==true)
|
.Where(x => x.IsEnabled == true)
|
||||||
.Where(x => x.ModelType == ModelTypeEnum.Chat)
|
.Where(x => x.ModelType == ModelTypeEnum.Chat)
|
||||||
.Where(x => x.ModelApiType == ModelApiTypeEnum.OpenAi)
|
.Where(x => x.ModelApiType == ModelApiTypeEnum.Completions)
|
||||||
.OrderByDescending(x => x.OrderNum)
|
.OrderByDescending(x => x.OrderNum)
|
||||||
.Select(x => new ModelGetListOutput
|
.Select(x => new ModelGetListOutput
|
||||||
{
|
{
|
||||||
@@ -105,8 +107,19 @@ public class AiChatService : ApplicationService
|
|||||||
ModelName = x.Name,
|
ModelName = x.Name,
|
||||||
ModelDescribe = x.Description,
|
ModelDescribe = x.Description,
|
||||||
Remark = x.Description,
|
Remark = x.Description,
|
||||||
IsPremiumPackage = x.IsPremium
|
IsPremiumPackage = x.IsPremium,
|
||||||
|
ModelApiType = x.ModelApiType,
|
||||||
}).ToListAsync();
|
}).ToListAsync();
|
||||||
|
|
||||||
|
output.ForEach(x =>
|
||||||
|
{
|
||||||
|
if (x.ModelId == FreeModelId)
|
||||||
|
{
|
||||||
|
x.IsPremiumPackage = false;
|
||||||
|
x.IsFree = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,7 +135,7 @@ public class AiChatService : ApplicationService
|
|||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
//除了免费模型,其他的模型都要校验
|
//除了免费模型,其他的模型都要校验
|
||||||
if (!input.Model.Contains("DeepSeek-R1"))
|
if (input.Model!=FreeModelId)
|
||||||
{
|
{
|
||||||
//有token,需要黑名单校验
|
//有token,需要黑名单校验
|
||||||
if (CurrentUser.IsAuthenticated)
|
if (CurrentUser.IsAuthenticated)
|
||||||
@@ -173,22 +186,10 @@ public class AiChatService : ApplicationService
|
|||||||
{
|
{
|
||||||
throw new BusinessException("当前接口不支持第三方使用");
|
throw new BusinessException("当前接口不支持第三方使用");
|
||||||
}
|
}
|
||||||
|
input.Model = "gpt-5-chat";
|
||||||
if (CurrentUser.IsAuthenticated)
|
if (CurrentUser.IsAuthenticated)
|
||||||
{
|
{
|
||||||
await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId());
|
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
|
//ai网关代理httpcontext
|
||||||
|
|||||||
@@ -4,15 +4,15 @@ namespace Yi.Framework.AiHub.Domain.Shared.Enums;
|
|||||||
|
|
||||||
public enum ModelApiTypeEnum
|
public enum ModelApiTypeEnum
|
||||||
{
|
{
|
||||||
[Description("OpenAI")]
|
[Description("OpanAi Completions")]
|
||||||
OpenAi,
|
Completions,
|
||||||
|
|
||||||
[Description("Claude")]
|
[Description("Claude Messages")]
|
||||||
Claude,
|
Messages,
|
||||||
|
|
||||||
[Description("Response")]
|
[Description("OpanAi Responses")]
|
||||||
Response,
|
Responses,
|
||||||
|
|
||||||
[Description("GenerateContent")]
|
[Description("Gemini GenerateContent")]
|
||||||
GenerateContent
|
GenerateContent
|
||||||
}
|
}
|
||||||
@@ -128,7 +128,7 @@ public class AiGateWayManager : DomainService
|
|||||||
var response = httpContext.Response;
|
var response = httpContext.Response;
|
||||||
// 设置响应头,声明是 json
|
// 设置响应头,声明是 json
|
||||||
//response.ContentType = "application/json; charset=UTF-8";
|
//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 =
|
var chatService =
|
||||||
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
|
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
|
||||||
|
|
||||||
@@ -202,7 +202,7 @@ public class AiGateWayManager : DomainService
|
|||||||
|
|
||||||
|
|
||||||
_specialCompatible.Compatible(request);
|
_specialCompatible.Compatible(request);
|
||||||
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, request.Model);
|
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Completions, request.Model);
|
||||||
var chatService =
|
var chatService =
|
||||||
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
|
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
|
||||||
|
|
||||||
@@ -351,7 +351,7 @@ public class AiGateWayManager : DomainService
|
|||||||
var model = request.Model;
|
var model = request.Model;
|
||||||
if (string.IsNullOrEmpty(model)) model = "dall-e-2";
|
if (string.IsNullOrEmpty(model)) model = "dall-e-2";
|
||||||
|
|
||||||
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, model);
|
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Completions, model);
|
||||||
|
|
||||||
// 获取渠道指定的实现类型的服务
|
// 获取渠道指定的实现类型的服务
|
||||||
var imageService =
|
var imageService =
|
||||||
@@ -422,7 +422,7 @@ public class AiGateWayManager : DomainService
|
|||||||
using var embedding =
|
using var embedding =
|
||||||
Activity.Current?.Source.StartActivity("向量模型调用");
|
Activity.Current?.Source.StartActivity("向量模型调用");
|
||||||
|
|
||||||
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.OpenAi, input.Model);
|
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Completions, input.Model);
|
||||||
|
|
||||||
// 获取渠道指定的实现类型的服务
|
// 获取渠道指定的实现类型的服务
|
||||||
var embeddingService =
|
var embeddingService =
|
||||||
@@ -536,7 +536,7 @@ public class AiGateWayManager : DomainService
|
|||||||
var response = httpContext.Response;
|
var response = httpContext.Response;
|
||||||
// 设置响应头,声明是 json
|
// 设置响应头,声明是 json
|
||||||
//response.ContentType = "application/json; charset=UTF-8";
|
//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;
|
var sourceModelId = request.Model;
|
||||||
if (!string.IsNullOrEmpty(request.Model) &&
|
if (!string.IsNullOrEmpty(request.Model) &&
|
||||||
@@ -617,7 +617,7 @@ public class AiGateWayManager : DomainService
|
|||||||
response.Headers.TryAdd("Connection", "keep-alive");
|
response.Headers.TryAdd("Connection", "keep-alive");
|
||||||
|
|
||||||
_specialCompatible.AnthropicCompatible(request);
|
_specialCompatible.AnthropicCompatible(request);
|
||||||
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Claude, request.Model);
|
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Messages, request.Model);
|
||||||
var chatService =
|
var chatService =
|
||||||
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
|
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
|
||||||
|
|
||||||
@@ -726,7 +726,7 @@ public class AiGateWayManager : DomainService
|
|||||||
var response = httpContext.Response;
|
var response = httpContext.Response;
|
||||||
// 设置响应头,声明是 json
|
// 设置响应头,声明是 json
|
||||||
//response.ContentType = "application/json; charset=UTF-8";
|
//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 =
|
var chatService =
|
||||||
LazyServiceProvider.GetRequiredKeyedService<IOpenAiResponseService>(modelDescribe.HandlerName);
|
LazyServiceProvider.GetRequiredKeyedService<IOpenAiResponseService>(modelDescribe.HandlerName);
|
||||||
@@ -803,7 +803,7 @@ public class AiGateWayManager : DomainService
|
|||||||
response.Headers.TryAdd("Cache-Control", "no-cache");
|
response.Headers.TryAdd("Cache-Control", "no-cache");
|
||||||
response.Headers.TryAdd("Connection", "keep-alive");
|
response.Headers.TryAdd("Connection", "keep-alive");
|
||||||
|
|
||||||
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Response, request.Model);
|
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Responses, request.Model);
|
||||||
var chatService =
|
var chatService =
|
||||||
LazyServiceProvider.GetRequiredKeyedService<IOpenAiResponseService>(modelDescribe.HandlerName);
|
LazyServiceProvider.GetRequiredKeyedService<IOpenAiResponseService>(modelDescribe.HandlerName);
|
||||||
var sourceModelId = request.Model;
|
var sourceModelId = request.Model;
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ public class ChatManager : DomainService
|
|||||||
response.Headers.TryAdd("Cache-Control", "no-cache");
|
response.Headers.TryAdd("Cache-Control", "no-cache");
|
||||||
response.Headers.TryAdd("Connection", "keep-alive");
|
response.Headers.TryAdd("Connection", "keep-alive");
|
||||||
|
|
||||||
var modelDescribe = await _aiGateWayManager.GetModelAsync(ModelApiTypeEnum.OpenAi, modelId);
|
var modelDescribe = await _aiGateWayManager.GetModelAsync(ModelApiTypeEnum.Completions, modelId);
|
||||||
|
|
||||||
//token状态检查,在应用层统一处理
|
//token状态检查,在应用层统一处理
|
||||||
var client = new OpenAIClient(new ApiKeyCredential(token),
|
var client = new OpenAIClient(new ApiKeyCredential(token),
|
||||||
|
|||||||
Reference in New Issue
Block a user