Commit ef5e56d5605ca716785bb8c2427aa86ef4e7bd0e
1 parent
59c74452
【代码优化】AI:硅基流动的图片生成
Showing
4 changed files
with
35 additions
and
6 deletions
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/factory/AiModelFactoryImpl.java
| ... | ... | @@ -207,6 +207,8 @@ public class AiModelFactoryImpl implements AiModelFactory { |
| 207 | 207 | return SpringUtil.getBean(QianFanImageModel.class); |
| 208 | 208 | case ZHI_PU: |
| 209 | 209 | return SpringUtil.getBean(ZhiPuAiImageModel.class); |
| 210 | + case SILICON_FLOW: | |
| 211 | + return SpringUtil.getBean(SiliconFlowImageModel.class); | |
| 210 | 212 | case OPENAI: |
| 211 | 213 | return SpringUtil.getBean(OpenAiImageModel.class); |
| 212 | 214 | case STABLE_DIFFUSION: | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/siliconflow/SiliconFlowApiConstants.java
| ... | ... | @@ -27,6 +27,8 @@ public final class SiliconFlowApiConstants { |
| 27 | 27 | |
| 28 | 28 | public static final String MODEL_DEFAULT = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"; |
| 29 | 29 | |
| 30 | + public static final String DEFAULT_IMAGE_MODEL = "Kwai-Kolors/Kolors"; | |
| 31 | + | |
| 30 | 32 | public static final String PROVIDER_NAME = "Siiconflow"; |
| 31 | 33 | |
| 32 | 34 | } | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/siliconflow/SiliconFlowImageModel.java
| ... | ... | @@ -31,6 +31,7 @@ import org.springframework.ai.openai.api.OpenAiImageApi; |
| 31 | 31 | import org.springframework.ai.openai.metadata.OpenAiImageGenerationMetadata; |
| 32 | 32 | import org.springframework.ai.retry.RetryUtils; |
| 33 | 33 | import org.springframework.http.ResponseEntity; |
| 34 | +import org.springframework.lang.Nullable; | |
| 34 | 35 | import org.springframework.retry.support.RetryTemplate; |
| 35 | 36 | import org.springframework.util.Assert; |
| 36 | 37 | |
| ... | ... | @@ -82,7 +83,8 @@ public class SiliconFlowImageModel implements ImageModel { |
| 82 | 83 | |
| 83 | 84 | @Override |
| 84 | 85 | public ImageResponse call(ImagePrompt imagePrompt) { |
| 85 | - SiliconFlowImageApi.SiliconflowImageRequest imageRequest = createRequest(imagePrompt); | |
| 86 | + SiliconFlowImageOptions requestImageOptions = mergeOptions(imagePrompt.getOptions(), this.defaultOptions); | |
| 87 | + SiliconFlowImageApi.SiliconflowImageRequest imageRequest = createRequest(imagePrompt, requestImageOptions); | |
| 86 | 88 | |
| 87 | 89 | var observationContext = ImageModelObservationContext.builder() |
| 88 | 90 | .imagePrompt(imagePrompt) |
| ... | ... | @@ -105,13 +107,14 @@ public class SiliconFlowImageModel implements ImageModel { |
| 105 | 107 | }); |
| 106 | 108 | } |
| 107 | 109 | |
| 108 | - private SiliconFlowImageApi.SiliconflowImageRequest createRequest(ImagePrompt imagePrompt) { | |
| 110 | + private SiliconFlowImageApi.SiliconflowImageRequest createRequest(ImagePrompt imagePrompt, | |
| 111 | + SiliconFlowImageOptions requestImageOptions) { | |
| 109 | 112 | String instructions = imagePrompt.getInstructions().get(0).getText(); |
| 110 | 113 | |
| 111 | 114 | SiliconFlowImageApi.SiliconflowImageRequest imageRequest = new SiliconFlowImageApi.SiliconflowImageRequest(instructions, |
| 112 | - imagePrompt.getOptions().getModel()); | |
| 115 | + SiliconFlowApiConstants.DEFAULT_IMAGE_MODEL); | |
| 113 | 116 | |
| 114 | - return ModelOptionsUtils.merge(imagePrompt.getOptions(), imageRequest, SiliconFlowImageApi.SiliconflowImageRequest.class); | |
| 117 | + return ModelOptionsUtils.merge(requestImageOptions, imageRequest, SiliconFlowImageApi.SiliconflowImageRequest.class); | |
| 115 | 118 | } |
| 116 | 119 | |
| 117 | 120 | private ImageResponse convertResponse(ResponseEntity<OpenAiImageApi.OpenAiImageResponse> imageResponseEntity, |
| ... | ... | @@ -131,4 +134,26 @@ public class SiliconFlowImageModel implements ImageModel { |
| 131 | 134 | ImageResponseMetadata openAiImageResponseMetadata = new ImageResponseMetadata(imageApiResponse.created()); |
| 132 | 135 | return new ImageResponse(imageGenerationList, openAiImageResponseMetadata); |
| 133 | 136 | } |
| 137 | + | |
| 138 | + private SiliconFlowImageOptions mergeOptions(@Nullable ImageOptions runtimeOptions, SiliconFlowImageOptions defaultOptions) { | |
| 139 | + var runtimeOptionsForProvider = ModelOptionsUtils.copyToTarget(runtimeOptions, ImageOptions.class, | |
| 140 | + SiliconFlowImageOptions.class); | |
| 141 | + | |
| 142 | + if (runtimeOptionsForProvider == null) { | |
| 143 | + return defaultOptions; | |
| 144 | + } | |
| 145 | + | |
| 146 | + return SiliconFlowImageOptions.builder() | |
| 147 | + // Handle portable image options | |
| 148 | + .model(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getModel(), defaultOptions.getModel())) | |
| 149 | + .batchSize(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getN(), defaultOptions.getN())) | |
| 150 | + .width(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getWidth(), defaultOptions.getWidth())) | |
| 151 | + .height(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getHeight(), defaultOptions.getHeight())) | |
| 152 | + // Handle OpenAI specific image options | |
| 153 | + .negativePrompt(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getNegativePrompt(), defaultOptions.getNegativePrompt())) | |
| 154 | + .numInferenceSteps(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getNumInferenceSteps(), defaultOptions.getNumInferenceSteps())) | |
| 155 | + .guidanceScale(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getGuidanceScale(), defaultOptions.getGuidanceScale())) | |
| 156 | + .seed(ModelOptionsUtils.mergeOption(runtimeOptionsForProvider.getSeed(), defaultOptions.getSeed())) | |
| 157 | + .build(); | |
| 158 | + } | |
| 134 | 159 | } | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/model/siliconflow/SiliconFlowImageOptions.java
| ... | ... | @@ -89,12 +89,12 @@ public class SiliconFlowImageOptions implements ImageOptions { |
| 89 | 89 | |
| 90 | 90 | @Override |
| 91 | 91 | public Integer getN() { |
| 92 | - return null; | |
| 92 | + return batchSize; | |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | @Override |
| 96 | 96 | public String getResponseFormat() { |
| 97 | - return null; | |
| 97 | + return "url"; | |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | @Override | ... | ... |