Skip to content

Commit 6d62bad

Browse files
authored
Merge pull request #396 from firebase/mc/vertex
Rachel's code review comments
2 parents f39db0f + 3a50d85 commit 6d62bad

File tree

1 file changed

+30
-70
lines changed

1 file changed

+30
-70
lines changed

VertexAISnippets/VertexAISnippets/VertexAISnippets.swift

Lines changed: 30 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,13 @@ class Snippets {
3232
let vertex = VertexAI.vertexAI()
3333

3434
// Initialize the generative model with a model that supports your use case
35-
// Gemini 1.5 Pro is versatile and can accept both text-only or multimodal prompt inputs
36-
let model = vertex.generativeModel(modelName: "gemini-1.5-pro-preview-0409")
35+
// Gemini 1.5 models are versatile and can be used with all API capabilities
36+
let model = vertex.generativeModel(modelName: "{{generic_model_name_initialization}}")
3737
// [END initialize_model]
3838

3939
self.model = model
4040
}
4141

42-
func templateInitializeModel() {
43-
// [START template_initialize_model]
44-
// Initialize the Vertex AI service
45-
let vertex = VertexAI.vertexAI()
46-
47-
// Initialize the generative model with a model that supports your use case
48-
// Gemini 1.5 Pro is versatile and can accept both text-only or multimodal prompt inputs
49-
let model = vertex.generativeModel(modelName: "{{generic_model_name_initialization}}")
50-
// [END template_initialize_model]
51-
}
52-
5342
func configureModel() {
5443
let vertex = VertexAI.vertexAI()
5544

@@ -63,7 +52,7 @@ class Snippets {
6352
)
6453

6554
let model = vertex.generativeModel(
66-
modelName: "{{ '<var>MODEL_NAME</var>' }}",
55+
modelName: "{{generic_model_name_initialization}}",
6756
generationConfig: config
6857
)
6958
// [END configure_model]
@@ -74,7 +63,7 @@ class Snippets {
7463

7564
// [START safety_settings]
7665
let model = vertex.generativeModel(
77-
modelName: "{{ '<var>MODEL_NAME</var>' }}",
66+
modelName: "{{generic_model_name_initialization}}",
7867
safetySettings: [
7968
SafetySetting(harmCategory: .harassment, threshold: .blockOnlyHigh)
8069
]
@@ -90,42 +79,14 @@ class Snippets {
9079
let hateSpeechSafety = SafetySetting(harmCategory: .hateSpeech, threshold: .blockMediumAndAbove)
9180

9281
let model = vertex.generativeModel(
93-
modelName: "{{ '<var>MODEL_NAME</var>' }}",
82+
modelName: "{{generic_model_name_initialization}}",
9483
safetySettings: [harassmentSafety, hateSpeechSafety]
9584
)
9685
// [END multi_safety_settings]
9786
}
9887

99-
func callGemini() async throws {
100-
// [START call_gemini]
101-
// Provide a prompt that contains text
102-
let prompt = "Write a story about a magic backpack."
103-
104-
// To generate text output, call generateContent with the text input
105-
let response = try await model.generateContent(prompt)
106-
if let text = response.text {
107-
print(text)
108-
}
109-
// [END call_gemini]
110-
}
111-
112-
func callGeminiStreaming() async throws {
113-
// [START call_gemini_streaming]
114-
// Provide a prompt that contains text
115-
let prompt = "Write a story about a magic backpack."
116-
117-
// To stream generated text output, call generateContentStream with the text input
118-
let contentStream = model.generateContentStream(prompt)
119-
for try await chunk in contentStream {
120-
if let text = chunk.text {
121-
print(text)
122-
}
123-
}
124-
// [END call_gemini_streaming]
125-
}
126-
12788
func sendTextOnlyPromptStreaming() async throws {
128-
// [START text_only_prompt_streaming]
89+
// [START text_gen_text_only_prompt_streaming]
12990
// Provide a prompt that contains text
13091
let prompt = "Write a story about a magic backpack."
13192

@@ -136,12 +97,11 @@ class Snippets {
13697
print(text)
13798
}
13899
}
139-
// [END text_only_prompt_streaming]
100+
// [START text_gen_text_only_prompt_streaming]
140101
}
141102

142-
// Note: This is the same as the call gemini prompt, but may change in the future.
143103
func sendTextOnlyPromt() async throws {
144-
// [START text_only_prompt]
104+
// [START text_gen_text_only_prompt]
145105
// Provide a prompt that contains text
146106
let prompt = "Write a story about a magic backpack."
147107

@@ -150,11 +110,11 @@ class Snippets {
150110
if let text = response.text {
151111
print(text)
152112
}
153-
// [END text_only_prompt]
113+
// [START text_gen_text_only_prompt]
154114
}
155115

156116
func sendMultimodalPromptStreaming() async throws {
157-
// [START multimodal_prompt_streaming]
117+
// [START text_gen_multimodal_one_image_prompt_streaming]
158118
#if canImport(UIKit)
159119
guard let image = UIImage(named: "image") else { fatalError() }
160120
#else
@@ -171,11 +131,11 @@ class Snippets {
171131
print(text)
172132
}
173133
}
174-
// [END multimodal_prompt_streaming]
134+
// [END text_gen_multimodal_one_image_prompt_streaming]
175135
}
176136

177137
func sendMultimodalPrompt() async throws {
178-
// [START multimodal_prompt]
138+
// [START text_gen_multimodal_one_image_prompt]
179139
// Provide a text prompt to include with the image
180140
#if canImport(UIKit)
181141
guard let image = UIImage(named: "image") else { fatalError() }
@@ -190,11 +150,11 @@ class Snippets {
190150
if let text = response.text {
191151
print(text)
192152
}
193-
// [END multimodal_prompt]
153+
// [END text_gen_multimodal_one_image_prompt]
194154
}
195155

196156
func multiImagePromptStreaming() async throws {
197-
// [START two_image_prompt_streaming]
157+
// [START text_gen_multimodal_multi_image_prompt_streaming]
198158
#if canImport(UIKit)
199159
guard let image1 = UIImage(named: "image1") else { fatalError() }
200160
guard let image2 = UIImage(named: "image2") else { fatalError() }
@@ -213,11 +173,11 @@ class Snippets {
213173
print(text)
214174
}
215175
}
216-
// [END two_image_prompt_streaming]
176+
// [END text_gen_multimodal_multi_image_prompt_streaming]
217177
}
218178

219179
func multiImagePrompt() async throws {
220-
// [START two_image_prompt]
180+
// [START text_gen_multimodal_multi_image_prompt]
221181
#if canImport(UIKit)
222182
guard let image1 = UIImage(named: "image1") else { fatalError() }
223183
guard let image2 = UIImage(named: "image2") else { fatalError() }
@@ -234,12 +194,12 @@ class Snippets {
234194
if let text = response.text {
235195
print(text)
236196
}
237-
// [END two_image_prompt]
197+
// [END text_gen_multimodal_multi_image_prompt]
238198
}
239199

240200
func textAndVideoPrompt() async throws {
241-
// [START text_video_prompt]
242-
guard let fileURL = Bundle.main.url(forResource: "sample",
201+
// [START text_gen_multimodal_video_prompt]
202+
guard let fileURL = Bundle.main.url(forResource: "sample",
243203
withExtension: "mp4") else { fatalError() }
244204
let video = try Data(contentsOf: fileURL)
245205
let prompt = "What's in this video?"
@@ -250,11 +210,11 @@ class Snippets {
250210
if let text = response.text {
251211
print(text)
252212
}
253-
// [END text_video_prompt]
213+
// [END text_gen_multimodal_video_prompt]
254214
}
255215

256216
func textAndVideoPromptStreaming() async throws {
257-
// [START text_video_prompt_streaming]
217+
// [START text_gen_multimodal_video_prompt_streaming]
258218
guard let fileURL = Bundle.main.url(forResource: "sample",
259219
withExtension: "mp4") else { fatalError() }
260220
let video = try Data(contentsOf: fileURL)
@@ -268,7 +228,7 @@ class Snippets {
268228
print(text)
269229
}
270230
}
271-
// [END text_video_prompt_streaming]
231+
// [END text_gen_multimodal_video_prompt_streaming]
272232
}
273233

274234
func chatStreaming() async throws {
@@ -360,26 +320,26 @@ class Snippets {
360320
}
361321

362322
func setSafetySetting() {
363-
// [START set_safety_setting]
323+
// [START set_one_safety_setting]
364324
let model = VertexAI.vertexAI().generativeModel(
365-
modelName: "MODEL_NAME",
325+
modelName: "{{generic_model_name_initialization}}",
366326
safetySettings: [
367327
SafetySetting(harmCategory: .harassment, threshold: .blockOnlyHigh)
368328
]
369329
)
370-
// [END set_safety_setting]
330+
// [END set_one_safety_setting]
371331
}
372332

373333
func setMultipleSafetySettings() {
374-
// [START set_safety_settings]
334+
// [START set_multi_safety_settings]
375335
let harassmentSafety = SafetySetting(harmCategory: .harassment, threshold: .blockOnlyHigh)
376336
let hateSpeechSafety = SafetySetting(harmCategory: .hateSpeech, threshold: .blockMediumAndAbove)
377337

378338
let model = VertexAI.vertexAI().generativeModel(
379-
modelName: "MODEL_NAME",
339+
modelName: "{{generic_model_name_initialization}}",
380340
safetySettings: [harassmentSafety, hateSpeechSafety]
381341
)
382-
// [END set_safety_settings]
342+
// [END set_multi_safety_settings]
383343
}
384344

385345
// MARK: - Function Calling
@@ -421,7 +381,7 @@ class Snippets {
421381
// Initialize the generative model
422382
// Use a model that supports function calling, like Gemini 1.0 Pro.
423383
let model = vertex.generativeModel(
424-
modelName: "gemini-1.0-pro",
384+
modelName: "{{generic_model_name_initialization}}",
425385
// Specify the function declaration.
426386
tools: [Tool(functionDeclarations: [getExchangeRate])]
427387
)
@@ -483,7 +443,7 @@ class Snippets {
483443
// [START function_modes]
484444
let model = VertexAI.vertexAI().generativeModel(
485445
// Setting a function calling mode is only available in Gemini 1.5 Pro
486-
modelName: "gemini-1.5-pro-preview-0409",
446+
modelName: "{{generic_model_name_initialization}}",
487447
// Pass the function declaration
488448
tools: [Tool(functionDeclarations: [getExchangeRate])],
489449
toolConfig: ToolConfig(

0 commit comments

Comments
 (0)