Skip to content

Commit 1d933ac

Browse files
fix: WanxImageModel Image response list IndexOutOfBound (#307)
* fix: WanxImageModel Image response list IndexOutOfBound wanx2.1-t2i-turbo model will return error code whrn prompt contains illegal info which casued IndexOutOfBound Exception when processing respons. * make format --------- Co-authored-by: Martin7-1 <[email protected]>
1 parent e0ff56e commit 1d933ac

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

models/langchain4j-community-dashscope/src/main/java/dev/langchain4j/community/model/dashscope/WanxImageModel.java

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,43 +88,26 @@ public WanxImageModel(
8888

8989
@Override
9090
public Response<Image> generate(String prompt) {
91-
ImageSynthesisParam.ImageSynthesisParamBuilder<?, ?> builder =
92-
requestBuilder(prompt).n(1);
93-
94-
try {
95-
imageSynthesisParamCustomizer.accept(builder);
96-
ImageSynthesisResult result = imageSynthesis.call(builder.build());
97-
return Response.from(imagesFrom(result).get(0));
98-
} catch (NoApiKeyException e) {
99-
throw new IllegalArgumentException(e);
100-
}
91+
return Response.from(doGenerate(requestBuilder(prompt).n(1)).get(0));
10192
}
10293

10394
@Override
10495
public Response<List<Image>> generate(String prompt, int n) {
105-
ImageSynthesisParam.ImageSynthesisParamBuilder<?, ?> builder =
106-
requestBuilder(prompt).n(n);
107-
108-
try {
109-
imageSynthesisParamCustomizer.accept(builder);
110-
ImageSynthesisResult result = imageSynthesis.call(builder.build());
111-
return Response.from(imagesFrom(result));
112-
} catch (NoApiKeyException e) {
113-
throw new IllegalArgumentException(e);
114-
}
96+
return Response.from(doGenerate(requestBuilder(prompt).n(n)));
11597
}
11698

11799
@Override
118100
public Response<Image> edit(Image image, String prompt) {
119101
String imageUrl = imageUrl(image, modelName, apiKey);
120-
121102
ImageSynthesisParam.ImageSynthesisParamBuilder<?, ?> builder =
122103
requestBuilder(prompt).refImage(imageUrl).n(1);
123-
124104
if (imageUrl.startsWith("oss://")) {
125105
builder.header("X-DashScope-OssResourceResolve", "enable");
126106
}
107+
return Response.from(doGenerate(builder).get(0));
108+
}
127109

110+
private List<Image> doGenerate(ImageSynthesisParam.ImageSynthesisParamBuilder<?, ?> builder) {
128111
try {
129112
imageSynthesisParamCustomizer.accept(builder);
130113
ImageSynthesisResult result = imageSynthesis.call(builder.build());
@@ -135,7 +118,7 @@ public Response<Image> edit(Image image, String prompt) {
135118
String.format("[%s] %s: %s", output.getTaskStatus(), output.getCode(), output.getMessage());
136119
throw new IllegalStateException(errorMessage);
137120
}
138-
return Response.from(images.get(0));
121+
return images;
139122
} catch (NoApiKeyException e) {
140123
throw new IllegalArgumentException(e);
141124
}

models/langchain4j-community-dashscope/src/test/java/dev/langchain4j/community/model/dashscope/WanxImageModelIT.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import static dev.langchain4j.community.model.dashscope.QwenTestHelper.apiKey;
44
import static dev.langchain4j.community.model.dashscope.QwenTestHelper.multimodalImageData;
55
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
67

78
import dev.langchain4j.data.image.Image;
89
import dev.langchain4j.model.output.Response;
910
import java.net.URI;
11+
import org.junit.jupiter.api.Test;
1012
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
1113
import org.junit.jupiter.params.ParameterizedTest;
1214
import org.junit.jupiter.params.provider.MethodSource;
@@ -135,4 +137,17 @@ void simple_image_edition_works_by_data(String modelName) {
135137
log.info("Your remote image is here: {}", remoteImage);
136138
assertThat(remoteImage).isNotNull();
137139
}
140+
141+
// This case only occurs when using model WANX2_1_T2I_TURBO.
142+
@Test
143+
void simple_image_generation_return_error_msg_when_prompt_illegal() {
144+
WanxImageModel model = WanxImageModel.builder()
145+
.apiKey(apiKey())
146+
.modelName(WanxModelName.WANX2_1_T2I_TURBO)
147+
.build();
148+
149+
assertThatThrownBy(() -> model.generate("Draw a Donald Duck."))
150+
.isInstanceOf(IllegalStateException.class)
151+
.hasMessageContaining("IPInfringementSuspect");
152+
}
138153
}

0 commit comments

Comments
 (0)