CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-dev-langchain4j--langchain4j-vertex-ai

LangChain4j integration for Google Vertex AI models including chat, language, embedding, image, and scoring capabilities

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

examples.mddocs/models/image/

Image Model Examples

Basic Image Generation

import dev.langchain4j.model.vertexai.VertexAiImageModel;
import dev.langchain4j.data.image.Image;
import dev.langchain4j.model.output.Response;

VertexAiImageModel model = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .build();

Response<Image> response = model.generate("A serene mountain landscape at sunset");
Image image = response.content();

// Access image data
byte[] imageData = image.bytes();
String base64Data = image.base64Data();
URI imageUri = image.uri();

Generate Multiple Variations

Response<List<Image>> response = model.generate(
    "A futuristic city with flying cars",
    4  // Generate 4 variations
);

List<Image> images = response.content();
for (int i = 0; i < images.size(); i++) {
    System.out.println("Image " + i + " URI: " + images.get(i).uri());
}

With Generation Parameters

import dev.langchain4j.model.vertexai.VertexAiImageModel.AspectRatio;
import dev.langchain4j.model.vertexai.VertexAiImageModel.MimeType;
import dev.langchain4j.model.vertexai.VertexAiImageModel.PersonGeneration;

VertexAiImageModel model = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .aspectRatio(AspectRatio.LANDSCAPE)          // 16:9 aspect ratio
    .seed(12345L)                                 // Reproducible results
    .guidanceScale(15)                            // Edit strength
    .negativePrompt("blurry, low quality")        // What to avoid
    .mimeType(MimeType.PNG)                       // PNG format
    .personGeneration(PersonGeneration.ALLOW_ADULT)
    .maxRetries(3)
    .build();

Reproducible Generation (Seed)

VertexAiImageModel model = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .seed(42L)  // Same seed = same image for same prompt
    .build();

Response<Image> response1 = model.generate("A red apple");
Response<Image> response2 = model.generate("A red apple");
// response1 and response2 will produce identical images

With Negative Prompt

String prompt = "A professional photograph of a modern office space";
String negativePrompt = "blurry, dark, cluttered, distorted, low quality";

VertexAiImageModel model = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .negativePrompt(negativePrompt)
    .build();

Response<Image> response = model.generate(prompt);

Aspect Ratios

// Square (1:1)
VertexAiImageModel squareModel = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .aspectRatio(AspectRatio.SQUARE)
    .build();

// Portrait (9:16)
VertexAiImageModel portraitModel = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .aspectRatio(AspectRatio.PORTRAIT)
    .build();

// Landscape (16:9)
VertexAiImageModel landscapeModel = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .aspectRatio(AspectRatio.LANDSCAPE)
    .build();

Output Format (PNG vs JPEG)

// PNG (lossless, larger files)
VertexAiImageModel pngModel = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .mimeType(MimeType.PNG)
    .build();

// JPEG (lossy, smaller files)
VertexAiImageModel jpegModel = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .mimeType(MimeType.JPEG)
    .compressionQuality(85)  // 1-100, higher = better quality
    .build();

Local Persistence

import java.nio.file.Path;

VertexAiImageModel model = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .withPersisting()
    .persistTo(Path.of("/path/to/save/images"))
    .build();

Response<Image> response = model.generate("A sunset");
// Image automatically saved to /path/to/save/images/

Cloud Storage Persistence

VertexAiImageModel model = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .persistToCloudStorage("gs://my-bucket/images/")
    .build();

Response<Image> response = model.generate("A mountain");
// Image automatically saved to gs://my-bucket/images/

Edit Image with Prompt

import java.nio.file.Files;
import java.nio.file.Path;

Image sourceImage = Image.fromBytes(
    Files.readAllBytes(Path.of("input.png"))
);

Response<Image> response = model.edit(
    sourceImage,
    "Add a rainbow in the sky"
);

Image editedImage = response.content();

Edit Image with Mask

Image sourceImage = Image.fromBytes(
    Files.readAllBytes(Path.of("input.png"))
);
Image maskImage = Image.fromBytes(
    Files.readAllBytes(Path.of("mask.png"))  // White = edit, Black = preserve
);

Response<Image> response = model.edit(
    sourceImage,
    maskImage,
    "Replace masked area with a lake"
);

Image editedImage = response.content();

Person Generation Policy

// Don't generate people
VertexAiImageModel noPersonModel = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .personGeneration(PersonGeneration.DONT_ALLOW)
    .build();

// Allow adults only
VertexAiImageModel adultModel = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .personGeneration(PersonGeneration.ALLOW_ADULT)
    .build();

Image Styles (Imagen v1 only)

import dev.langchain4j.model.vertexai.VertexAiImageModel.ImageStyle;

VertexAiImageModel model = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@002")  // Imagen v1
    .sampleImageStyle(ImageStyle.WATERCOLOR)
    .build();

Response<Image> response = model.generate("A forest scene");

Guidance Scale (Edit Strength)

// Low guidance (subtle changes)
VertexAiImageModel subtleModel = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .guidanceScale(5)  // 0-9: subtle
    .build();

// High guidance (strong changes)
VertexAiImageModel strongModel = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .guidanceScale(25)  // 21+: strong
    .build();

Language Support

VertexAiImageModel model = VertexAiImageModel.builder()
    .endpoint("https://us-central1-aiplatform.googleapis.com/v1/")
    .project("your-project-id")
    .location("us-central1")
    .publisher("google")
    .modelName("imagegeneration@006")
    .language("es")  // Spanish
    .build();

Response<Image> response = model.generate("Una montaña hermosa");

Save Image to File

Response<Image> response = model.generate("A sunset");
Image image = response.content();

// Save to file
Files.write(Path.of("generated-image.png"), image.bytes());

Error Handling

try {
    Response<Image> response = model.generate("Some prompt");
    Image image = response.content();
} catch (Exception e) {
    System.err.println("Image generation failed: " + e.getMessage());
    // Model automatically retries transient errors up to maxRetries
}

Prompt Best Practices

// Good: Descriptive and specific
String goodPrompt = "A professional photograph of a modern office space, " +
                    "natural lighting, minimalist design, wide angle, " +
                    "high detail, 4k quality";

// Include style, lighting, composition
String detailedPrompt = "Digital art, cyberpunk city at night, " +
                        "neon lights, rain-soaked streets, " +
                        "cinematic composition, vibrant colors";

// Use negative prompt for quality
String negativePrompt = "blurry, distorted, low quality, watermark, " +
                        "text, signature, deformed";

Install with Tessl CLI

npx tessl i tessl/maven-dev-langchain4j--langchain4j-vertex-ai@1.11.0

docs

index.md

quick-reference.md

tile.json