From 97f7f1d715b87089a3cde4ed57f3c2b705de045a Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Fri, 22 Nov 2024 12:45:04 -0500 Subject: [PATCH 1/3] [Vertex AI] Add compilation test for GCS snippets --- .../Unit/Snippets/CloudStorageSnippets.swift | 63 +++++++++++++++++++ Package.swift | 5 +- 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 FirebaseVertexAI/Tests/Unit/Snippets/CloudStorageSnippets.swift diff --git a/FirebaseVertexAI/Tests/Unit/Snippets/CloudStorageSnippets.swift b/FirebaseVertexAI/Tests/Unit/Snippets/CloudStorageSnippets.swift new file mode 100644 index 00000000000..98b1f853998 --- /dev/null +++ b/FirebaseVertexAI/Tests/Unit/Snippets/CloudStorageSnippets.swift @@ -0,0 +1,63 @@ +// Copyright 2024 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import FirebaseCore +import FirebaseStorage +import FirebaseVertexAI + +// These CloudStorageSnippets are not currently runnable due to the GCS upload paths but are used as +// compilation tests. +@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) +final class CloudStorageSnippets { + let model: GenerativeModel! = nil + + func cloudStorageFile_referenceMIMEType() async throws { + // Upload an image file using Cloud Storage for Firebase. + let storageRef = Storage.storage().reference(withPath: "images/image.jpg") + guard let imageURL = Bundle.main.url(forResource: "image", withExtension: "jpg") else { + fatalError("File 'image.jpg' not found in main bundle.") + } + let metadata = try await storageRef.putFileAsync(from: imageURL) + + // Get the MIME type and Cloud Storage for Firebase URL. + guard let mimeType = metadata.contentType else { + fatalError("The MIME type of the uploaded image is nil.") + } + // Construct a URL in the required format. + let storageURL = "gs://\(storageRef.bucket)/\(storageRef.fullPath)" + + let prompt = "What's in this picture?" + // Construct the imagePart with the MIME type and the URL. + let imagePart = FileDataPart(uri: storageURL, mimeType: mimeType) + + // To generate text output, call generateContent with the prompt and the imagePart. + let result = try await model.generateContent(prompt, imagePart) + if let text = result.text { + print(text) + } + } + + func cloudStorageFile_explicitMIMEType() async throws { + let prompt = "What's in this picture?" + // Construct an imagePart that explicitly includes the MIME type and Cloud Storage for Firebase + // URL values. + let imagePart = FileDataPart(uri: "gs://bucket-name/path/image.jpg", mimeType: "image/jpeg") + + // To generate text output, call generateContent with the prompt and imagePart. + let result = try await model.generateContent(prompt, imagePart) + if let text = result.text { + print(text) + } + } +} diff --git a/Package.swift b/Package.swift index 73151c6c226..d15a82c837f 100644 --- a/Package.swift +++ b/Package.swift @@ -1310,7 +1310,10 @@ let package = Package( ), .testTarget( name: "FirebaseVertexAIUnit", - dependencies: ["FirebaseVertexAI"], + dependencies: [ + "FirebaseVertexAI", + "FirebaseStorage", + ], path: "FirebaseVertexAI/Tests/Unit", resources: [ .process("vertexai-sdk-test-data/mock-responses"), From acb0befdf51140ec7d6d224d2fdfc9ccf555c0d3 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Fri, 22 Nov 2024 13:04:49 -0500 Subject: [PATCH 2/3] Compile on SPM only --- .../Unit/Snippets/CloudStorageSnippets.swift | 86 ++++++++++--------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/FirebaseVertexAI/Tests/Unit/Snippets/CloudStorageSnippets.swift b/FirebaseVertexAI/Tests/Unit/Snippets/CloudStorageSnippets.swift index 98b1f853998..15322627d43 100644 --- a/FirebaseVertexAI/Tests/Unit/Snippets/CloudStorageSnippets.swift +++ b/FirebaseVertexAI/Tests/Unit/Snippets/CloudStorageSnippets.swift @@ -12,52 +12,56 @@ // See the License for the specific language governing permissions and // limitations under the License. -import FirebaseCore -import FirebaseStorage -import FirebaseVertexAI - -// These CloudStorageSnippets are not currently runnable due to the GCS upload paths but are used as -// compilation tests. -@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) -final class CloudStorageSnippets { - let model: GenerativeModel! = nil - - func cloudStorageFile_referenceMIMEType() async throws { - // Upload an image file using Cloud Storage for Firebase. - let storageRef = Storage.storage().reference(withPath: "images/image.jpg") - guard let imageURL = Bundle.main.url(forResource: "image", withExtension: "jpg") else { - fatalError("File 'image.jpg' not found in main bundle.") - } - let metadata = try await storageRef.putFileAsync(from: imageURL) +#if SWIFT_PACKAGE // The FirebaseStorage dependency has only been added in Package.swift. - // Get the MIME type and Cloud Storage for Firebase URL. - guard let mimeType = metadata.contentType else { - fatalError("The MIME type of the uploaded image is nil.") - } - // Construct a URL in the required format. - let storageURL = "gs://\(storageRef.bucket)/\(storageRef.fullPath)" + import FirebaseCore + import FirebaseStorage + import FirebaseVertexAI + + // These CloudStorageSnippets are not currently runnable due to the GCS upload paths but are used + // as compilation tests. + @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) + final class CloudStorageSnippets { + let model: GenerativeModel! = nil + + func cloudStorageFile_referenceMIMEType() async throws { + // Upload an image file using Cloud Storage for Firebase. + let storageRef = Storage.storage().reference(withPath: "images/image.jpg") + guard let imageURL = Bundle.main.url(forResource: "image", withExtension: "jpg") else { + fatalError("File 'image.jpg' not found in main bundle.") + } + let metadata = try await storageRef.putFileAsync(from: imageURL) - let prompt = "What's in this picture?" - // Construct the imagePart with the MIME type and the URL. - let imagePart = FileDataPart(uri: storageURL, mimeType: mimeType) + // Get the MIME type and Cloud Storage for Firebase URL. + guard let mimeType = metadata.contentType else { + fatalError("The MIME type of the uploaded image is nil.") + } + // Construct a URL in the required format. + let storageURL = "gs://\(storageRef.bucket)/\(storageRef.fullPath)" - // To generate text output, call generateContent with the prompt and the imagePart. - let result = try await model.generateContent(prompt, imagePart) - if let text = result.text { - print(text) + let prompt = "What's in this picture?" + // Construct the imagePart with the MIME type and the URL. + let imagePart = FileDataPart(uri: storageURL, mimeType: mimeType) + + // To generate text output, call generateContent with the prompt and the imagePart. + let result = try await model.generateContent(prompt, imagePart) + if let text = result.text { + print(text) + } } - } - func cloudStorageFile_explicitMIMEType() async throws { - let prompt = "What's in this picture?" - // Construct an imagePart that explicitly includes the MIME type and Cloud Storage for Firebase - // URL values. - let imagePart = FileDataPart(uri: "gs://bucket-name/path/image.jpg", mimeType: "image/jpeg") + func cloudStorageFile_explicitMIMEType() async throws { + let prompt = "What's in this picture?" + // Construct an imagePart that explicitly includes the MIME type and Cloud Storage for + // Firebase URL values. + let imagePart = FileDataPart(uri: "gs://bucket-name/path/image.jpg", mimeType: "image/jpeg") - // To generate text output, call generateContent with the prompt and imagePart. - let result = try await model.generateContent(prompt, imagePart) - if let text = result.text { - print(text) + // To generate text output, call generateContent with the prompt and imagePart. + let result = try await model.generateContent(prompt, imagePart) + if let text = result.text { + print(text) + } } } -} + +#endif // SWIFT_PACKAGE From 1d3059313022e272ba009ae0fba20fc9677846c1 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Mon, 25 Nov 2024 10:27:35 -0500 Subject: [PATCH 3/3] Update comment line breaks to match internal snippet --- .../Tests/Unit/Snippets/CloudStorageSnippets.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FirebaseVertexAI/Tests/Unit/Snippets/CloudStorageSnippets.swift b/FirebaseVertexAI/Tests/Unit/Snippets/CloudStorageSnippets.swift index 15322627d43..6627fb870ea 100644 --- a/FirebaseVertexAI/Tests/Unit/Snippets/CloudStorageSnippets.swift +++ b/FirebaseVertexAI/Tests/Unit/Snippets/CloudStorageSnippets.swift @@ -52,8 +52,8 @@ func cloudStorageFile_explicitMIMEType() async throws { let prompt = "What's in this picture?" - // Construct an imagePart that explicitly includes the MIME type and Cloud Storage for - // Firebase URL values. + // Construct an imagePart that explicitly includes the MIME type and + // Cloud Storage for Firebase URL values. let imagePart = FileDataPart(uri: "gs://bucket-name/path/image.jpg", mimeType: "image/jpeg") // To generate text output, call generateContent with the prompt and imagePart.