Skip to content

Commit 97f7f1d

Browse files
committed
[Vertex AI] Add compilation test for GCS snippets
1 parent 7cfb06f commit 97f7f1d

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
import FirebaseCore
16+
import FirebaseStorage
17+
import FirebaseVertexAI
18+
19+
// These CloudStorageSnippets are not currently runnable due to the GCS upload paths but are used as
20+
// compilation tests.
21+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
22+
final class CloudStorageSnippets {
23+
let model: GenerativeModel! = nil
24+
25+
func cloudStorageFile_referenceMIMEType() async throws {
26+
// Upload an image file using Cloud Storage for Firebase.
27+
let storageRef = Storage.storage().reference(withPath: "images/image.jpg")
28+
guard let imageURL = Bundle.main.url(forResource: "image", withExtension: "jpg") else {
29+
fatalError("File 'image.jpg' not found in main bundle.")
30+
}
31+
let metadata = try await storageRef.putFileAsync(from: imageURL)
32+
33+
// Get the MIME type and Cloud Storage for Firebase URL.
34+
guard let mimeType = metadata.contentType else {
35+
fatalError("The MIME type of the uploaded image is nil.")
36+
}
37+
// Construct a URL in the required format.
38+
let storageURL = "gs://\(storageRef.bucket)/\(storageRef.fullPath)"
39+
40+
let prompt = "What's in this picture?"
41+
// Construct the imagePart with the MIME type and the URL.
42+
let imagePart = FileDataPart(uri: storageURL, mimeType: mimeType)
43+
44+
// To generate text output, call generateContent with the prompt and the imagePart.
45+
let result = try await model.generateContent(prompt, imagePart)
46+
if let text = result.text {
47+
print(text)
48+
}
49+
}
50+
51+
func cloudStorageFile_explicitMIMEType() async throws {
52+
let prompt = "What's in this picture?"
53+
// Construct an imagePart that explicitly includes the MIME type and Cloud Storage for Firebase
54+
// URL values.
55+
let imagePart = FileDataPart(uri: "gs://bucket-name/path/image.jpg", mimeType: "image/jpeg")
56+
57+
// To generate text output, call generateContent with the prompt and imagePart.
58+
let result = try await model.generateContent(prompt, imagePart)
59+
if let text = result.text {
60+
print(text)
61+
}
62+
}
63+
}

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)