Spring AI ImageModel (Text-to-Image)
๐ผ๏ธ Spring AI: Fun with Image Generationโ
๐จ LLMs + AI = Magic Images! ๐งโโ๏ธโ
Ever wanted to turn your wildest imaginations into reality? Well, sort of! Large Language Models (LLMs) like DALL-E, Stable Diffusion, Midjourney, Imagen, GauGAN, Pixray, and more can generate stunning images from just a simple text prompt. ๐๏ธโจ
Lucky for us, Spring AI makes it super easy to integrate these magical capabilities into our applications. ๐
๐ Supported Image Generation Wizardsโ
- DALL-E (by OpenAI)
- DALL-E (by Azure OpenAI)
- Stable Diffusion (by Stability AI)
- CogView (by ZhiPuAI & QianFan)
This guide will walk you through the Spring AI module for text-to-image generation, with real code examples! ๐ญ
๐ ๏ธ Step 1: Add Dependencies (Because Magic Needs Tools!)โ
To enable OpenAI API support, add the following to your pom.xml
:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
For Stability AIโs API support, add:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-stability-ai-spring-boot-starter</artifactId>
</dependency>
By default, these dependencies automatically configure an imageClient
bean. Want to disable it? Just set the following in application.properties
:
spring.ai.openai.image.enabled=false
spring.ai.stabilityai.image.enabled=false
๐ญ Step 2: Meet the Image Generation APIโ
Spring AI provides a bunch of cool classes and interfaces to make image generation a breeze. Hereโs the A-list:
Core Class | What It Does |
---|---|
ImageModel | The main actor! Calls imagePrompt and returns ImageResponse . |
ImageMessage | Holds the text and its importance in shaping the image. ๐ญ |
ImagePrompt | A fancy box containing ImageMessage objects and request options. ๐ |
ImageOptions | Specifies extra magic (resolution, quality, etc.). ๐จ |
ImageResponse | The AI's response, containing one or more generated images. ๐ผ๏ธ |
ImageGeneration | The actual image output and related metadata. ๐ธ |
ImageGenerationMetadata | Metadata describing the generated image. ๐ท๏ธ |
โจ How to Cast an Image Spellโ
@RestController
public class OpenAiImageController {
private final OpenAiImageModel imageModel;
@Autowired
public OpenAiImageController(OpenAiImageModel imageModel) {
this.imageModel = imageModel;
}
@GetMapping("/ask/image")
public ImageResponse generateImage(@RequestParam(value = "message", defaultValue = "Horse running behind tiger") String messgae) {
return imageModel.call(
new ImagePrompt(messgae,
OpenAiImageOptions.builder()
.quality("hd")
.N(4)
.height(1024)
.width(1024).build())
);
}
}
Boom! ๐ฑ๐จ๐ญ
๐ง Step 3: Creating the ImageModel
โ
Spring AI provides two classes that implement the ImageModel
interface:
OpenAiImageModel
(for DALL-E ๐ง ๐จ)StabilityAiImageModel
(for Stable Diffusion ๐)
Define an ImageModel
bean in your config file:
๐ข Pro Tip: Keep your API keys out of your code! Store them safely in application.properties
:
spring.ai.openai.api-key=${OPENAI_API_KEY}
# OR
spring.ai.stability.api-key=${STABILITY_API_KEY}
๐ฎ Step 4: Creating the Image Generation Controllerโ
Time to roll up our sleeves and make some API magic! Letโs create a simple REST controller to generate images on demand. ๐งโ๐ป
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OpenAiImageController {
private final OpenAiImageModel imageModel;
@Autowired
public OpenAiImageController(OpenAiImageModel imageModel) {
this.imageModel = imageModel;
}
@GetMapping("/ask/image")
public ImageResponse generateImage(@RequestParam(value = "message", defaultValue = "Horse running behind tiger") String messgae) {
return imageModel.call(
new ImagePrompt(messgae,
OpenAiImageOptions.builder()
.quality("hd")
.N(4)
.height(1024)
.width(1024).build())
);
}
@GetMapping("/ask/image/withUrl")
public String generateImageUrl(@RequestParam(value = "message", defaultValue = "Horse running behind tiger") String messgae) {
ImageResponse imageResponse = imageModel.call(new ImagePrompt(messgae, OpenAiImageOptions.builder().quality("hd").N(4).height(1024).width(1024).build()));
String imageUrl = imageResponse.getResult().getOutput().getUrl();
return "Redirect to:" + imageUrl;
}
}
๐ This same code works for Stability AI tooโjust swap in StabilityAiImageModel
. ๐
๐ Step 5: Configuring the Base URLโ
By default, Spring AI selects these URLs for image generation:
spring.ai.openai.image.base-url=api.openai.com
# OR
spring.ai.stabilityai.image.base-url=api.stability.ai/v1
Want to use a custom endpoint? Just override these properties. Easy peasy! ๐
๐ ๏ธ Step 6: Testing Your Image Generation API ๐ฏโ
Letโs test it! Send a prompt like:
GET /ask/image/withUrl?message=Horse runing behind Tiger
๐ Youโll get back a URL. Copy and paste it into your browser to see the generated image! ๐
๐ฌ Conclusion: AI Art, Made Easy! ๐โ
With Spring AI, turning text into images is ridiculously easy and fun! ๐คฉ We covered:
โ
Adding dependencies ๐๏ธ
โ
Setting up image generation APIs ๐ผ๏ธ
โ
Configuring ImageModel
๐จ
โ
Creating a REST Controller ๐
โ
Generating images with a simple API call! ๐ญ
Now go aheadโexperiment, explore, and bring your AI-powered artistic visions to life! โจ
Happy Coding & Image Generating! ๐๐จ