From 7b2798026f673c2822683a84d6ea9e38810acd35 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 24 Mar 2025 20:12:30 +0000 Subject: [PATCH 1/5] Log warning for unsupported model names in GenerativeModel and ImagenModel initializers. This change adds a warning message to the initializers of GenerativeModel and ImagenModel. The warning is logged when the provided model name does not start with the expected prefix ("gemini-" for GenerativeModel and "imagen-" for ImagenModel). The warning message includes a link to the documentation for supported models. The changes were tested manually, but automated tests could not be run due to an unavailable test environment. --- FirebaseVertexAI/Sources/GenerativeModel.swift | 7 +++++++ .../Sources/Types/Public/Imagen/ImagenModel.swift | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/FirebaseVertexAI/Sources/GenerativeModel.swift b/FirebaseVertexAI/Sources/GenerativeModel.swift index 3d37be52061..71423e225d3 100644 --- a/FirebaseVertexAI/Sources/GenerativeModel.swift +++ b/FirebaseVertexAI/Sources/GenerativeModel.swift @@ -90,6 +90,13 @@ public final class GenerativeModel: Sendable { } self.requestOptions = requestOptions + if !name.starts(with: "gemini-") { + VertexLog.warn(code: .unsupportedModelName, """ + Unsupported Gemini model "\(name)"; see \ + https://firebase.google.com/docs/vertex-ai/models for a list supported Gemini model names. + """) + } + if VertexLog.additionalLoggingEnabled() { VertexLog.debug(code: .verboseLoggingEnabled, "Verbose logging enabled.") } else { diff --git a/FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenModel.swift b/FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenModel.swift index f0224ec8fdb..c355945fb54 100644 --- a/FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenModel.swift +++ b/FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenModel.swift @@ -60,6 +60,13 @@ public final class ImagenModel { self.generationConfig = generationConfig self.safetySettings = safetySettings self.requestOptions = requestOptions + + if !name.starts(with: "imagen-") { + VertexLog.warn(code: .unsupportedModelName, """ + Unsupported Imagen model "\(name)"; see \ + https://firebase.google.com/docs/vertex-ai/models for a list supported Imagen model names. + """) + } } /// **[Public Preview]** Generates images using the Imagen model and returns them as inline data. From 5e171904dbb8ef807a93fa576b6426b2c62d803e Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Mon, 24 Mar 2025 16:27:12 -0400 Subject: [PATCH 2/5] Add `VertexLog` message codes for these warnings --- FirebaseVertexAI/Sources/VertexLog.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/FirebaseVertexAI/Sources/VertexLog.swift b/FirebaseVertexAI/Sources/VertexLog.swift index 9332aa87961..cd115e5a84f 100644 --- a/FirebaseVertexAI/Sources/VertexLog.swift +++ b/FirebaseVertexAI/Sources/VertexLog.swift @@ -33,9 +33,11 @@ enum VertexLog { // Generative Model Configuration case generativeModelInitialized = 1000 + case unsupportedGeminiModel = 1001 // Imagen Model Configuration case imagenInvalidJPEGCompressionQuality = 1201 + case unsupportedImagenModel = 1200 // Network Errors case generativeAIServiceNonHTTPResponse = 2000 From 46865e24f9040c1f89822ceaf54ed52e16f315bd Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Mon, 24 Mar 2025 16:28:03 -0400 Subject: [PATCH 3/5] Fix logging code and add static constants --- FirebaseVertexAI/Sources/GenerativeModel.swift | 17 ++++++++++------- .../Types/Public/Imagen/ImagenModel.swift | 17 ++++++++++------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/FirebaseVertexAI/Sources/GenerativeModel.swift b/FirebaseVertexAI/Sources/GenerativeModel.swift index 71423e225d3..6df2620bc7f 100644 --- a/FirebaseVertexAI/Sources/GenerativeModel.swift +++ b/FirebaseVertexAI/Sources/GenerativeModel.swift @@ -20,6 +20,9 @@ import Foundation /// content based on various input types. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) public final class GenerativeModel: Sendable { + /// Model name prefix to identify Gemini models. + static let geminiModelNamePrefix = "gemini-" + /// The resource name of the model in the backend; has the format "models/model-name". let modelResourceName: String @@ -71,6 +74,13 @@ public final class GenerativeModel: Sendable { systemInstruction: ModelContent? = nil, requestOptions: RequestOptions, urlSession: URLSession = .shared) { + if !name.starts(with: GenerativeModel.geminiModelNamePrefix) { + VertexLog.warning(code: .unsupportedGeminiModel, """ + Unsupported Gemini model "\(name)"; see \ + https://firebase.google.com/docs/vertex-ai/models for a list supported Gemini model names. + """) + } + modelResourceName = name self.apiConfig = apiConfig generativeAIService = GenerativeAIService( @@ -90,13 +100,6 @@ public final class GenerativeModel: Sendable { } self.requestOptions = requestOptions - if !name.starts(with: "gemini-") { - VertexLog.warn(code: .unsupportedModelName, """ - Unsupported Gemini model "\(name)"; see \ - https://firebase.google.com/docs/vertex-ai/models for a list supported Gemini model names. - """) - } - if VertexLog.additionalLoggingEnabled() { VertexLog.debug(code: .verboseLoggingEnabled, "Verbose logging enabled.") } else { diff --git a/FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenModel.swift b/FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenModel.swift index c355945fb54..13b0b0dba35 100644 --- a/FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenModel.swift +++ b/FirebaseVertexAI/Sources/Types/Public/Imagen/ImagenModel.swift @@ -28,6 +28,9 @@ import Foundation /// could change in backwards-incompatible ways. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) public final class ImagenModel { + /// Model name prefix to identify Imagen models. + static let imagenModelNamePrefix = "imagen-" + /// The resource name of the model in the backend; has the format "models/model-name". let modelResourceName: String @@ -51,6 +54,13 @@ public final class ImagenModel { safetySettings: ImagenSafetySettings?, requestOptions: RequestOptions, urlSession: URLSession = .shared) { + if !name.starts(with: ImagenModel.imagenModelNamePrefix) { + VertexLog.warning(code: .unsupportedImagenModel, """ + Unsupported Imagen model "\(name)"; see \ + https://firebase.google.com/docs/vertex-ai/models for a list supported Imagen model names. + """) + } + modelResourceName = name self.apiConfig = apiConfig generativeAIService = GenerativeAIService( @@ -60,13 +70,6 @@ public final class ImagenModel { self.generationConfig = generationConfig self.safetySettings = safetySettings self.requestOptions = requestOptions - - if !name.starts(with: "imagen-") { - VertexLog.warn(code: .unsupportedModelName, """ - Unsupported Imagen model "\(name)"; see \ - https://firebase.google.com/docs/vertex-ai/models for a list supported Imagen model names. - """) - } } /// **[Public Preview]** Generates images using the Imagen model and returns them as inline data. From bd41de72e448c9749b973c01f0ba87ecd8bea9b0 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Mon, 24 Mar 2025 16:36:35 -0400 Subject: [PATCH 4/5] Re-order cases in `VertexLog.MessageCode` --- FirebaseVertexAI/Sources/VertexLog.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FirebaseVertexAI/Sources/VertexLog.swift b/FirebaseVertexAI/Sources/VertexLog.swift index cd115e5a84f..ff94a2d435e 100644 --- a/FirebaseVertexAI/Sources/VertexLog.swift +++ b/FirebaseVertexAI/Sources/VertexLog.swift @@ -36,8 +36,8 @@ enum VertexLog { case unsupportedGeminiModel = 1001 // Imagen Model Configuration - case imagenInvalidJPEGCompressionQuality = 1201 case unsupportedImagenModel = 1200 + case imagenInvalidJPEGCompressionQuality = 1201 // Network Errors case generativeAIServiceNonHTTPResponse = 2000 From bf0b21fb8e105b5fb1de508a6251fb8860cf8bef Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Mon, 24 Mar 2025 16:43:00 -0400 Subject: [PATCH 5/5] Add CHANGELOG entry --- FirebaseVertexAI/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/FirebaseVertexAI/CHANGELOG.md b/FirebaseVertexAI/CHANGELOG.md index 2c84aaaafa3..cd38e2bec67 100644 --- a/FirebaseVertexAI/CHANGELOG.md +++ b/FirebaseVertexAI/CHANGELOG.md @@ -1,3 +1,7 @@ +# 11.11.0 +- [added] Emits a warning when attempting to use an incompatible model with + `GenerativeModel` or `ImagenModel`. (#14610) + # 11.10.0 - [feature] The Vertex AI SDK no longer requires `@preconcurrency` when imported in Swift 6. - [feature] The Vertex AI Sample App now includes an image generation example.