Skip to content

Commit b5eed8d

Browse files
matthew29tangcopybara-github
authored andcommitted
feat: Support Imagen 4 Ingredients on Vertex
PiperOrigin-RevId: 808749595
1 parent ec323d1 commit b5eed8d

File tree

5 files changed

+241
-5
lines changed

5 files changed

+241
-5
lines changed

examples/src/main/java/com/google/genai/examples/Constants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ private Constants() {}
4040
/** The name of the Imagen model to be used for image editing in the examples. */
4141
public static final String IMAGEN_CAPABILITY_MODEL_NAME = "imagen-3.0-capability-001";
4242

43+
/** The name of the Imagen ingredients model to be used in the examples. */
44+
public static final String IMAGEN_INGREDIENTS_MODEL_NAME = "imagen-4.0-ingredients-preview";
45+
4346
/** The name of the Imagen product recontext model to be used in the examples. */
4447
public static final String IMAGEN_RECONTEXT_MODEL_NAME = "imagen-product-recontext-preview-06-30";
4548

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Usage:
19+
*
20+
* <p>1a. If you are using Vertex AI, setup ADC to get credentials:
21+
* https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp
22+
*
23+
* <p>Then set Project, Location, and USE_VERTEXAI flag as environment variables:
24+
*
25+
* <p>export GOOGLE_CLOUD_PROJECT=YOUR_PROJECT
26+
*
27+
* <p>export GOOGLE_CLOUD_LOCATION=YOUR_LOCATION
28+
*
29+
* <p>export GOOGLE_GENAI_USE_VERTEXAI=true
30+
*
31+
* <p>1b. If you are using Gemini Developer API, set an API key environment variable. You can find a
32+
* list of available API keys here: https://aistudio.google.com/app/apikey
33+
*
34+
* <p>export GOOGLE_API_KEY=YOUR_API_KEY
35+
*
36+
* <p>2. Compile the java package and run the sample code.
37+
*
38+
* <p>mvn clean compile
39+
*
40+
* <p>mvn exec:java -Dexec.mainClass="com.google.genai.examples.EditImageContentReference"
41+
* -Dexec.args="YOUR_MODEL_ID"
42+
*/
43+
package com.google.genai.examples;
44+
45+
import com.google.genai.Client;
46+
import com.google.genai.types.ContentReferenceImage;
47+
import com.google.genai.types.EditImageConfig;
48+
import com.google.genai.types.EditImageResponse;
49+
import com.google.genai.types.Image;
50+
import com.google.genai.types.ReferenceImage;
51+
import com.google.genai.types.StyleReferenceConfig;
52+
import com.google.genai.types.StyleReferenceImage;
53+
import java.util.ArrayList;
54+
55+
/** An example of using the Unified Gen AI Java SDK to edit an image (Mask reference). */
56+
public final class EditImageContentReference {
57+
public static void main(String[] args) {
58+
final String modelId;
59+
if (args.length != 0) {
60+
modelId = args[0];
61+
} else {
62+
modelId = Constants.IMAGEN_INGREDIENTS_MODEL_NAME;
63+
}
64+
65+
// Instantiate the client. The client by default uses the Gemini Developer API. It gets the API
66+
// key from the environment variable `GOOGLE_API_KEY`. Vertex AI API can be used by setting the
67+
// environment variables `GOOGLE_CLOUD_LOCATION` and `GOOGLE_CLOUD_PROJECT`, as well as setting
68+
// `GOOGLE_GENAI_USE_VERTEXAI` to "true".
69+
//
70+
// Note: Some services are only available in a specific API backend (Gemini or Vertex), you will
71+
// get a `UnsupportedOperationException` if you try to use a service that is not available in
72+
// the backend you are using.
73+
Client client = new Client();
74+
75+
if (client.vertexAI()) {
76+
System.out.println("Using Vertex AI");
77+
} else {
78+
System.out.println("Gemini Developer API is not supported for this example.");
79+
System.exit(0);
80+
}
81+
82+
EditImageConfig editImageConfig =
83+
EditImageConfig.builder().numberOfImages(1).outputMimeType("image/jpeg").build();
84+
85+
ArrayList<ReferenceImage> referenceImages = new ArrayList<>();
86+
Image dogImage = Image.builder().gcsUri("gs://genai-sdk-tests/inputs/images/dog.jpg").build();
87+
ContentReferenceImage contentReferenceImage =
88+
ContentReferenceImage.builder().referenceImage(dogImage).referenceId(1).build();
89+
referenceImages.add(contentReferenceImage);
90+
91+
Image cyberpunkImage =
92+
Image.builder().gcsUri("gs://genai-sdk-tests/inputs/images/cyberpunk.jpg").build();
93+
StyleReferenceImage styleReferenceImage =
94+
StyleReferenceImage.builder()
95+
.referenceId(2)
96+
.referenceImage(cyberpunkImage)
97+
.config(StyleReferenceConfig.builder().styleDescription("cyberpunk style").build())
98+
.build();
99+
referenceImages.add(styleReferenceImage);
100+
101+
EditImageResponse editImageResponse =
102+
client.models.editImage(
103+
modelId,
104+
"Dog in [1] sleeping on the ground at the bottom of the image with the cyberpunk city"
105+
+ " landscape in [2] in the background visible on the side of the mug.",
106+
referenceImages,
107+
editImageConfig);
108+
109+
Image editedImage = editImageResponse.generatedImages().get().get(0).image().get();
110+
// Do something with editedImage.
111+
}
112+
113+
private EditImageContentReference() {}
114+
}

