Spring / Spring AI interview questions
What is image generation in Spring AI and how do you use ImageModel?
Spring AI provides an ImageModel abstraction for generating images from text descriptions (text-to-image). Providers that support it include OpenAI (DALL-E 2, DALL-E 3), Azure OpenAI, and Google Vertex AI (Imagen). The interface is separate from ChatModel because image generation has a fundamentally different request/response shape.
@Service
public class ImageService {
private final ImageModel imageModel;
public ImageService(ImageModel imageModel) {
this.imageModel = imageModel;
}
public String generateImageUrl(String description) {
ImageResponse response = imageModel.call(
new ImagePrompt(description,
OpenAiImageOptions.builder()
.withQuality("hd")
.withN(1)
.withWidth(1024)
.withHeight(1024)
.build())
);
return response.getResult().getOutput().getUrl();
}
}The ImagePrompt wraps the textual description and optional ImageOptions that control quality, size, number of images, and style. The ImageResponse contains a list of ImageGeneration objects, each of which holds either a URL to the generated image (which expires after a provider-defined TTL) or a base64-encoded data URI, depending on the options you specify.
For DALL-E 3 you can also specify a revised_prompt response field — the model rewrites your prompt internally and returns both the original and the revised version it actually used.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
