-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Vertex AI] Add simple integration tests (#13075)
- Loading branch information
1 parent
17165d1
commit 901844a
Showing
6 changed files
with
194 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,77 @@ | ||
// Copyright 2024 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 FirebaseCore | ||
import FirebaseVertexAI | ||
import XCTest | ||
|
||
// These tests are functional on other platforms but are compiled-out of non-macOS platforms to | ||
// avoid re-running them as part of `swift-build-run` job (iOS-only) in the `spm` workflow on CI: | ||
// https://github.com/firebase/firebase-ios-sdk/blob/0492e83cb22833ec548e61d854bb7b830e83b826/.github/workflows/spm.yml#L57 | ||
// Since these requests are billed, we are running them more sparsely than the unit tests. | ||
#if os(macOS) | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, *) | ||
final class IntegrationTests: XCTestCase { | ||
// Set temperature, topP and topK to lowest allowed values to make responses more deterministic. | ||
let generationConfig = GenerationConfig(temperature: 0.0, topP: 0.0, topK: 1) | ||
|
||
var vertex: VertexAI! | ||
var model: GenerativeModel! | ||
|
||
override func setUp() async throws { | ||
let plistPath = try XCTUnwrap(Bundle.module.path( | ||
forResource: "GoogleService-Info", | ||
ofType: "plist" | ||
)) | ||
let options = try XCTUnwrap(FirebaseOptions(contentsOfFile: plistPath)) | ||
FirebaseApp.configure(options: options) | ||
|
||
vertex = VertexAI.vertexAI() | ||
model = vertex.generativeModel( | ||
modelName: "gemini-1.5-flash", | ||
generationConfig: generationConfig | ||
) | ||
} | ||
|
||
override func tearDown() async throws { | ||
if let app = FirebaseApp.app() { | ||
await app.delete() | ||
} | ||
} | ||
|
||
// MARK: - Generate Content | ||
|
||
func testGenerateContent() async throws { | ||
let prompt = "Where is Google headquarters located? Answer with the city name only." | ||
|
||
let response = try await model.generateContent(prompt) | ||
|
||
let text = try XCTUnwrap(response.text).trimmingCharacters(in: .whitespacesAndNewlines) | ||
XCTAssertEqual(text, "Mountain View") | ||
} | ||
|
||
// MARK: - Count Tokens | ||
|
||
func testCountTokens() async throws { | ||
let prompt = "Why is the sky blue?" | ||
|
||
let response = try await model.countTokens(prompt) | ||
|
||
XCTAssertEqual(response.totalTokens, 6) | ||
XCTAssertEqual(response.totalBillableCharacters, 16) | ||
} | ||
} | ||
|
||
#endif // os(macOS) |
Empty file.
This file contains 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
Binary file not shown.
77 changes: 77 additions & 0 deletions
77
scripts/spm_test_schemes/FirebaseVertexAIIntegration.xcscheme
This file contains 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,77 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1200" | ||
version = "1.3"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "NO" | ||
buildForArchiving = "NO" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "FirebaseVertexAIIntegration" | ||
BuildableName = "FirebaseVertexAIIntegration" | ||
BlueprintName = "FirebaseVertexAIIntegration" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES"> | ||
<Testables> | ||
<TestableReference | ||
skipped = "NO"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "FirebaseVertexAIIntegration" | ||
BuildableName = "FirebaseVertexAIIntegration" | ||
BlueprintName = "FirebaseVertexAIIntegration" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</TestableReference> | ||
</Testables> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "FirebaseVertexAIIntegration" | ||
BuildableName = "FirebaseVertexAIIntegration" | ||
BlueprintName = "FirebaseVertexAIIntegration" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |