|
| 1 | += Generating Images |
| 2 | + |
| 3 | +include::./includes/attributes.adoc[] |
| 4 | +include::./includes/customization.adoc[] |
| 5 | + |
| 6 | +Text completions are powerful, but many modern AI use cases also demand image generation. |
| 7 | +With Quarkus LangChain4j’s Image model support, you can declare a service method that returns a `dev.langchain4j.data.image.Image` instance, letting your microservice request, receive, and expose AI‐generated visuals seamlessly. |
| 8 | + |
| 9 | +== Prerequisites |
| 10 | + |
| 11 | +* A Quarkus project with the `quarkus-langchain4j-openai` extension on the classpath (or another model provider that supports image models) |
| 12 | +* OpenAI (or another provider) API Key configured in `application.properties` or via `QUARKUS_LANGCHAIN4J_OPENAI_API_KEY` |
| 13 | +* An **Image Model** enabled (e.g. `dall-e-3` or `GPT-Image-1`) in your config: |
| 14 | + |
| 15 | +[source,properties] |
| 16 | +---- |
| 17 | +quarkus.langchain4j.openai.api-key=${OPENAI_API_KEY} |
| 18 | +quarkus.langchain4j.openai.image-model.model-name=dall-e-3 # This is the default model, but you can specify another one if needed |
| 19 | +---- |
| 20 | + |
| 21 | +* Extended timeout for image generation, which can take longer than text completions. For example: |
| 22 | + |
| 23 | +[source,properties] |
| 24 | +---- |
| 25 | +quarkus.langchain4j.openai.timeout=60s |
| 26 | +---- |
| 27 | + |
| 28 | +== Step 1. Define the AI service |
| 29 | + |
| 30 | +Declare an AI Service interface to encapsulate image generation calls: |
| 31 | + |
| 32 | +[source,java] |
| 33 | +---- |
| 34 | +include::{examples-dir}/io/quarkiverse/langchain4j/samples/images/ImageGenerationAiService.java[tags=head] |
| 35 | +---- |
| 36 | + |
| 37 | +Here, `@RegisterAiService` creates the xref:ai-services.adoc[AI Service], and `@SystemMessage` supplies the global instruction for all methods in the service. |
| 38 | + |
| 39 | +== Step 2. Define the Generation Method |
| 40 | + |
| 41 | +Add a method annotated with `@UserMessage` that returns Image. |
| 42 | +Quarkus LangChain4j will call the configured image model and wrap the response in an `Image` object. |
| 43 | + |
| 44 | +[source,java] |
| 45 | +---- |
| 46 | +include::{examples-dir}/io/quarkiverse/langchain4j/samples/images/ImageGenerationAiService.java[tags=head;generation] |
| 47 | +---- |
| 48 | + |
| 49 | +When invoked, Quarkus will substitute the subject into the prompt, send it to the AI model provider, and return the generated image (URL or Base64). |
| 50 | + |
| 51 | +NOTE: When using OpenAI image model, the returned `Image` object contains a URL to the generated image, which you can use to display or download the image. |
| 52 | + |
| 53 | + |
| 54 | +== Step 3. Expose an HTTP Endpoint |
| 55 | + |
| 56 | +Finally, expose a REST resource that injects and calls your AI Service: |
| 57 | + |
| 58 | +Use the `Image` data type for local or in-memory images: |
| 59 | + |
| 60 | +[source,java] |
| 61 | +---- |
| 62 | +include::{examples-dir}/io/quarkiverse/langchain4j/samples/images/EndpointGeneration.java[] |
| 63 | +---- |
| 64 | + |
| 65 | +== Conclusion |
| 66 | + |
| 67 | +You’ve now seen how to: |
| 68 | +1. Register an image‐generation AI service (`@RegisterAiService`). |
| 69 | +2. Declare a method returning `Image` with `@UserMessage`. |
| 70 | +3. Expose a REST endpoint that returns the generated image back to clients. |
| 71 | + |
| 72 | +With this pattern, you can build rich, AI‐driven visual applications—everything from dynamic marketing graphics to in‐app illustration. |
| 73 | + |
0 commit comments