src/main/java/com/google/genai/Models.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7967,12 +7967,13 @@ public GenerateImagesResponse generateImages(
79677967
* @param model the name of the GenAI model to use for editing capabilities
79687968
* @param prompt the prompt to edit the image
79697969
* @param referenceImages a {@link List<com.google.genai.types.ReferenceImage>} to send to use for
7970-
* editing. The 5 types of reference images are: {@link
7970+
* editing. The 6 types of reference images are: {@link
79717971
* com.google.genai.types.RawReferenceImage}, {@link
79727972
* com.google.genai.types.MaskReferenceImage}, {@link
79737973
* com.google.genai.types.ControlReferenceImage}, {@link
79747974
* com.google.genai.types.StyleReferenceImage}, {@link
7975-
* com.google.genai.types.SubjectReferenceImage},
7975+
* com.google.genai.types.SubjectReferenceImage}, {@link
7976+
* com.google.genai.types.ContentReferenceImage}
79767977
* @param config a {@link com.google.genai.types.EditImageConfig} instance that specifies the
79777978
* optional configurations
79787979
* @return a {@link com.google.genai.types.EditImageResponse} instance that contains the edited
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// Auto-generated code. Do not edit.
18+
19+
package com.google.genai.types;
20+
21+
import com.fasterxml.jackson.annotation.JsonCreator;
22+
import com.fasterxml.jackson.annotation.JsonProperty;
23+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24+
import com.google.auto.value.AutoValue;
25+
import com.google.genai.JsonSerializable;
26+
import java.util.Optional;
27+
28+
/**
29+
* A content reference image.
30+
*
31+
* <p>A content reference image represents a subject to reference (ex. person, product, animal)
32+
* provided by the user. It can optionally be provided in addition to a style reference image (ex.
33+
* background, style reference).
34+
*/
35+
@AutoValue
36+
@JsonDeserialize(builder = ContentReferenceImage.Builder.class)
37+
public abstract class ContentReferenceImage extends JsonSerializable implements ReferenceImage {
38+
/** The reference image for the editing operation. */
39+
@JsonProperty("referenceImage")
40+
public abstract Optional<Image> referenceImage();
41+
42+
/** The id of the reference image. */
43+
@JsonProperty("referenceId")
44+
public abstract Optional<Integer> referenceId();
45+
46+
/** The type of the reference image. Only set by the SDK. */
47+
@JsonProperty("referenceType")
48+
public abstract Optional<String> referenceType();
49+
50+
/** Instantiates a builder for ContentReferenceImage. */
51+
@ExcludeFromGeneratedCoverageReport
52+
public static Builder builder() {
53+
return new AutoValue_ContentReferenceImage.Builder();
54+
}
55+
56+
/** Creates a builder with the same values as this instance. */
57+
public abstract Builder toBuilder();
58+
59+
/** Builder for ContentReferenceImage. */
60+
@AutoValue.Builder
61+
public abstract static class Builder {
62+
/** For internal usage. Please use `ContentReferenceImage.builder()` for instantiation. */
63+
@JsonCreator
64+
private static Builder create() {
65+
return new AutoValue_ContentReferenceImage.Builder();
66+
}
67+
68+
/**
69+
* Setter for referenceImage.
70+
*
71+
* <p>referenceImage: The reference image for the editing operation.
72+
*/
73+
@JsonProperty("referenceImage")
74+
public abstract Builder referenceImage(Image referenceImage);
75+
76+
/**
77+
* Setter for referenceImage builder.
78+
*
79+
* <p>referenceImage: The reference image for the editing operation.
80+
*/
81+
public Builder referenceImage(Image.Builder referenceImageBuilder) {
82+
return referenceImage(referenceImageBuilder.build());
83+
}
84+
85+
/**
86+
* Setter for referenceId.
87+
*
88+
* <p>referenceId: The id of the reference image.
89+
*/
90+
@JsonProperty("referenceId")
91+
public abstract Builder referenceId(Integer referenceId);
92+
93+
/**
94+
* Setter for referenceType.
95+
*
96+
* <p>referenceType: The type of the reference image. Only set by the SDK.
97+
*/
98+
@JsonProperty("referenceType")
99+
public abstract Builder referenceType(String referenceType);
100+
101+
public abstract ContentReferenceImage build();
102+
}
103+
104+
/** Deserializes a JSON string to a ContentReferenceImage object. */
105+
@ExcludeFromGeneratedCoverageReport
106+
public static ContentReferenceImage fromJson(String jsonString) {
107+
return JsonSerializable.fromJsonString(jsonString, ContentReferenceImage.class);
108+
}
109+
110+
@Override
111+
public ReferenceImageAPI toReferenceImageAPI() {
112+
ReferenceImageAPI.Builder referenceImageAPIBuilder = ReferenceImageAPI.builder();
113+
referenceImage().ifPresent(referenceImageAPIBuilder::referenceImage);
114+
referenceId().ifPresent(referenceImageAPIBuilder::referenceId);
115+
referenceImageAPIBuilder.referenceType("REFERENCE_TYPE_CONTENT");
116+
return referenceImageAPIBuilder.build();
117+
}
118+
}

src/main/java/com/google/genai/types/EditImageParameters.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public abstract class EditImageParameters extends JsonSerializable {
4141
@JsonProperty("prompt")
4242
public abstract Optional<String> prompt();
4343

44-
/** The reference images for Imagen 3 editing. */
44+
/** The reference images for editing. */
4545
@JsonProperty("referenceImages")
4646
public abstract Optional<List<ReferenceImageAPI>> referenceImages();
4747

@@ -86,15 +86,15 @@ private static Builder create() {
8686
/**
8787
* Setter for referenceImages.
8888
*
89-
* <p>referenceImages: The reference images for Imagen 3 editing.
89+
* <p>referenceImages: The reference images for editing.
9090
*/
9191
@JsonProperty("referenceImages")
9292
public abstract Builder referenceImages(List<ReferenceImageAPI> referenceImages);
9393

9494
/**
9595
* Setter for referenceImages.
9696
*
97-
* <p>referenceImages: The reference images for Imagen 3 editing.
97+
* <p>referenceImages: The reference images for editing.
9898
*/
9999
public Builder referenceImages(ReferenceImageAPI... referenceImages) {
100100
return referenceImages(Arrays.asList(referenceImages));

0 commit comments

Comments
 (0)