Skip to content

Adds some snippets in devsite that were missing and fixes a snippet start tag. #394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions VertexAISnippets/VertexAISnippets/VertexAISnippets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,63 @@ class Snippets {
self.model = model
}

func templateInitializeModel() {
// [START template_initialize_model]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the snippet registry... would we use this initialization snippet or the one added with #392?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved with the deletion of the other snippet and conversion of all snippets to template model name.

// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()

// Initialize the generative model with a model that supports your use case
// Gemini 1.5 Pro is versatile and can accept both text-only or multimodal prompt inputs

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Gemini 1.5 models are versatile and can be used with all API capabilities

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

let model = vertex.generativeModel(modelName: "{{generic_model_name_initialization}}")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for includecode and the snippet registry to work with DevSite variables like this?

{{generic_model_name_initialization}}

If so, could we pls change the explicit insertion of a model name in the other snippets to use this variable, too? :-D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe. It depends on which templating thing runs first in devsite. I put this here without checking, but I can try it out in staging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does work!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted this snippet and updated all others to use the template.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried moving these into the docs today and it doesn't work. Can't remember what I did to test that made them work.

// [END template_initialize_model]
}

func configureModel() {
let vertex = VertexAI.vertexAI()

// [START configure_model]
let config = GenerationConfig(
temperature: 0.9,
topP: 0.1,
topK: 16,
maxOutputTokens: 200,
stopSequences: ["red"]
)

let model = vertex.generativeModel(
modelName: "{{ '<var>MODEL_NAME</var>' }}",
generationConfig: config
)
// [END configure_model]
}

func safetySettings() {
let vertex = VertexAI.vertexAI()

// [START safety_settings]
let model = vertex.generativeModel(
modelName: "{{ '<var>MODEL_NAME</var>' }}",
safetySettings: [
SafetySetting(harmCategory: .harassment, threshold: .blockOnlyHigh)
]
)
// [END safety_settings]
}

func multiSafetySettings() {
let vertex = VertexAI.vertexAI()

// [START multi_safety_settings]
let harassmentSafety = SafetySetting(harmCategory: .harassment, threshold: .blockOnlyHigh)
let hateSpeechSafety = SafetySetting(harmCategory: .hateSpeech, threshold: .blockMediumAndAbove)

let model = vertex.generativeModel(
modelName: "{{ '<var>MODEL_NAME</var>' }}",
safetySettings: [harassmentSafety, hateSpeechSafety]
)
// END multi_safety_settings
}

func callGemini() async throws {
// [START call_gemini]
// Provide a prompt that contains text
Expand Down Expand Up @@ -181,7 +238,6 @@ class Snippets {
}

func textAndVideoPrompt() async throws {
// AVFoundation support coming soon™
// [START text_video_prompt]
guard let fileURL = Bundle.main.url(forResource: "sample",
withExtension: "mp4") else { fatalError() }
Expand Down Expand Up @@ -273,7 +329,7 @@ class Snippets {
let response = try await model.countTokens(image, "What's in this picture?")
print("Total Tokens: \(response.totalTokens)")
print("Total Billable Characters: \(response.totalBillableCharacters)")
// [START count_tokens_text_image]
// [END count_tokens_text_image]
}

func countTokensMultiImage() async throws {
Expand Down