refactor: 清理无用代码并统一网关处理逻辑
- 移除未使用的 using 和多余空行,优化代码可读性 - 统一 yi- 前缀模型名处理逻辑,减少重复代码 - 使用 EnsureSuccessStatusCode 简化图片上传错误处理流程 - 不影响现有功能,仅做代码结构和规范优化
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Net;
|
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
@@ -93,14 +92,12 @@ public class AiGateWayManager : DomainService
|
|||||||
{
|
{
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -546,11 +543,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)
|
||||||
@@ -620,7 +617,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();
|
||||||
@@ -980,7 +977,6 @@ public class AiGateWayManager : DomainService
|
|||||||
}
|
}
|
||||||
|
|
||||||
private const string ImageStoreHost = "http://localhost:19001/api/app";
|
private const string ImageStoreHost = "http://localhost:19001/api/app";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gemini 生成(Image)-非流式-缓存处理
|
/// Gemini 生成(Image)-非流式-缓存处理
|
||||||
/// 返回图片绝对路径
|
/// 返回图片绝对路径
|
||||||
@@ -1017,13 +1013,7 @@ public class AiGateWayManager : DomainService
|
|||||||
var uploadUrl = $"{ImageStoreHost}/ai-image/upload-base64";
|
var uploadUrl = $"{ImageStoreHost}/ai-image/upload-base64";
|
||||||
var content = new StringContent(JsonSerializer.Serialize(imagePrefixBase64), Encoding.UTF8, "application/json");
|
var content = new StringContent(JsonSerializer.Serialize(imagePrefixBase64), Encoding.UTF8, "application/json");
|
||||||
var uploadResponse = await httpClient.PostAsync(uploadUrl, content, cancellationToken);
|
var uploadResponse = await httpClient.PostAsync(uploadUrl, content, cancellationToken);
|
||||||
// uploadResponse.EnsureSuccessStatusCode();
|
uploadResponse.EnsureSuccessStatusCode();
|
||||||
if (uploadResponse.StatusCode != HttpStatusCode.OK)
|
|
||||||
{
|
|
||||||
var errorMessage = await uploadResponse.Content.ReadAsStringAsync(cancellationToken);
|
|
||||||
throw new UserFriendlyException($"{errorMessage}");
|
|
||||||
}
|
|
||||||
|
|
||||||
var storeUrl = await uploadResponse.Content.ReadAsStringAsync(cancellationToken);
|
var storeUrl = await uploadResponse.Content.ReadAsStringAsync(cancellationToken);
|
||||||
|
|
||||||
var tokenUsage = new ThorUsageResponse
|
var tokenUsage = new ThorUsageResponse
|
||||||
|
|||||||
Reference in New Issue
Block a user