Skip to content

Commit a46e014

Browse files
devholic22markpollack
authored andcommitted
Add comments and prevent IndexOutOfBounds in ImageResponse
* Add comments in ImageResponse like ChatResponse * Prevent IndexOutOfBounds in getResult function * Update toString for formatting like ChatResponse
1 parent ef5a3fa commit a46e014

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

spring-ai-core/src/main/java/org/springframework/ai/image/ImageResponse.java

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,81 @@
1919
import java.util.Objects;
2020

2121
import org.springframework.ai.model.ModelResponse;
22+
import org.springframework.util.CollectionUtils;
2223

24+
/**
25+
* The image completion (e.g. imageGeneration) response returned by an AI provider.
26+
*
27+
* @author Mark Pollack
28+
* @author Christian Tzolov
29+
* @author Hyunjoon Choi
30+
*/
2331
public class ImageResponse implements ModelResponse<ImageGeneration> {
2432

2533
private final ImageResponseMetadata imageResponseMetadata;
2634

35+
/**
36+
* List of generate images returned by the AI provider.
37+
*/
2738
private final List<ImageGeneration> imageGenerations;
2839

40+
/**
41+
* Construct a new {@link ImageResponse} instance without metadata.
42+
* @param generations the {@link List} of {@link ImageGeneration} returned by the AI
43+
* provider.
44+
*/
2945
public ImageResponse(List<ImageGeneration> generations) {
3046
this(generations, ImageResponseMetadata.NULL);
3147
}
3248

49+
/**
50+
* Construct a new {@link ImageResponse} instance.
51+
* @param generations the {@link List} of {@link ImageGeneration} returned by the AI
52+
* provider.
53+
* @param imageResponseMetadata {@link ImageResponseMetadata} containing information
54+
* about the use of the AI provider's API.
55+
*/
3356
public ImageResponse(List<ImageGeneration> generations, ImageResponseMetadata imageResponseMetadata) {
3457
this.imageResponseMetadata = imageResponseMetadata;
3558
this.imageGenerations = List.copyOf(generations);
3659
}
3760

61+
/**
62+
* The {@link List} of {@link ImageGeneration generated outputs}.
63+
* <p>
64+
* It is a {@link List} of {@link List lists} because the Prompt could request
65+
* multiple output {@link ImageGeneration generations}.
66+
* @return the {@link List} of {@link ImageGeneration generated outputs}.
67+
*/
3868
@Override
39-
public ImageGeneration getResult() {
40-
return imageGenerations.get(0);
69+
public List<ImageGeneration> getResults() {
70+
return imageGenerations;
4171
}
4272

73+
/**
74+
* @return Returns the first {@link ImageGeneration} in the generations list.
75+
*/
4376
@Override
44-
public List<ImageGeneration> getResults() {
45-
return imageGenerations;
77+
public ImageGeneration getResult() {
78+
if (CollectionUtils.isEmpty(this.imageGenerations)) {
79+
return null;
80+
}
81+
return imageGenerations.get(0);
4682
}
4783

84+
/**
85+
* @return Returns {@link ImageResponseMetadata} containing information about the use
86+
* of the AI provider's API.
87+
*/
4888
@Override
4989
public ImageResponseMetadata getMetadata() {
5090
return imageResponseMetadata;
5191
}
5292

5393
@Override
5494
public String toString() {
55-
return "ImageResponse{" + "imageResponseMetadata=" + imageResponseMetadata + ", imageGenerations="
56-
+ imageGenerations + '}';
95+
return "ImageResponse [" + "imageResponseMetadata=" + imageResponseMetadata + ", imageGenerations="
96+
+ imageGenerations + "]";
5797
}
5898

5999
@Override

0 commit comments

Comments
 (0)