修改配置增加模特功能修改

fix:模特有时会没有设置成功性别
豆包qwen模型增加违规提示词提醒
This commit is contained in:
litianxiang
2025-10-13 13:42:38 +08:00
parent 67f0ce7c6c
commit 02f7031a67
5 changed files with 308 additions and 76 deletions

View File

@@ -53,7 +53,7 @@ public class CollectionElement implements Serializable {
private String level2Type;
/**
* 三级类型 目前为线稿标注性别
* 三级类型 目前为线稿和模特标注性别
*/
@TableField("level3_type")
private String level3Type;

View File

@@ -579,6 +579,7 @@ public class CollectionElementServiceImpl extends ServiceImpl<CollectionElementM
//去掉与性别不符合的线稿元素 - 使用 Iterator 安全移除元素
Iterator<CollectionSketchDTO> sketchIterator = designDTO.getSketchBoards().iterator();
while (sketchIterator.hasNext()) {
try {
CollectionSketchDTO sketchBoard = sketchIterator.next();
String level2Type = sketchBoard.getLevel2Type();
String level3Type = null;
@@ -598,6 +599,9 @@ public class CollectionElementServiceImpl extends ServiceImpl<CollectionElementM
if (!level3Type.equalsIgnoreCase(designDTO.getModelSex())) {
sketchIterator.remove();
}
} catch (Exception e) {
continue;
}
}
//再次判断草图板数量如果为null则跳出当前if

View File

@@ -31,6 +31,7 @@ import com.alibaba.dashscope.utils.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
@@ -674,7 +675,9 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
// processCreditDeduction(generateDTO.getUserId(), taskId, CreditsEventsEnum.WX_TEXT2IMG);
return new PrepareForGenerateVO(Collections.singletonList(taskId), 200);
}
public String toProductAsyncTask(String imagePath, String useModel, String prompt) {
long startTime = System.currentTimeMillis();
log.info("开始执行toProductAsyncTask - model: {}, imagePath: {}, prompt: {}",
useModel, imagePath, prompt);
@@ -888,7 +891,6 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
}
public String createGoogleAsyncTask(GenerateThroughImageTextDTO generateDTO, String useModel, String prompt) {
// 从 resources 加载 JSON 文件
String level1Type = generateDTO.getLevel1Type();
@@ -987,6 +989,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
// imagen 模型的参数
parametersObj.set("addWatermark", false);
parametersObj.set("sampleCount", 1);
parametersObj.set("aspectRatio", "9:16");
// 构建完整的请求体
requestBody.set("instances", Arrays.asList(instanceObj));
@@ -1154,7 +1157,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
taskId,
level1Type,
level2Type,
prompt,
generateDTO.getText(),
"text(" + gender + ")",
useModel, // 记录使用的具体模型名称
new Date()
@@ -1162,7 +1165,6 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
save(generate);
return taskId;
}
@@ -1335,16 +1337,37 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
try {
result = imageSynthesis.asyncCall(param);
} catch (Exception e) {
String exceptionMessage = e.getMessage();
if (exceptionMessage != null && exceptionMessage.contains("\"message\"")) {
try {
// 解析JSON格式的异常信息
JSONObject jsonObj = JSONUtil.parseObj(exceptionMessage);
exceptionMessage = jsonObj.getStr("message");
if ("Input data may contain inappropriate content.".equals(exceptionMessage)){
LambdaQueryWrapper<Account> select = new LambdaQueryWrapper<Account>().eq(Account::getId, userId).select(Account::getLanguage);
Account account = accountService.getOne(select);
if ("CHINESE_SIMPLIFIED".equals(account.getLanguage())){
exceptionMessage = "输入数据可能包含不当内容。";
}
}
throw new RuntimeException(exceptionMessage);
} catch (Exception parseException) {
// 如果解析失败,返回原始消息
throw new RuntimeException(exceptionMessage);
}
}
throw new RuntimeException(e.getMessage());
}
String taskId = result.getOutput().getTaskId();
log.info("qwen text2image 请求生成:{}, taskId{}", JsonUtils.toJson(result), taskId);
Generate generate = new Generate(userId, taskId, level1Type, level2Type, prompt, "text(" + gender + ")", "qwen-image", new Date());
Generate generate = new Generate(userId, taskId, level1Type, level2Type, generateDTO.getText(), "text(" + gender + ")", "qwen-image", new Date());
save(generate);
return taskId;
}
public String createDouBaoAsyncTask(GenerateThroughImageTextDTO generateDTO, String useModel, String prompt) {
// 从DTO中获取基础参数
String level1Type = generateDTO.getLevel1Type();
@@ -1393,6 +1416,19 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
requestBuilder.image(finalImagePath1);
}
// 保存生成记录到数据库
Generate generate = new Generate(
userId,
taskId,
level1Type,
level2Type,
generateDTO.getText(),
"text(" + gender + ")",
useModel, // 记录使用的具体模型名称
new Date()
);
save(generate);
GenerateImagesRequest generateRequest = requestBuilder.build();
ImagesResponse imagesResponse = service.generateImages(generateRequest);
@@ -1404,27 +1440,88 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
GenerateResultVO successResultVO = new GenerateResultVO(taskId, null, imageUrl, "Success");
redisUtil.addToString(key, new Gson().toJson(successResultVO), CommonConstant.GENERATE_RESULT_EXPIRE_TIME);
// 保存生成记录到数据库
Generate generate = new Generate(
userId,
taskId,
level1Type,
level2Type,
prompt,
"text(" + gender + ")",
useModel, // 记录使用的具体模型名称
new Date()
);
save(generate);
// TODO: 处理积分扣除逻辑
} catch (Exception e) {
log.error("Doubao image generation failed for taskId: {}", taskId, e);
// 生成失败更新Redis状态
GenerateResultVO failResultVO = new GenerateResultVO(taskId, null, null, "Fail");
LambdaQueryWrapper<Account> select = new LambdaQueryWrapper<Account>().eq(Account::getId, userId).select(Account::getLanguage);
Account account = accountService.getOne(select);
// 根据用户语言设置默认错误信息
String errorMessage = "图像生成失败,请稍后重试"; // 默认中文
String userLanguage = "CHINESE_SIMPLIFIED"; // 默认语言
if (account != null && account.getLanguage() != null) {
userLanguage = account.getLanguage();
if (account.getLanguage().equals("ENGLISH")) {
errorMessage = "Image generation failed, please try again later";
} else if (account.getLanguage().equals("CHINESE_SIMPLIFIED")) {
errorMessage = "图像生成失败,请稍后重试";
}
}
String errorCode = null;
// 检查是否为ArkHttpException提取code和detailMessage
try {
// 通过反射获取code字段
java.lang.reflect.Field codeField = e.getClass().getDeclaredField("code");
codeField.setAccessible(true);
Object codeValue = codeField.get(e);
if (codeValue != null) {
errorCode = codeValue.toString();
}
// 通过反射获取detailMessage字段
e.printStackTrace();
String detailMessageValue = e.getMessage();
if (detailMessageValue != null) {
String detailMessage = detailMessageValue.toString();
// 根据错误码返回相应的用户友好错误信息
errorMessage = getDoubaoErrorMessage(errorCode, detailMessage, userLanguage);
}
} catch (Exception reflectionException) {
reflectionException.printStackTrace();
log.warn("Failed to extract error details from ArkHttpException: {}", reflectionException.getMessage());
// 如果反射失败,尝试从异常消息中提取信息
String exceptionMessage = e.getMessage();
if (exceptionMessage != null) {
boolean isEnglish = "ENGLISH".equals(userLanguage);
if (exceptionMessage.contains("OutputImageSensitiveContentDetected")) {
errorMessage = isEnglish ? "Generated image may contain sensitive content, please try with different input" : "生成的图像可能包含敏感信息,请更换输入内容后重试";
} else if (exceptionMessage.contains("InputTextSensitiveContentDetected")) {
errorMessage = isEnglish ? "Input text may contain sensitive content, please try again" : "输入文本可能包含敏感信息,请更换后重试";
} else if (exceptionMessage.contains("InputImageSensitiveContentDetected")) {
errorMessage = isEnglish ? "Input image may contain sensitive content, please try again" : "输入图像可能包含敏感信息,请更换后重试";
} else if (exceptionMessage.contains("SensitiveContentDetected")) {
errorMessage = isEnglish ? "Input content may contain sensitive information, please try again" : "输入内容可能包含敏感信息,请更换后重试";
} else if (exceptionMessage.contains("MissingParameter")) {
errorMessage = isEnglish ? "Request parameters are incomplete, please check input" : "请求参数不完整,请检查输入内容";
} else if (exceptionMessage.contains("InvalidParameter")) {
errorMessage = isEnglish ? "Request parameters are invalid, please check input format" : "请求参数无效,请检查输入格式";
} else if (exceptionMessage.contains("InvalidImageURL")) {
errorMessage = isEnglish ? "Image format is incorrect or data is corrupted, please re-upload" : "图片格式不正确或数据损坏,请重新上传";
} else if (exceptionMessage.contains("OutofContextError")) {
errorMessage = isEnglish ? "Input content is too long, please shorten text or image size" : "输入内容过长,请缩短文本或图片尺寸";
} else if (exceptionMessage.contains("AuthenticationError")) {
errorMessage = isEnglish ? "Service authentication failed, please contact administrator" : "服务认证失败,请联系管理员";
} else if (exceptionMessage.contains("TooManyRequests") || exceptionMessage.contains("429")) {
errorMessage = isEnglish ? "Too many requests, please try again later" : "请求过于频繁,请稍后重试";
} else if (exceptionMessage.contains("InternalServerError") || exceptionMessage.contains("500")) {
errorMessage = isEnglish ? "Image generation service is temporarily unavailable, please try again later" : "图像生成服务暂时不可用,请稍后重试";
}
}
}
// 更新Redis状态为失败
GenerateResultVO failResultVO = new GenerateResultVO(taskId, null, errorMessage, "Fail");
redisUtil.addToString(key, new Gson().toJson(failResultVO), CommonConstant.GENERATE_RESULT_EXPIRE_TIME);
// 记录详细错误信息用于调试
log.error("Doubao API error - taskId: {}, errorCode: {}, errorMessage: {}",
taskId, errorCode, errorMessage);
}
});
@@ -3238,6 +3335,12 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
} else {
throw new BusinessException("Unknown generate task");
}
} else if ("Fail".equals(cachedResult.getStatus())) {
// 获取失败原因
String errorMessage = cachedResult.getUrl();
if (errorMessage != null) {
throw new BusinessException(errorMessage, ResultEnum.PROMPT.getCode());
}
}
// 4. 其他状态(如 Unknown返回默认失败
@@ -4019,4 +4122,123 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
return null;
}
}
/**
* 根据豆包错误码和详细信息返回用户友好的错误信息
*
* @param errorCode 错误码
* @param detailMessage 详细错误信息
* @return 用户友好的错误信息
*/
private String getDoubaoErrorMessage(String errorCode, String detailMessage, String userLanguage) {
boolean isEnglish = "ENGLISH".equals(userLanguage);
if (errorCode == null) {
return isEnglish ? "Image generation failed, please try again later" : "图像生成失败,请稍后重试";
}
switch (errorCode) {
// === 用户输入问题(用户可以自行解决) ===
// 敏感内容检测 - 用户需要修改提示词
case "SensitiveContentDetected":
case "SensitiveContentDetected.SevereViolation":
case "SensitiveContentDetected.Violence":
case "InputTextSensitiveContentDetected":
return isEnglish ? "Your prompt contains sensitive content. Please modify your description and try again."
: "您的提示词包含敏感内容,请修改描述后重试";
case "InputImageSensitiveContentDetected":
return isEnglish ? "Your uploaded image contains sensitive content. Please use a different image and try again."
: "您上传的图片包含敏感内容,请更换图片后重试";
case "InputVideoSensitiveContentDetected":
return isEnglish ? "Your uploaded video contains sensitive content. Please use a different video and try again."
: "您上传的视频包含敏感内容,请更换视频后重试";
case "OutputTextSensitiveContentDetected":
case "OutputImageSensitiveContentDetected":
case "OutputVideoSensitiveContentDetected":
return isEnglish ? "The generated content may contain sensitive information. Please modify your prompt and try again."
: "生成的内容可能包含敏感信息,请修改提示词后重试";
// 图片格式问题 - 用户需要检查图片
case "InvalidImageURL.EmptyURL":
return isEnglish ? "No image was uploaded. Please upload an image and try again."
: "未上传图片,请上传图片后重试";
case "InvalidImageURL.InvalidFormat":
return isEnglish ? "The image format is not supported or the image is corrupted. Please upload a valid image (JPG, PNG, etc.) and try again."
: "图片格式不支持或图片已损坏请上传有效的图片格式JPG、PNG等后重试";
// 内容长度问题 - 用户需要缩短内容
case "OutofContextError":
return isEnglish ? "Your prompt and image content is too long. Please shorten your description or use a smaller image and try again."
: "您的提示词和图片内容过长,请缩短描述或使用更小的图片后重试";
// 请求频率问题 - 用户需要等待
case "TooManyRequests":
return isEnglish ? "You are sending requests too frequently. Please wait a moment and try again."
: "您的请求过于频繁,请稍等片刻后重试";
// === 系统问题(需要联系管理员) ===
// 参数格式错误 - 系统问题
case "MissingParameter":
case "InvalidParameter":
case "Duplicate.Tags.Key":
case "InvalidArgumentError":
case "InvalidArgumentError.UnknownRole":
case "InvalidArgumentError.InvalidImageDetail":
case "InvalidArgumentError.InvalidPixelLimit":
return isEnglish ? "System error: Invalid request parameters. Please contact the administrator."
: "系统错误:请求参数格式有误,请联系管理员";
// 认证问题 - 系统问题
case "AuthenticationError":
return isEnglish ? "System error: Authentication failed. Please contact the administrator."
: "系统错误:身份验证失败,请联系管理员";
// 服务配置问题 - 系统问题
case "InvalidEndpoint.ClosedEndpoint":
case "OperationDenied.InvalidState":
case "OperationDenied.ConflictedValidationSet":
case "NotFound.Model":
case "NotFound.Endpoint":
case "NotFound.Context":
case "InvalidOperation.Conflict":
case "ModelNotOpen":
return isEnglish ? "System error: Service configuration issue. Please contact the administrator."
: "系统错误:服务配置问题,请联系管理员";
// 配额/费用问题 - 系统问题
case "QuotaExceeded.DailyQuota":
case "QuotaExceeded.MonthlyQuota":
case "QuotaExceeded.TotalQuota":
case "QuotaExceeded.RPM":
case "QuotaExceeded.TPM":
case "QuotaExceeded.RPD":
case "QuotaExceeded.TPD":
return isEnglish ? "System error: Service quota exceeded. Please contact the administrator."
: "系统错误:服务配额已用完,请联系管理员";
// 服务器错误 - 系统问题
case "InternalError":
case "ServiceUnavailable":
return isEnglish ? "System error: Service is temporarily unavailable. Please contact the administrator or try again later."
: "系统错误:服务暂时不可用,请联系管理员或稍后重试";
default:
// 根据详细信息判断是用户问题还是系统问题
if (detailMessage != null) {
if (detailMessage.contains("sensitive") || detailMessage.contains("敏感")) {
return isEnglish ? "Your content contains sensitive information. Please modify and try again."
: "您的内容包含敏感信息,请修改后重试";
} else if (detailMessage.contains("quota") || detailMessage.contains("配额") ||
detailMessage.contains("parameter") || detailMessage.contains("参数") ||
detailMessage.contains("auth") || detailMessage.contains("认证")) {
return isEnglish ? "System error occurred. Please contact the administrator."
: "系统错误,请联系管理员";
}
}
return isEnglish ? "Image generation failed. Please try again later or contact the administrator if the problem persists."
: "图像生成失败,请稍后重试,如问题持续请联系管理员";
}
}
}

