Skip to content

Commit ef97e21

Browse files
committed
[Vertex AI] Add documentation for Imagen symbols (#14411)
1 parent 368b0d0 commit ef97e21

12 files changed

+251
-3
lines changed

FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenAspectRatio.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,45 @@
1414

1515
import Foundation
1616

17+
/// An aspect ratio for images generated by Imagen.
18+
///
19+
/// To specify an aspect ratio for generated images, set ``ImagenGenerationConfig/aspectRatio`` in
20+
/// your ``ImagenGenerationConfig``. See the [Cloud
21+
/// documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/image/generate-images#aspect-ratio)
22+
/// for more details and examples of the supported aspect ratios.
1723
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1824
public struct ImagenAspectRatio {
25+
/// Square (1:1) aspect ratio.
26+
///
27+
/// Common uses for this aspect ratio include social media posts.
1928
public static let square1x1 = ImagenAspectRatio(kind: .square1x1)
2029

30+
/// Portrait widescreen (9:16) aspect ratio.
31+
///
32+
/// This is the ``landscape16x9`` aspect ratio rotated 90 degrees. This a relatively new aspect
33+
/// ratio that has been popularized by short form video apps (for example, YouTube shorts). Use
34+
/// this for tall objects with strong vertical orientations such as buildings, trees, waterfalls,
35+
/// or other similar objects.
2136
public static let portrait9x16 = ImagenAspectRatio(kind: .portrait9x16)
2237

38+
/// Widescreen (16:9) aspect ratio.
39+
///
40+
/// This ratio has replaced ``landscape4x3`` as the most common aspect ratio for TVs, monitors,
41+
/// and mobile phone screens (landscape). Use this aspect ratio when you want to capture more of
42+
/// the background (for example, scenic landscapes).
2343
public static let landscape16x9 = ImagenAspectRatio(kind: .landscape16x9)
2444

45+
/// Portrait full screen (3:4) aspect ratio.
46+
///
47+
/// This is the ``landscape4x3`` aspect ratio rotated 90 degrees. This lets to capture more of
48+
/// the scene vertically compared to the ``square1x1`` aspect ratio.
2549
public static let portrait3x4 = ImagenAspectRatio(kind: .portrait3x4)
2650

51+
/// Fullscreen (4:3) aspect ratio.
52+
///
53+
/// This aspect ratio is commonly used in media or film. It is also the dimensions of most old
54+
/// (non-widescreen) TVs and medium format cameras. It captures more of the scene horizontally
55+
/// (compared to ``square1x1``), making it a preferred aspect ratio for photography.
2756
public static let landscape4x3 = ImagenAspectRatio(kind: .landscape4x3)
2857

2958
let rawValue: String

FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenGCSImage.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@
1414

1515
import Foundation
1616

17+
/// An image generated by Imagen, stored in Cloud Storage (GCS) for Firebase.
1718
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1819
public struct ImagenGCSImage {
20+
/// The IANA standard MIME type of the image file; either `"image/png"` or `"image/jpeg"`.
21+
///
22+
/// > Note: To request a different format, set ``ImagenGenerationConfig/imageFormat`` in
23+
/// your ``ImagenGenerationConfig``.
1924
public let mimeType: String
25+
26+
/// The URI of the file in Cloud Storage (GCS) for Firebase.
27+
///
28+
/// This is a `"gs://"`-prefixed URI , for example, `"gs://bucket-name/path/sample_0.jpg"`.
2029
public let gcsURI: String
2130

2231
init(mimeType: String, gcsURI: String) {

FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenGenerationConfig.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,60 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
/// Configuration options for generating images with Imagen.
1516
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1617
public struct ImagenGenerationConfig {
18+
/// Specifies elements to exclude from the generated image.
19+
///
20+
/// Defaults to `nil`, which disables negative prompting. Use a comma-separated list to describe
21+
/// unwanted elements or characteristics. See the [Cloud
22+
/// documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/image/generate-images#negative-prompt)
23+
/// for more details.
24+
///
25+
/// > Important: Support for negative prompts depends on the Imagen model.
1726
public var negativePrompt: String?
27+
28+
/// The number of image samples to generate; defaults to 1 if not specified.
29+
///
30+
/// > Important: The number of sample images that may be generated in each request depends on the
31+
/// model (typically up to 4); see the
32+
/// [`sampleCount`](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/imagen-api#parameter_list)
33+
/// documentation for more details.
1834
public var numberOfImages: Int?
35+
36+
/// The aspect ratio of generated images.
37+
///
38+
/// Defaults to to square, 1:1. Supported aspect ratios depend on the model; see
39+
/// ``ImagenAspectRatio`` for more details.
1940
public var aspectRatio: ImagenAspectRatio?
41+
42+
/// The image format of generated images.
43+
///
44+
/// Defaults to PNG. See ``ImagenImageFormat`` for more details.
2045
public var imageFormat: ImagenImageFormat?
46+
47+
/// Whether to add an invisible watermark to generated images.
48+
///
49+
/// If `true`, an invisible SynthID watermark is embedded in generated images to indicate that
50+
/// they are AI generated; `false` disables watermarking.
51+
///
52+
/// > Important: The default value depends on the model; see the
53+
/// [`addWatermark`](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/imagen-api#parameter_list)
54+
/// documentation for model-specific details.
2155
public var addWatermark: Bool?
2256

57+
/// Initializes configuration options for generating images with Imagen.
58+
///
59+
/// - Parameters:
60+
/// - negativePrompt: Specifies elements to exclude from the generated image; disabled if not
61+
/// specified. See ``negativePrompt``.
62+
/// - numberOfImages: The number of image samples to generate; defaults to 1 if not specified.
63+
/// See ``numberOfImages``.
64+
/// - aspectRatio: The aspect ratio of generated images; defaults to to square, 1:1. See
65+
/// ``aspectRatio``.
66+
/// - imageFormat: The image format of generated images; defaults to PNG. See ``imageFormat``.
67+
/// - addWatermark: Whether to add an invisible watermark to generated images; the default value
68+
/// depends on the model. See ``addWatermark``.
2369
public init(negativePrompt: String? = nil, numberOfImages: Int? = nil,
2470
aspectRatio: ImagenAspectRatio? = nil, imageFormat: ImagenImageFormat? = nil,
2571
addWatermark: Bool? = nil) {

FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenGenerationResponse.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,29 @@
1414

1515
import Foundation
1616

17+
/// A response from a request to generate images with Imagen.
18+
///
19+
/// The type placeholder `T` is an image type of either ``ImagenInlineImage`` or ``ImagenGCSImage``.
20+
///
21+
/// This type is returned from:
22+
/// - ``ImagenModel/generateImages(prompt:)`` where `T` is ``ImagenInlineImage``
23+
/// - ``ImagenModel/generateImages(prompt:gcsURI:)`` where `T` is ``ImagenGCSImage``
1724
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1825
public struct ImagenGenerationResponse<T> {
26+
/// The images generated by Imagen; see ``ImagenInlineImage`` and ``ImagenGCSImage``.
27+
///
28+
/// > Important: The number of images generated may be fewer than the number requested if one or
29+
/// more were filtered out; see ``filteredReason``.
1930
public let images: [T]
31+
32+
/// The reason, if any, that generated images were filtered out.
33+
///
34+
/// This property will only be populated if fewer images were generated than were requested,
35+
/// otherwise it will be `nil`. Images may be filtered out due to the ``ImagenSafetyFilterLevel``,
36+
/// the ``ImagenPersonFilterLevel``, or filtering included in the model. The filter levels may be
37+
/// adjusted in your ``ImagenSafetySettings``. See the [Responsible AI and usage guidelines for
38+
/// Imagen](https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen)
39+
/// for more details.
2040
public let filteredReason: String?
2141
}
2242

FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenImageFormat.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,36 @@
1414

1515
import Foundation
1616

17+
/// An image format for images generated by Imagen.
18+
///
19+
/// To specify an image format for generated images, set ``ImagenGenerationConfig/imageFormat`` in
20+
/// your ``ImagenGenerationConfig``. See the [Cloud
21+
/// documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/imagen-api#output-options)
22+
/// for more details.
1723
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1824
public struct ImagenImageFormat {
1925
let mimeType: String
2026
let compressionQuality: Int?
2127

28+
/// PNG image format.
29+
///
30+
/// Portable Network Graphic (PNG) is a lossless image format, meaning no image data is lost
31+
/// during compression. Images in PNG format are *typically* larger than JPEG images, though this
32+
/// depends on the image content and JPEG compression quality.
2233
public static func png() -> ImagenImageFormat {
2334
return ImagenImageFormat(mimeType: "image/png", compressionQuality: nil)
2435
}
2536

37+
/// JPEG image format.
38+
///
39+
/// Joint Photographic Experts Group (JPEG) is a lossy compression format, meaning some image data
40+
/// is discarded during compression. Images in JPEG format are *typically* larger than PNG images,
41+
/// though this depends on the image content and JPEG compression quality.
42+
///
43+
/// - Parameters:
44+
/// - compressionQuality: The JPEG quality setting from 0 to 100, where `0` is highest level of
45+
/// compression (lowest image quality, smallest file size) and `100` is the lowest level of
46+
/// compression (highest image quality, largest file size); defaults to `75`.
2647
public static func jpeg(compressionQuality: Int? = nil) -> ImagenImageFormat {
2748
return ImagenImageFormat(mimeType: "image/jpeg", compressionQuality: compressionQuality)
2849
}

FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenInlineImage.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@
1414

1515
import Foundation
1616

17+
/// An image generated by Imagen, represented as inline data.
1718
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1819
public struct ImagenInlineImage {
20+
/// The IANA standard MIME type of the image file; either `"image/png"` or `"image/jpeg"`.
21+
///
22+
/// > Note: To request a different format, set ``ImagenGenerationConfig/imageFormat`` in
23+
/// your ``ImagenGenerationConfig``.
1924
public let mimeType: String
25+
26+
/// The image data in PNG or JPEG format.
2027
public let data: Data
2128

2229
init(mimeType: String, bytesBase64Encoded: String) {

FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenModel.swift

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ import FirebaseAppCheckInterop
1616
import FirebaseAuthInterop
1717
import Foundation
1818

19+
/// Represents a remote Imagen model with the ability to generate images using text prompts.
20+
///
21+
/// See the [Cloud
22+
/// documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/image/generate-images) for
23+
/// more details about the image generation capabilities offered by the Imagen model.
24+
///
25+
/// > Warning: For Vertex AI in Firebase, image generation using Imagen 3 models is in Public
26+
/// Preview, which means that the feature is not subject to any SLA or deprecation policy and
27+
/// could change in backwards-incompatible ways.
1928
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
2029
public final class ImagenModel {
2130
/// The resource name of the model in the backend; has the format "models/model-name".
@@ -53,6 +62,20 @@ public final class ImagenModel {
5362
self.requestOptions = requestOptions
5463
}
5564

65+
/// **[Public Preview]** Generates images using the Imagen model and returns them as inline data.
66+
///
67+
/// The individual ``ImagenInlineImage/data`` is provided for each of the generated
68+
/// ``ImagenGenerationResponse/images``.
69+
///
70+
/// > Note: By default, 1 image sample is generated; see ``ImagenGenerationConfig/numberOfImages``
71+
/// to configure the number of images that are generated.
72+
///
73+
/// > Warning: For Vertex AI in Firebase, image generation using Imagen 3 models is in Public
74+
/// Preview, which means that the feature is not subject to any SLA or deprecation policy and
75+
/// could change in backwards-incompatible ways.
76+
///
77+
/// - Parameters:
78+
/// - prompt: A text prompt describing the image(s) to generate.
5679
public func generateImages(prompt: String) async throws
5780
-> ImagenGenerationResponse<ImagenInlineImage> {
5881
return try await generateImages(
@@ -65,12 +88,34 @@ public final class ImagenModel {
6588
)
6689
}
6790

68-
public func generateImages(prompt: String, gcsUri: String) async throws
91+
/// **[Public Preview]** Generates images using the Imagen model and stores them in Cloud Storage
92+
/// (GCS) for Firebase.
93+
///
94+
/// The generated images are stored in a subdirectory of the requested `gcsURI`, named as a random
95+
/// numeric hash. For example, for the `gcsURI` `"gs://bucket-name/path/"`, the generated images
96+
/// are stored in `"gs://bucket-name/path/1234567890123/"` with the names `sample_0.png`,
97+
/// `sample_1.png`, `sample_2.png`, ..., `sample_N.png`. In this example, `1234567890123` is the
98+
/// hash value and `N` is the number of images that were generated, up to the number requested in
99+
/// ``ImagenGenerationConfig/numberOfImages``. The individual ``ImagenGCSImage/gcsURI`` is
100+
/// provided for each of the generated ``ImagenGenerationResponse/images``.
101+
///
102+
/// > Note: By default, 1 image sample is generated; see ``ImagenGenerationConfig/numberOfImages``
103+
/// to configure the number of images that are generated.
104+
///
105+
/// > Warning: For Vertex AI in Firebase, image generation using Imagen 3 models is in Public
106+
/// Preview, which means that the feature is not subject to any SLA or deprecation policy and
107+
/// could change in backwards-incompatible ways.
108+
///
109+
/// - Parameters:
110+
/// - prompt: A text prompt describing the image(s) to generate.
111+
/// - gcsURI: The Cloud Storage (GCS) for Firebase URI where the generated images are stored.
112+
/// This is a `"gs://"`-prefixed URI , for example, `"gs://bucket-name/path/"`.
113+
public func generateImages(prompt: String, gcsURI: String) async throws
69114
-> ImagenGenerationResponse<ImagenGCSImage> {
70115
return try await generateImages(
71116
prompt: prompt,
72117
parameters: ImagenModel.imageGenerationParameters(
73-
storageURI: gcsUri,
118+
storageURI: gcsURI,
74119
generationConfig: generationConfig,
75120
safetySettings: safetySettings
76121
)

FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenPersonFilterLevel.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
/// A filter level controlling whether generation of images containing people or faces is allowed.
16+
///
17+
/// See the
18+
/// [`personGeneration`](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/imagen-api#parameter_list)
19+
/// documentation for more details.
1520
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1621
public struct ImagenPersonFilterLevel: ProtoEnum {
1722
enum Kind: String {
@@ -20,8 +25,23 @@ public struct ImagenPersonFilterLevel: ProtoEnum {
2025
case allowAll = "allow_all"
2126
}
2227

28+
/// Disallow generation of images containing people or faces; images of people are filtered out.
2329
public static let blockAll = ImagenPersonFilterLevel(kind: .blockAll)
30+
31+
/// Allow generation of images containing adults only; images of children are filtered out.
32+
///
33+
/// > Important: Generation of images containing people or faces may require your use case to be
34+
/// reviewed and approved by Cloud support; see the [Responsible AI and usage
35+
/// guidelines](https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen#person-face-gen)
36+
/// for more details.
2437
public static let allowAdult = ImagenPersonFilterLevel(kind: .allowAdult)
38+
39+
/// Allow generation of images containing people of all ages.
40+
///
41+
/// > Important: Generation of images containing people or faces may require your use case to be
42+
/// reviewed and approved; see the [Responsible AI and usage
43+
/// guidelines](https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen#person-face-gen)
44+
/// for more details.
2545
public static let allowAll = ImagenPersonFilterLevel(kind: .allowAll)
2646

2747
let rawValue: String

FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenSafetyFilterLevel.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
/// A filter level controlling how aggressively to filter sensitive content.
16+
///
17+
/// Text prompts provided as inputs and images (generated or uploaded) through Imagen on Vertex AI
18+
/// are assessed against a list of safety filters, which include 'harmful categories' (for example,
19+
/// `violence`, `sexual`, `derogatory`, and `toxic`). This filter level controls how aggressively to
20+
/// filter out potentially harmful content from responses. See the
21+
/// [`safetySetting`](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/imagen-api#parameter_list)
22+
/// documentation and the [Responsible AI and usage
23+
/// guidelines](https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen#safety-filters)
24+
/// for more details.
1525
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1626
public struct ImagenSafetyFilterLevel: ProtoEnum {
1727
enum Kind: String {
@@ -21,9 +31,21 @@ public struct ImagenSafetyFilterLevel: ProtoEnum {
2131
case blockNone = "block_none"
2232
}
2333

34+
/// The most aggressive filtering level; most strict blocking.
2435
public static let blockLowAndAbove = ImagenSafetyFilterLevel(kind: .blockLowAndAbove)
36+
37+
/// Blocks some problematic prompts and responses.
2538
public static let blockMediumAndAbove = ImagenSafetyFilterLevel(kind: .blockMediumAndAbove)
39+
40+
/// Reduces the number of requests blocked due to safety filters.
41+
///
42+
/// > Important: This may increase objectionable content generated by Imagen.
2643
public static let blockOnlyHigh = ImagenSafetyFilterLevel(kind: .blockOnlyHigh)
44+
45+
/// The least aggressive filtering level; blocks very few problematic prompts and responses.
46+
///
47+
/// > Important: Access to this feature is restricted and may require your use case to be reviewed
48+
/// and approved by Cloud support.
2749
public static let blockNone = ImagenSafetyFilterLevel(kind: .blockNone)
2850

2951
let rawValue: String

FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenSafetySettings.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,23 @@
1414

1515
import Foundation
1616

17+
/// Settings for controlling the aggressiveness of filtering out sensitive content.
18+
///
19+
/// See the [Responsible AI and usage
20+
/// guidelines](https://cloud.google.com/vertex-ai/generative-ai/docs/image/responsible-ai-imagen#config-safety-filters)
21+
/// for more details.
1722
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1823
public struct ImagenSafetySettings {
1924
let safetyFilterLevel: ImagenSafetyFilterLevel?
2025
let personFilterLevel: ImagenPersonFilterLevel?
2126

27+
/// Initializes safety settings for the Imagen model.
28+
///
29+
/// - Parameters:
30+
/// - safetyFilterLevel: A filter level controlling how aggressively to filter out sensitive
31+
/// content from generated images.
32+
/// - personFilterLevel: A filter level controlling whether generation of images containing
33+
/// people or faces is allowed.
2234
public init(safetyFilterLevel: ImagenSafetyFilterLevel? = nil,
2335
personFilterLevel: ImagenPersonFilterLevel? = nil) {
2436
self.safetyFilterLevel = safetyFilterLevel

0 commit comments

Comments
 (0)