-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[Vertex AI] Add Developer API encoding CountTokensRequest
#14512
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
276fa48
[Vertex AI] Add Developer API encoding `CountTokensRequest`
andrewheard 5ad783d
Fix tests
andrewheard ff59d95
Add `CountTokensIntegrationTests`
andrewheard 6202ea2
Fix rebase issues
andrewheard 83d8d3a
Address Gemini review comments
andrewheard 041b9d1
Add system instruction integration tests
andrewheard dece0ad
Add minimal encoding unit tests for `CountTokensRequest`
andrewheard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
FirebaseVertexAI/Tests/TestApp/Tests/Integration/CountTokensIntegrationTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import FirebaseAuth | ||
import FirebaseCore | ||
import FirebaseStorage | ||
import FirebaseVertexAI | ||
import Testing | ||
import VertexAITestApp | ||
|
||
@testable import struct FirebaseVertexAI.APIConfig | ||
|
||
@Suite(.serialized) | ||
struct CountTokensIntegrationTests { | ||
let generationConfig = GenerationConfig( | ||
temperature: 1.2, | ||
topP: 0.95, | ||
topK: 32, | ||
candidateCount: 1, | ||
maxOutputTokens: 8192, | ||
presencePenalty: 1.5, | ||
frequencyPenalty: 1.75, | ||
stopSequences: ["cat", "dog", "bird"] | ||
) | ||
let safetySettings = [ | ||
SafetySetting(harmCategory: .harassment, threshold: .blockLowAndAbove), | ||
SafetySetting(harmCategory: .hateSpeech, threshold: .blockLowAndAbove), | ||
SafetySetting(harmCategory: .sexuallyExplicit, threshold: .blockLowAndAbove), | ||
SafetySetting(harmCategory: .dangerousContent, threshold: .blockLowAndAbove), | ||
SafetySetting(harmCategory: .civicIntegrity, threshold: .blockLowAndAbove), | ||
] | ||
let systemInstruction = ModelContent( | ||
role: "system", | ||
parts: "You are a friendly and helpful assistant." | ||
) | ||
|
||
@Test(arguments: InstanceConfig.allConfigs) | ||
func countTokens_text(_ config: InstanceConfig) async throws { | ||
let prompt = "Why is the sky blue?" | ||
let model = VertexAI.componentInstance(config).generativeModel( | ||
modelName: ModelNames.gemini2Flash, | ||
generationConfig: generationConfig, | ||
safetySettings: safetySettings | ||
) | ||
|
||
let response = try await model.countTokens(prompt) | ||
|
||
#expect(response.totalTokens == 6) | ||
switch config.apiConfig.service { | ||
case .vertexAI: | ||
#expect(response.totalBillableCharacters == 16) | ||
case .developer: | ||
#expect(response.totalBillableCharacters == nil) | ||
} | ||
andrewheard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#expect(response.promptTokensDetails.count == 1) | ||
let promptTokensDetails = try #require(response.promptTokensDetails.first) | ||
#expect(promptTokensDetails.modality == .text) | ||
#expect(promptTokensDetails.tokenCount == response.totalTokens) | ||
} | ||
andrewheard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Test(arguments: [ | ||
InstanceConfig.vertexV1, | ||
InstanceConfig.vertexV1Beta, | ||
/* System instructions are not supported on the v1 Developer API. */ | ||
InstanceConfig.developerV1Beta, | ||
]) | ||
func countTokens_text_systemInstruction(_ config: InstanceConfig) async throws { | ||
let model = VertexAI.componentInstance(config).generativeModel( | ||
modelName: ModelNames.gemini2Flash, | ||
generationConfig: generationConfig, | ||
safetySettings: safetySettings, | ||
systemInstruction: systemInstruction // Not supported on the v1 Developer API | ||
) | ||
|
||
let response = try await model.countTokens("What is your favourite colour?") | ||
|
||
#expect(response.totalTokens == 14) | ||
switch config.apiConfig.service { | ||
case .vertexAI: | ||
#expect(response.totalBillableCharacters == 61) | ||
case .developer: | ||
#expect(response.totalBillableCharacters == nil) | ||
} | ||
#expect(response.promptTokensDetails.count == 1) | ||
let promptTokensDetails = try #require(response.promptTokensDetails.first) | ||
#expect(promptTokensDetails.modality == .text) | ||
#expect(promptTokensDetails.tokenCount == response.totalTokens) | ||
} | ||
|
||
@Test(arguments: [ | ||
/* System instructions are not supported on the v1 Developer API. */ | ||
InstanceConfig.developerV1, | ||
]) | ||
func countTokens_text_systemInstruction_unsupported(_ config: InstanceConfig) async throws { | ||
let model = VertexAI.componentInstance(config).generativeModel( | ||
modelName: ModelNames.gemini2Flash, | ||
systemInstruction: systemInstruction // Not supported on the v1 Developer API | ||
) | ||
|
||
try await #require( | ||
throws: BackendError.self, | ||
""" | ||
If this test fails (i.e., `countTokens` succeeds), remove \(config) from this test and add it | ||
to `countTokens_text_systemInstruction`. | ||
""", | ||
performing: { | ||
try await model.countTokens("What is your favourite colour?") | ||
} | ||
) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.