Skip to content

Commit 8ae5205

Browse files
authored
Add documentation code snippets for counting tokens (#185)
1 parent b72e9ba commit 8ae5205

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

samples/CountTokens.swift

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 CountTokensSnippets: 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 testCountTokensTextOnly() async throws {
34+
// [START tokens_text_only]
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+
)
43+
44+
let prompt = "Write a story about a magic backpack."
45+
46+
let response = try await generativeModel.countTokens(prompt)
47+
48+
print("Total Tokens: \(response.totalTokens)")
49+
// [END tokens_text_only]
50+
}
51+
52+
func testCountTokensChat() async throws {
53+
// [START tokens_chat]
54+
let generativeModel =
55+
GenerativeModel(
56+
// Specify a Gemini model appropriate for your use case
57+
name: "gemini-1.5-flash",
58+
// Access your API key from your on-demand resource .plist file (see "Set up your API key"
59+
// above)
60+
apiKey: APIKey.default
61+
)
62+
63+
// Optionally specify existing chat history
64+
let history = [
65+
ModelContent(role: "user", parts: "Hello, I have 2 dogs in my house."),
66+
ModelContent(role: "model", parts: "Great to meet you. What would you like to know?"),
67+
]
68+
69+
// Initialize the chat with optional chat history
70+
let chat = generativeModel.startChat(history: history)
71+
72+
let response = try await generativeModel.countTokens(chat.history + [
73+
ModelContent(role: "user", parts: "This is the message I intend to send"),
74+
])
75+
print("Total Tokens: \(response.totalTokens)")
76+
// [END tokens_chat]
77+
}
78+
79+
#if canImport(UIKit)
80+
func testCountTokensMultimodalInline() async throws {
81+
// [START tokens_multimodal_image_inline]
82+
let generativeModel =
83+
GenerativeModel(
84+
// Specify a Gemini model appropriate for your use case
85+
name: "gemini-1.5-flash",
86+
// Access your API key from your on-demand resource .plist file (see "Set up your API key"
87+
// above)
88+
apiKey: APIKey.default
89+
)
90+
91+
guard let image1 = UIImage(systemName: "cloud.sun") else { fatalError() }
92+
guard let image2 = UIImage(systemName: "cloud.heavyrain") else { fatalError() }
93+
94+
let prompt = "What's the difference between these pictures?"
95+
96+
let response = try await generativeModel.countTokens(image1, image2, prompt)
97+
print("Total Tokens: \(response.totalTokens)")
98+
// [END tokens_multimodal_image_inline]
99+
}
100+
#endif // canImport(UIKit)
101+
}

0 commit comments

Comments
 (0)