Skip to content

Commit 9d10b80

Browse files
authored
[Vertex AI] Add compilation tests for GCS snippets (#14164)
1 parent c6681a4 commit 9d10b80

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#if SWIFT_PACKAGE // The FirebaseStorage dependency has only been added in Package.swift.
16+
17+
import FirebaseCore
18+
import FirebaseStorage
19+
import FirebaseVertexAI
20+
21+
// These CloudStorageSnippets are not currently runnable due to the GCS upload paths but are used
22+
// as compilation tests.
23+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
24+
final class CloudStorageSnippets {
25+
let model: GenerativeModel! = nil
26+
27+
func cloudStorageFile_referenceMIMEType() async throws {
28+
// Upload an image file using Cloud Storage for Firebase.
29+
let storageRef = Storage.storage().reference(withPath: "images/image.jpg")
30+
guard let imageURL = Bundle.main.url(forResource: "image", withExtension: "jpg") else {
31+
fatalError("File 'image.jpg' not found in main bundle.")
32+
}
33+
let metadata = try await storageRef.putFileAsync(from: imageURL)
34+
35+
// Get the MIME type and Cloud Storage for Firebase URL.
36+
guard let mimeType = metadata.contentType else {
37+
fatalError("The MIME type of the uploaded image is nil.")
38+
}
39+
// Construct a URL in the required format.
40+
let storageURL = "gs://\(storageRef.bucket)/\(storageRef.fullPath)"
41+
42+
let prompt = "What's in this picture?"
43+
// Construct the imagePart with the MIME type and the URL.
44+
let imagePart = FileDataPart(uri: storageURL, mimeType: mimeType)
45+
46+
// To generate text output, call generateContent with the prompt and the imagePart.
47+
let result = try await model.generateContent(prompt, imagePart)
48+
if let text = result.text {
49+
print(text)
50+
}
51+
}
52+
53+
func cloudStorageFile_explicitMIMEType() async throws {
54+
let prompt = "What's in this picture?"
55+
// Construct an imagePart that explicitly includes the MIME type and
56+
// Cloud Storage for Firebase URL values.
57+
let imagePart = FileDataPart(uri: "gs://bucket-name/path/image.jpg", mimeType: "image/jpeg")
58+
59+
// To generate text output, call generateContent with the prompt and imagePart.
60+
let result = try await model.generateContent(prompt, imagePart)
61+
if let text = result.text {
62+
print(text)
63+
}
64+
}
65+
}
66+
67+
#endif // SWIFT_PACKAGE

Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,10 @@ let package = Package(
13101310
),
13111311
.testTarget(
13121312
name: "FirebaseVertexAIUnit",
1313-
dependencies: ["FirebaseVertexAI"],
1313+
dependencies: [
1314+
"FirebaseVertexAI",
1315+
"FirebaseStorage",
1316+
],
13141317
path: "FirebaseVertexAI/Tests/Unit",
13151318
resources: [
13161319
.process("vertexai-sdk-test-data/mock-responses"),

0 commit comments

Comments
 (0)