Skip to content

Commit bad1c2b

Browse files
authored
Add documentation code snippets for model config (#186)
1 parent 5ec651c commit bad1c2b

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

samples/GenerationConfig.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 GoogleGenerativeAI
16+
import XCTest
17+
18+
// Set up your API Key
19+
// ====================
20+
// To use the Gemini API, you'll need an API key. To learn more, see the "Set up your API Key"
21+
// section in the Gemini API quickstart:
22+
// https://ai.google.dev/gemini-api/docs/quickstart?lang=swift#set-up-api-key
23+
24+
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
25+
final class GenerationConfigSnippets: XCTestCase {
26+
override func setUpWithError() throws {
27+
try XCTSkipIf(
28+
APIKey.default.isEmpty,
29+
"`\(APIKey.apiKeyEnvVar)` environment variable not set."
30+
)
31+
}
32+
33+
func testConfigureModelParameters() {
34+
// [START configure_model_parameters]
35+
let config = GenerationConfig(
36+
temperature: 0.9,
37+
topP: 0.1,
38+
topK: 16,
39+
candidateCount: 1,
40+
maxOutputTokens: 200,
41+
stopSequences: ["red", "orange"]
42+
)
43+
44+
let generativeModel =
45+
GenerativeModel(
46+
// Specify a Gemini model appropriate for your use case
47+
name: "gemini-1.5-flash",
48+
// Access your API key from your on-demand resource .plist file (see "Set up your API key"
49+
// above)
50+
apiKey: APIKey.default,
51+
generationConfig: config
52+
)
53+
// [END configure_model_parameters]
54+
55+
// Added to silence the compiler warning about unused variable.
56+
let _ = String(describing: generativeModel)
57+
}
58+
}

samples/SafetySettings.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 GoogleGenerativeAI
16+
import XCTest
17+
18+
// Set up your API Key
19+
// ====================
20+
// To use the Gemini API, you'll need an API key. To learn more, see the "Set up your API Key"
21+
// section in the Gemini API quickstart:
22+
// https://ai.google.dev/gemini-api/docs/quickstart?lang=swift#set-up-api-key
23+
24+
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
25+
final class SafetySettingsSnippets: XCTestCase {
26+
override func setUpWithError() throws {
27+
try XCTSkipIf(
28+
APIKey.default.isEmpty,
29+
"`\(APIKey.apiKeyEnvVar)` environment variable not set."
30+
)
31+
}
32+
33+
func testSafetySettings() {
34+
// [START safety_settings]
35+
let generativeModel =
36+
GenerativeModel(
37+
// Specify a Gemini model appropriate for your use case
38+
name: "gemini-1.5-flash",
39+
// Access your API key from your on-demand resource .plist file (see "Set up your API key"
40+
// above)
41+
apiKey: APIKey.default,
42+
safetySettings: [SafetySetting(harmCategory: .harassment, threshold: .blockLowAndAbove)]
43+
)
44+
// [END safety_settings]
45+
46+
// Added to silence the compiler warning about unused variable.
47+
let _ = String(describing: generativeModel)
48+
}
49+
50+
func testSafetySettingsMulti() {
51+
// [START safety_settings_multi]
52+
let safetySettings = [
53+
SafetySetting(harmCategory: .dangerousContent, threshold: .blockLowAndAbove),
54+
SafetySetting(harmCategory: .harassment, threshold: .blockMediumAndAbove),
55+
SafetySetting(harmCategory: .hateSpeech, threshold: .blockOnlyHigh),
56+
]
57+
58+
let generativeModel =
59+
GenerativeModel(
60+
// Specify a Gemini model appropriate for your use case
61+
name: "gemini-1.5-flash",
62+
// Access your API key from your on-demand resource .plist file (see "Set up your API key"
63+
// above)
64+
apiKey: APIKey.default,
65+
safetySettings: safetySettings
66+
)
67+
// [END safety_settings_multi]
68+
69+
// Added to silence the compiler warning about unused variable.
70+
let _ = String(describing: generativeModel)
71+
}
72+
}

samples/SystemInstructions.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 GoogleGenerativeAI
16+
import XCTest
17+
18+
// Set up your API Key
19+
// ====================
20+
// To use the Gemini API, you'll need an API key. To learn more, see the "Set up your API Key"
21+
// section in the Gemini API quickstart:
22+
// https://ai.google.dev/gemini-api/docs/quickstart?lang=swift#set-up-api-key
23+
24+
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
25+
final class SystemInstructionsSnippets: XCTestCase {
26+
override func setUpWithError() throws {
27+
try XCTSkipIf(
28+
APIKey.default.isEmpty,
29+
"`\(APIKey.apiKeyEnvVar)` environment variable not set."
30+
)
31+
}
32+
33+
func testSystemInstruction() {
34+
// [START system_instruction]
35+
let generativeModel =
36+
GenerativeModel(
37+
// Specify a model that supports system instructions, like a Gemini 1.5 model
38+
name: "gemini-1.5-flash",
39+
// Access your API key from your on-demand resource .plist file (see "Set up your API key"
40+
// above)
41+
apiKey: APIKey.default,
42+
systemInstruction: ModelContent(role: "system", parts: "You are a cat. Your name is Neko.")
43+
)
44+
// [END system_instruction]
45+
46+
// Added to silence the compiler warning about unused variable.
47+
let _ = String(describing: generativeModel)
48+
}
49+
}

0 commit comments

Comments
 (0)