DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Leverage Amazon BedRock Chat Model With Java and Spring AI
  • GenAI: Spring Boot Integration With LocalAI for Code Conversion
  • AI in Java: Building a ChatGPT Clone With Spring Boot and LangChain
  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era

Trending

  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Measuring the Impact of AI on Software Engineering Productivity
  • Start Coding With Google Cloud Workstations
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Using Spring AI to Generate Images With OpenAI's DALL-E 3

Using Spring AI to Generate Images With OpenAI's DALL-E 3

Integrate Spring AI with OpenAI's DALL-E 3 to generate images. Set up Spring Boot, configure the API integration, and customize settings easily.

By 
Danil Temnikov user avatar
Danil Temnikov
DZone Core CORE ·
Jan. 28, 25 · Tutorial
Likes (12)
Comment
Save
Tweet
Share
9.3K Views

Join the DZone community and get the full member experience.

Join For Free

Hi, community!

This is my first article in a series of introductions to Spring AI. Today, we will see how we can easily generate pictures using text prompts. To achieve this, we will leverage the OpenAI API and the DALL-E 3 model.

In this article, I'll skip the explanation of some basic Spring concepts like bean management, starters, etc, as the main goal of this article is to discover Spring AI capabilities. For the same reason, I'll not create detailed instructions on how to generate an OpenAI API key. 

Prerequisite

If you don't have an active OpenAI API key, do the following steps:

  1. Create an account on OpenAI.
  2. Generate the token on the API Keys page.

Step 1: Set Up a Project

To quickly generate a project template with all necessary dependencies, you may use https://start.spring.io/.

In my example, I'll use Java 17 and Spring Boot 3.4.1. We also need to include the following dependencies:

  • Spring WEB: This dependency will allow us to create a web server and expose REST endpoints as entry points to our application
  • OpenAI: This dependency provides us with smooth integration with Open AI just by writing a couple lines of code and a few lines of configurations.

After clicking generate, open downloaded files in the IDE you are working on and validate that all necessary dependencies exist in pom.xml.

XML
 
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>


Step 2: Set Up a Configuration File

As a next step, we need to configure our property file. By default, Spring uses application.yaml or application.properties file. In this example, I'm going to use yaml format. You may reformat the code into .properties if you feel more comfortable working with this format.

Here are all the configs we need to add to the application.yaml file:

YAML
 
spring:
  ai:
    openai:
      api-key: [your OpenAI api key]
    image:
      options:
        model: dall-e-3
        size: 1024x1024
        style: vivid
        quality: standard
        response-format: url


  • Model: We are going to use the dall-e-3 model as the only available model in Spring AI at the moment of writing this article.
  • Size: Configures the size of the generated image. It must be one of 1024x1024, 1792x1024, or 1024x1792 for dall-e-3 model
  • Style: The vivid style generates more hyper-realistic images. If you want your pictures to look more real, set value natural.
  • Quality: Might be one out of two options: standard or HD.
  • Response-format: Might be one out of two options: url and b64_json. I'll be using the URL for demo purposes and simplicity. The image will be available by URL one hour after generation.

Step 3: Create ImageGenerationService

Let's create a service that will be responsible for generating images.

Java
 
@Service
public class ImageGenerationService {

  @Autowired
  ImageModel imageModel;

}


We created a new class and annotated it as a Service. We also autowired the ImageModel bean.

ImageModel is the main interface used to generate pictures. As we provided all the necessary configurations in Step 2, Spring Boot Starter will automatically generate an implementation of this interface called OpenAiImageModel for us.

When our class is configured, we may start implementing a method that calls an OpenAI API to generate pictures using our prompts. And this is where the real magic of Spring AI will happen. Let's take a look at it.

Java
 
public String generateImage(String prompt) {
  ImagePrompt imagePrompt = new ImagePrompt(prompt);
  ImageResponse imageResponse = imageModel.call(imagePrompt);
  return imageResponse.getResult().getOutput().getUrl();
}


That's it! We just need three lines of code to actually generate an image with Spring AI. Isn't that amazing?

In the first step, we created a new ImagePrompt just by providing a string prompt. Next, we made an API call using imageModel.call(imagePrompt) and stored the response in the ImageResponse variable. In the last step, we returned the URL of the generated image. Remember, the image is only available for one hour; after that, the link will not be available anymore. So don't forget to save your masterpiece!

Step 4: Create ImageGenerationController to Run Our Code

We need to create a last file to allow users to execute our integration. It may look like this:

Java
 
@RestController()
@RequestMapping("/image")
public class ImageGenerationController {

  @Autowired
  ImageGenerationService imageService;

  @GetMapping("/generate")
  public ResponseEntity<String> generateImage(@RequestParam String prompt) {
  	return ResponseEntity.ok(imageService.generateImage(prompt));
  }
}


As you can see, we just created a simple controller with just one GET endpoint inside. This endpoint will be available at localhost:8080/image/generate.

Step 5. Run Our Application

To start our application, we need to run the following command:

Plain Text
 
mvn spring-boot:run


When the application is running, we may check the result by executing the following curl with any prompt you want. I used the following: Cute cat playing chess. Don't forget to add %20 instead of whitespaces if you are using the command line for calling your endpoint:

Shell
 
curl -X GET "http://localhost:8080/image/generate?prompt=Cute%20cat%20playing%20chess"


After executing, wait a few seconds as it takes some time for OpenAI to generate your image and voila:

Generated image

Congratulations! You've just created and tested your first Spring AI application, which generates images using custom prompts!

Step 6: Give More Flexibility in Generating Images (Optional)

In the second step, we configured the default behavior to our model and provided all the necessary configurations in the application.yaml file. But can we give more flexibility to our users and let them provide their configurations? The answer is yes!

To do this, we need to use the ImageOptions interface.

Here is an example:

Java
 
public String generateImage(GenerateImageRequest imageRequest) {

  ImageOptions options = OpenAiImageOptions.builder()
    .withQuality("standard")
    .withStyle("vivid")
    .withHeight(1024)
    .withWidth(1024)
    .withResponseFormat("url")
    .build();

  ImagePrompt imagePrompt = new ImagePrompt(imageRequest.getPrompt(), options);
  ImageResponse imageResponse = imageModel.call(imagePrompt);
  return imageResponse.getResult().getOutput().getUrl();
}


To achieve this, we need to build options programmatically using all the configs we set up in application.yaml and provide these options when creating an ImagePrompt object. You may find more configuration options in Spring AI docs.

Conclusion

Spring AI is a great tool that helps developers smoothly integrate with different AI models. As of writing this article, Spring AI supports five image models, including but not limited to Azure AI and Stability.

I hope you found this article helpful and that it will inspire you to explore Spring AI more deeply.

AI Java (programming language) Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Leverage Amazon BedRock Chat Model With Java and Spring AI
  • GenAI: Spring Boot Integration With LocalAI for Code Conversion
  • AI in Java: Building a ChatGPT Clone With Spring Boot and LangChain
  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!