View File

@@ -604,6 +604,8 @@ public class LLMServiceImpl implements LLMService {
collectionElement.setAccountId(userHolder.getId());
collectionElement.setProjectId(project.getId());
collectionElement.setLevel1Type(CollectionLevel1TypeEnum.MODEL.getRealName());
collectionElement.setLevel3Type(workspaceService.getProjectSexById(project.getId()));
// collectionElement.setLevel2Type(board.getLevel2Type());
collectionElement.setName(sysFile.getName());
collectionElement.setUrl(sysFile.getUrl());

View File

@@ -728,7 +728,10 @@ public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceMapper, Workspace
if (!workspaceNew.getSex().equals(workspace.getSex())) {
// deleteSketchByProjectId(projectId);
SysFile sysFile = sysFileService.getOneBySex(projectDTO.getStyleId(), projectDTO.getWorkspace().getSex(), projectDTO.getWorkspace().getAgeGroup());
if (!checkIfModelExistsInProject(sysFile.getMd5(), projectId)) {
LambdaQueryWrapper<CollectionElement> queryWrapper = new LambdaQueryWrapper<CollectionElement>().eq(CollectionElement::getProjectId, projectId)
.eq(CollectionElement::getLevel1Type, CollectionLevel1TypeEnum.MODEL.getRealName())
.eq(CollectionElement::getLevel3Type, workspaceNew.getSex());
if (collectionElementMapper.selectCount(queryWrapper) == 0){
CollectionElement collectionElement = new CollectionElement();
collectionElement.setAccountId(userInfo.getId());
collectionElement.setProjectId(projectId);
@@ -812,6 +815,7 @@ public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceMapper, Workspace
collectionElement.setAccountId(userInfo.getId());
collectionElement.setProjectId(project.getId());
collectionElement.setLevel1Type(CollectionLevel1TypeEnum.MODEL.getRealName());
collectionElement.setLevel3Type(projectDTO.getWorkspace().getSex());
collectionElement.setName(sysFile.getName());
collectionElement.setUrl(sysFile.getUrl());
collectionElement.setMd5(sysFile.getMd5());