fix: AI 对话解析多模型 JSON 配置,取 defaultModelId
All checks were successful
Build and Deploy / deploy (push) Successful in 1m12s
All checks were successful
Build and Deploy / deploy (push) Successful in 1m12s
兼容 apiSecret 纯模型名与 {defaultModelId,models} 结构,避免多模型配置后请求失败。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
efbb18c7c3
commit
fc48b84f80
@ -80,7 +80,11 @@ public class AiLlmClient
|
|||||||
throw new BusinessException($"请先配置 {provider.DisplayName} 的 API Key");
|
throw new BusinessException($"请先配置 {provider.DisplayName} 的 API Key");
|
||||||
}
|
}
|
||||||
|
|
||||||
var model = modelOverride ?? provider.ApiSecretEnc;
|
var model = modelOverride;
|
||||||
|
if (string.IsNullOrWhiteSpace(model))
|
||||||
|
{
|
||||||
|
model = ResolveConfiguredModelId(provider.ApiSecretEnc);
|
||||||
|
}
|
||||||
if (string.IsNullOrWhiteSpace(model))
|
if (string.IsNullOrWhiteSpace(model))
|
||||||
{
|
{
|
||||||
model = DefaultModelForProviderType(provider.ProviderType);
|
model = DefaultModelForProviderType(provider.ProviderType);
|
||||||
@ -273,6 +277,84 @@ public class AiLlmClient
|
|||||||
"zhipu" => "glm-5.1",
|
"zhipu" => "glm-5.1",
|
||||||
_ => null
|
_ => null
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 解析 ApiSecret:兼容纯模型名,或多模型 JSON
|
||||||
|
/// {"defaultModelId":"...","models":[{"modelId":"...","role":"文字"}]}。
|
||||||
|
/// </summary>
|
||||||
|
private static string? ResolveConfiguredModelId(string? apiSecret)
|
||||||
|
{
|
||||||
|
var secret = (apiSecret ?? string.Empty).Trim();
|
||||||
|
if (string.IsNullOrWhiteSpace(secret))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!secret.StartsWith('{'))
|
||||||
|
{
|
||||||
|
return secret;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var doc = JsonDocument.Parse(secret);
|
||||||
|
var root = doc.RootElement;
|
||||||
|
if (root.TryGetProperty("defaultModelId", out var defaultModel) &&
|
||||||
|
defaultModel.ValueKind == JsonValueKind.String)
|
||||||
|
{
|
||||||
|
var id = defaultModel.GetString()?.Trim();
|
||||||
|
if (!string.IsNullOrWhiteSpace(id))
|
||||||
|
{
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (root.TryGetProperty("models", out var models) &&
|
||||||
|
models.ValueKind == JsonValueKind.Array)
|
||||||
|
{
|
||||||
|
foreach (var item in models.EnumerateArray())
|
||||||
|
{
|
||||||
|
if (!item.TryGetProperty("modelId", out var modelIdEl) ||
|
||||||
|
modelIdEl.ValueKind != JsonValueKind.String)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var modelId = modelIdEl.GetString()?.Trim();
|
||||||
|
if (string.IsNullOrWhiteSpace(modelId))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.TryGetProperty("role", out var roleEl) &&
|
||||||
|
roleEl.ValueKind == JsonValueKind.String &&
|
||||||
|
string.Equals(roleEl.GetString(), "文字", StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
return modelId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var item in models.EnumerateArray())
|
||||||
|
{
|
||||||
|
if (item.TryGetProperty("modelId", out var modelIdEl) &&
|
||||||
|
modelIdEl.ValueKind == JsonValueKind.String)
|
||||||
|
{
|
||||||
|
var modelId = modelIdEl.GetString()?.Trim();
|
||||||
|
if (!string.IsNullOrWhiteSpace(modelId))
|
||||||
|
{
|
||||||
|
return modelId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (JsonException)
|
||||||
|
{
|
||||||
|
return secret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class AiLlmServiceExtensions
|
public static class AiLlmServiceExtensions
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user