Skip to content

Commit bb888cd

Browse files
authored
[Vertex AI] Add multimodal code snippets (#13929)
1 parent 78c1c2f commit bb888cd

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Binary file not shown.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 FirebaseVertexAI
17+
import XCTest
18+
19+
#if canImport(UIKit)
20+
import UIKit
21+
#endif // canImport(UIKit)
22+
23+
// These snippet tests are intentionally skipped in CI jobs; see the README file in this directory
24+
// for instructions on running them manually.
25+
26+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
27+
final class MultimodalSnippets: XCTestCase {
28+
let bundle = BundleTestUtil.bundle()
29+
lazy var model = VertexAI.vertexAI().generativeModel(modelName: "gemini-1.5-flash")
30+
lazy var videoURL = {
31+
guard let url = bundle.url(forResource: "animals", withExtension: "mp4") else {
32+
fatalError("Video file animals.mp4 not found in Resources.")
33+
}
34+
return url
35+
}()
36+
37+
override func setUpWithError() throws {
38+
try FirebaseApp.configureDefaultAppForSnippets()
39+
}
40+
41+
override func tearDown() async throws {
42+
await FirebaseApp.deleteDefaultAppForSnippets()
43+
}
44+
45+
#if canImport(UIKit)
46+
func testMultimodalOneImageNonStreaming() async throws {
47+
guard let image = UIImage(systemName: "bicycle") else { fatalError() }
48+
49+
// Provide a text prompt to include with the image
50+
let prompt = "What's in this picture?"
51+
52+
// To generate text output, call generateContent and pass in the prompt
53+
let response = try await model.generateContent(image, prompt)
54+
print(response.text ?? "No text in response.")
55+
}
56+
57+
func testMultimodalOneImageStreaming() async throws {
58+
guard let image = UIImage(systemName: "bicycle") else { fatalError() }
59+
60+
// Provide a text prompt to include with the image
61+
let prompt = "What's in this picture?"
62+
63+
// To stream generated text output, call generateContentStream and pass in the prompt
64+
let contentStream = try model.generateContentStream(image, prompt)
65+
for try await chunk in contentStream {
66+
if let text = chunk.text {
67+
print(text)
68+
}
69+
}
70+
}
71+
72+
func testMultimodalMultiImagesNonStreaming() async throws {
73+
guard let image1 = UIImage(systemName: "car") else { fatalError() }
74+
guard let image2 = UIImage(systemName: "car.2") else { fatalError() }
75+
76+
// Provide a text prompt to include with the images
77+
let prompt = "What's different between these pictures?"
78+
79+
// To generate text output, call generateContent and pass in the prompt
80+
let response = try await model.generateContent(image1, image2, prompt)
81+
print(response.text ?? "No text in response.")
82+
}
83+
84+
func testMultimodalMultiImagesStreaming() async throws {
85+
guard let image1 = UIImage(systemName: "car") else { fatalError() }
86+
guard let image2 = UIImage(systemName: "car.2") else { fatalError() }
87+
88+
// Provide a text prompt to include with the images
89+
let prompt = "What's different between these pictures?"
90+
91+
// To stream generated text output, call generateContentStream and pass in the prompt
92+
let contentStream = try model.generateContentStream(image1, image2, prompt)
93+
for try await chunk in contentStream {
94+
if let text = chunk.text {
95+
print(text)
96+
}
97+
}
98+
}
99+
#endif // canImport(UIKit)
100+
101+
func testMultimodalVideoNonStreaming() async throws {
102+
// Provide the video as `Data` with the appropriate MIME type
103+
let video = try InlineDataPart(data: Data(contentsOf: videoURL), mimeType: "video/mp4")
104+
105+
// Provide a text prompt to include with the video
106+
let prompt = "What is in the video?"
107+
108+
// To generate text output, call generateContent with the text and video
109+
let response = try await model.generateContent(video, prompt)
110+
print(response.text ?? "No text in response.")
111+
}
112+
113+
func testMultimodalVideoStreaming() async throws {
114+
// Provide the video as `Data` with the appropriate MIME type
115+
let video = try InlineDataPart(data: Data(contentsOf: videoURL), mimeType: "video/mp4")
116+
117+
// Provide a text prompt to include with the video
118+
let prompt = "What is in the video?"
119+
120+
// To stream generated text output, call generateContentStream with the text and video
121+
let contentStream = try model.generateContentStream(video, prompt)
122+
for try await chunk in contentStream {
123+
if let text = chunk.text {
124+
print(text)
125+
}
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)