mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-03-02 15:50:54 +08:00
refactor: 统一模型前缀处理并规范网关层逻辑
- 抽取并统一使用 ModelConst 处理模型前缀,移除重复的 yi- 前缀判断代码 - 网关层模型 ID 规范化逻辑集中,提升可维护性 - 修复常量文件缺失换行问题 - 前端版本号调整为 3.7.1
This commit is contained in:
@@ -3,4 +3,4 @@
|
||||
public class AiHubConst
|
||||
{
|
||||
public const string VipRole = "YiXinAi-Vip";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
namespace Yi.Framework.AiHub.Domain.Shared.Consts;
|
||||
|
||||
public class ModelConst
|
||||
{
|
||||
/// <summary>
|
||||
/// 需要移除的模型前缀列表
|
||||
/// </summary>
|
||||
private static readonly List<string> ModelPrefixesToRemove =
|
||||
[
|
||||
"yi-",
|
||||
"ma-"
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// 获取模型ID的前缀(如果存在)
|
||||
/// </summary>
|
||||
private static string? GetModelPrefix(string? modelId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(modelId)) return null;
|
||||
|
||||
return ModelPrefixesToRemove.FirstOrDefault(prefix =>
|
||||
modelId!.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除模型ID的前缀,返回标准模型ID
|
||||
/// </summary>
|
||||
public static string RemoveModelPrefix(string? modelId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(modelId)) return string.Empty;
|
||||
|
||||
var prefix = GetModelPrefix(modelId);
|
||||
if (prefix != null)
|
||||
{
|
||||
return modelId[prefix.Length..];
|
||||
}
|
||||
return modelId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理模型ID,如有前缀则移除并返回新字符串
|
||||
/// </summary>
|
||||
public static string ProcessModelId(string? modelId)
|
||||
{
|
||||
return RemoveModelPrefix(modelId);
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ using Yi.Framework.AiHub.Domain.AiGateWay.Exceptions;
|
||||
using Yi.Framework.AiHub.Domain.Entities.Chat;
|
||||
using Yi.Framework.AiHub.Domain.Entities.Model;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
||||
using ModelConst = Yi.Framework.AiHub.Domain.Shared.Consts.ModelConst;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.Gemini;
|
||||
@@ -97,12 +98,8 @@ public class AiGateWayManager : DomainService
|
||||
throw new UserFriendlyException($"【{modelId}】模型当前版本【{modelApiType}】格式不支持");
|
||||
}
|
||||
|
||||
// ✅ 统一处理 yi- 后缀(网关层模型规范化)
|
||||
if (!string.IsNullOrEmpty(aiModelDescribe.ModelId) &&
|
||||
aiModelDescribe.ModelId.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
aiModelDescribe.ModelId = aiModelDescribe.ModelId[3..];
|
||||
}
|
||||
// ✅ 统一处理模型前缀(网关层模型规范化)
|
||||
aiModelDescribe.ModelId = ModelConst.RemoveModelPrefix(aiModelDescribe.ModelId);
|
||||
|
||||
return aiModelDescribe;
|
||||
}
|
||||
@@ -134,11 +131,7 @@ public class AiGateWayManager : DomainService
|
||||
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
|
||||
|
||||
var sourceModelId = request.Model;
|
||||
if (!string.IsNullOrEmpty(request.Model) &&
|
||||
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
request.Model = request.Model[3..];
|
||||
}
|
||||
request.Model = ModelConst.ProcessModelId(request.Model);
|
||||
|
||||
var data = await chatService.CompleteChatAsync(modelDescribe, request, cancellationToken);
|
||||
data.SupplementalMultiplier(modelDescribe.Multiplier);
|
||||
@@ -208,11 +201,7 @@ public class AiGateWayManager : DomainService
|
||||
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
|
||||
|
||||
var sourceModelId = request.Model;
|
||||
if (!string.IsNullOrEmpty(request.Model) &&
|
||||
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
request.Model = request.Model[3..];
|
||||
}
|
||||
request.Model = ModelConst.ProcessModelId(request.Model);
|
||||
|
||||
var completeChatResponse = chatService.CompleteChatStreamAsync(modelDescribe, request, cancellationToken);
|
||||
var tokenUsage = new ThorUsageResponse();
|
||||
@@ -540,11 +529,7 @@ public class AiGateWayManager : DomainService
|
||||
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Messages, request.Model);
|
||||
|
||||
var sourceModelId = request.Model;
|
||||
if (!string.IsNullOrEmpty(request.Model) &&
|
||||
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
request.Model = request.Model[3..];
|
||||
}
|
||||
request.Model = ModelConst.ProcessModelId(request.Model);
|
||||
|
||||
var chatService =
|
||||
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
|
||||
@@ -620,11 +605,7 @@ public class AiGateWayManager : DomainService
|
||||
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
|
||||
|
||||
var sourceModelId = request.Model;
|
||||
if (!string.IsNullOrEmpty(request.Model) &&
|
||||
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
request.Model = request.Model[3..];
|
||||
}
|
||||
request.Model = ModelConst.ProcessModelId(request.Model);
|
||||
|
||||
var completeChatResponse = chatService.StreamChatCompletionsAsync(modelDescribe, request, cancellationToken);
|
||||
ThorUsageResponse? tokenUsage = new ThorUsageResponse();
|
||||
@@ -744,11 +725,7 @@ public class AiGateWayManager : DomainService
|
||||
var chatService =
|
||||
LazyServiceProvider.GetRequiredKeyedService<IOpenAiResponseService>(modelDescribe.HandlerName);
|
||||
var sourceModelId = request.Model;
|
||||
if (!string.IsNullOrEmpty(request.Model) &&
|
||||
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
request.Model = request.Model[3..];
|
||||
}
|
||||
request.Model = ModelConst.ProcessModelId(request.Model);
|
||||
|
||||
var data = await chatService.ResponsesAsync(modelDescribe, request, cancellationToken);
|
||||
|
||||
@@ -820,11 +797,7 @@ public class AiGateWayManager : DomainService
|
||||
var chatService =
|
||||
LazyServiceProvider.GetRequiredKeyedService<IOpenAiResponseService>(modelDescribe.HandlerName);
|
||||
var sourceModelId = request.Model;
|
||||
if (!string.IsNullOrEmpty(request.Model) &&
|
||||
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
request.Model = request.Model[3..];
|
||||
}
|
||||
request.Model = ModelConst.ProcessModelId(request.Model);
|
||||
|
||||
var completeChatResponse = chatService.ResponsesStreamAsync(modelDescribe, request, cancellationToken);
|
||||
ThorUsageResponse? tokenUsage = null;
|
||||
@@ -1164,12 +1137,8 @@ public class AiGateWayManager : DomainService
|
||||
response.Headers.TryAdd("Connection", "keep-alive");
|
||||
|
||||
var sourceModelId = modelId;
|
||||
// 处理 yi- 前缀
|
||||
if (!string.IsNullOrEmpty(modelId) &&
|
||||
modelId.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
modelId = modelId[3..];
|
||||
}
|
||||
// 处理模型前缀
|
||||
modelId = ModelConst.RemoveModelPrefix(modelId);
|
||||
|
||||
var modelDescribe = await GetModelAsync(apiType, sourceModelId);
|
||||
|
||||
@@ -1302,12 +1271,8 @@ public class AiGateWayManager : DomainService
|
||||
// 提取用户最后一条消息
|
||||
var userContent = request.Messages?.LastOrDefault()?.MessagesStore ?? string.Empty;
|
||||
|
||||
// 处理 yi- 前缀
|
||||
if (!string.IsNullOrEmpty(request.Model) &&
|
||||
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
request.Model = request.Model[3..];
|
||||
}
|
||||
// 处理模型前缀
|
||||
request.Model = ModelConst.ProcessModelId(request.Model);
|
||||
|
||||
var chatService = LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
|
||||
var completeChatResponse = chatService.CompleteChatStreamAsync(modelDescribe, request, cancellationToken);
|
||||
@@ -1391,12 +1356,8 @@ public class AiGateWayManager : DomainService
|
||||
userContent = textContent?.Text ?? System.Text.Json.JsonSerializer.Serialize(lastMessage.Contents);
|
||||
}
|
||||
|
||||
// 处理 yi- 前缀
|
||||
if (!string.IsNullOrEmpty(request.Model) &&
|
||||
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
request.Model = request.Model[3..];
|
||||
}
|
||||
// 处理模型前缀
|
||||
request.Model = ModelConst.ProcessModelId(request.Model);
|
||||
|
||||
var chatService = LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
|
||||
var completeChatResponse = chatService.StreamChatCompletionsAsync(modelDescribe, request, cancellationToken);
|
||||
@@ -1509,12 +1470,8 @@ public class AiGateWayManager : DomainService
|
||||
}
|
||||
}
|
||||
|
||||
// 处理 yi- 前缀
|
||||
if (!string.IsNullOrEmpty(request.Model) &&
|
||||
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
request.Model = request.Model[3..];
|
||||
}
|
||||
// 处理模型前缀
|
||||
request.Model = ModelConst.ProcessModelId(request.Model);
|
||||
|
||||
var chatService = LazyServiceProvider.GetRequiredKeyedService<IOpenAiResponseService>(modelDescribe.HandlerName);
|
||||
var completeChatResponse = chatService.ResponsesStreamAsync(modelDescribe, request, cancellationToken);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
|
||||
// 主版本号 - 修改此处即可同步更新所有地方的版本显示
|
||||
export const APP_VERSION = '3.8.0';
|
||||
export const APP_VERSION = '3.7.1';
|
||||
|
||||
// 应用名称
|
||||
export const APP_NAME = '意心AI';
|
||||
|
||||
Reference in New Issue
Block a user