feat: 增加图片生成内容安全拦截校验并优化日志信息

This commit is contained in:
ccnetcore
2026-01-18 17:46:34 +08:00
parent 09ecddb552
commit 8b183e289c

View File

@@ -1076,11 +1076,19 @@ public class AiGateWayManager : DomainService
LazyServiceProvider.GetRequiredKeyedService<IGeminiGenerateContentService>(modelDescribe.HandlerName);
var data = await chatService.GenerateContentAsync(modelDescribe, request, cancellationToken);
// 检查是否被大模型内容安全策略拦截
var rawResponse = data.GetRawText();
if (rawResponse.Contains("policies.google.com/terms/generative-ai/use-policy"))
{
_logger.LogWarning($"图片生成被内容安全策略拦截,模型:【{modelId}】,请求信息:【{request}】");
throw new UserFriendlyException("您的提示词涉及敏感信息,已被大模型拦截,请调整提示词后再试!");
}
//解析json获取base64字符串
var imagePrefixBase64 = GeminiGenerateContentAcquirer.GetImagePrefixBase64(data);
if (string.IsNullOrWhiteSpace(imagePrefixBase64))
{
_logger.LogError($"图片生成解析失败,模型id:,请求信息:【{request}】,请求响应信息:【{data}】");
_logger.LogError($"图片生成解析失败,模型:【{modelId}】,请求信息:【{request}】,请求响应信息:【{data}】");
throw new UserFriendlyException("大模型没有返回图片,请调整提示词或稍后再试");
}