Skip to content

Commit 9054b7f

Browse files
committed
[Vertex AI] Add Developer API encoding CountTokensRequest
1 parent 6039e0b commit 9054b7f

File tree

7 files changed

+66
-15
lines changed

7 files changed

+66
-15
lines changed

FirebaseVertexAI/Sources/CountTokensRequest.swift

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@ import Foundation
1616

1717
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1818
struct CountTokensRequest {
19-
let model: String
20-
21-
let contents: [ModelContent]
22-
let systemInstruction: ModelContent?
23-
let tools: [Tool]?
24-
let generationConfig: GenerationConfig?
25-
26-
let options: RequestOptions
19+
let generateContentRequest: GenerateContentRequest
2720
}
2821

2922
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
3023
extension CountTokensRequest: GenerativeAIRequest {
3124
typealias Response = CountTokensResponse
3225

26+
var options: RequestOptions {
27+
generateContentRequest.options
28+
}
29+
3330
var url: URL {
34-
URL(string: "\(Constants.baseURL)/\(options.apiVersion)/\(model):countTokens")!
31+
let model = generateContentRequest.model
32+
return URL(string: "\(Constants.baseURL)/\(options.apiVersion)/\(model):countTokens")!
3533
}
3634
}
3735

@@ -55,12 +53,36 @@ public struct CountTokensResponse {
5553

5654
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
5755
extension CountTokensRequest: Encodable {
58-
enum CodingKeys: CodingKey {
56+
enum VertexCodingKeys: CodingKey {
5957
case contents
6058
case systemInstruction
6159
case tools
6260
case generationConfig
6361
}
62+
63+
enum DeveloperCodingKeys: CodingKey {
64+
case generateContentRequest
65+
}
66+
67+
func encode(to encoder: any Encoder) throws {
68+
let backendAPI = encoder.userInfo[CodingUserInfoKey(rawValue: "BackendAPI")!] as! BackendAPI
69+
70+
switch backendAPI {
71+
case .vertexAI:
72+
var container = encoder.container(keyedBy: VertexCodingKeys.self)
73+
try container.encode(generateContentRequest.contents, forKey: .contents)
74+
try container.encodeIfPresent(
75+
generateContentRequest.systemInstruction, forKey: .systemInstruction
76+
)
77+
try container.encodeIfPresent(generateContentRequest.tools, forKey: .tools)
78+
try container.encodeIfPresent(
79+
generateContentRequest.generationConfig, forKey: .generationConfig
80+
)
81+
case .developer:
82+
var container = encoder.container(keyedBy: DeveloperCodingKeys.self)
83+
try container.encode(generateContentRequest, forKey: .generateContentRequest)
84+
}
85+
}
6486
}
6587

6688
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)

FirebaseVertexAI/Sources/FirebaseInfo.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,21 @@ struct FirebaseInfo: Sendable {
2828
let apiKey: String
2929
let googleAppID: String
3030
let app: FirebaseApp
31+
let backendAPI: BackendAPI
3132

3233
init(appCheck: AppCheckInterop? = nil,
3334
auth: AuthInterop? = nil,
3435
projectID: String,
3536
apiKey: String,
3637
googleAppID: String,
37-
firebaseApp: FirebaseApp) {
38+
firebaseApp: FirebaseApp,
39+
backendAPI: BackendAPI) {
3840
self.appCheck = appCheck
3941
self.auth = auth
4042
self.projectID = projectID
4143
self.apiKey = apiKey
4244
self.googleAppID = googleAppID
4345
app = firebaseApp
46+
self.backendAPI = backendAPI
4447
}
4548
}

FirebaseVertexAI/Sources/GenerateContentRequest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct GenerateContentRequest: Sendable {
3131
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
3232
extension GenerateContentRequest: Encodable {
3333
enum CodingKeys: String, CodingKey {
34+
case model
3435
case contents
3536
case generationConfig
3637
case safetySettings

FirebaseVertexAI/Sources/GenerativeAIService.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ struct GenerativeAIService {
199199
// }
200200

201201
let encoder = JSONEncoder()
202+
encoder.userInfo[CodingUserInfoKey(rawValue: "BackendAPI")!] = firebaseInfo.backendAPI
202203
urlRequest.httpBody = try encoder.encode(request)
203204
urlRequest.timeoutInterval = request.options.timeout
204205

FirebaseVertexAI/Sources/GenerativeModel.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,19 @@ public final class GenerativeModel: Sendable {
245245
/// - Returns: The results of running the model's tokenizer on the input; contains
246246
/// ``CountTokensResponse/totalTokens``.
247247
public func countTokens(_ content: [ModelContent]) async throws -> CountTokensResponse {
248-
let countTokensRequest = CountTokensRequest(
248+
let generateContentRequest = GenerateContentRequest(
249249
model: modelResourceName,
250250
contents: content,
251-
systemInstruction: systemInstruction,
252-
tools: tools,
253251
generationConfig: generationConfig,
252+
safetySettings: safetySettings,
253+
tools: tools,
254+
toolConfig: toolConfig,
255+
systemInstruction: systemInstruction,
256+
isStreaming: false,
254257
options: requestOptions
255258
)
259+
let countTokensRequest = CountTokensRequest(generateContentRequest: generateContentRequest)
260+
256261
return try await generativeAIService.loadRequest(request: countTokensRequest)
257262
}
258263

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2025 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+
enum BackendAPI {
16+
case vertexAI
17+
case developer
18+
}

FirebaseVertexAI/Sources/VertexAI.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ public class VertexAI {
173173
projectID: projectID,
174174
apiKey: apiKey,
175175
googleAppID: app.options.googleAppID,
176-
firebaseApp: app
176+
firebaseApp: app,
177+
backendAPI: .vertexAI
177178
)
178179
self.location = location
179180
}

0 commit comments

Comments
 (0)