Skip to content

Commit 79be9f0

Browse files
committed
[Vertex AI] Add multimodal code snippets
1 parent 78c1c2f commit 79be9f0

File tree

2 files changed

+122
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)