Skip to content
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

[Vertex AI] Add simple integration tests #13075

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions .github/workflows/vertexai.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ concurrency:
cancel-in-progress: true

jobs:
spm:
spm-unit:
strategy:
matrix:
target: [iOS, macOS, catalyst]
os: [macos-13]
os: [macos-14]
include:
- os: macos-13
- os: macos-14
xcode: Xcode_15.2
runs-on: ${{ matrix.os }}
env:
Expand All @@ -40,6 +40,35 @@ jobs:
retry_wait_seconds: 120
command: scripts/build.sh FirebaseVertexAIUnit ${{ matrix.target }} spm

spm-integration:
strategy:
matrix:
target: [macOS]
os: [macos-14]
include:
- os: macos-14
xcode: Xcode_15.2
runs-on: ${{ matrix.os }}
env:
FIREBASECI_USE_LATEST_GOOGLEAPPMEASUREMENT: 1
plist_secret: ${{ secrets.GHASecretsGPGPassphrase1 }}
steps:
- uses: actions/checkout@v4
- name: Install Secret GoogleService-Info.plist
run: scripts/decrypt_gha_secret.sh scripts/gha-encrypted/vertexai-integration.plist.gpg \
FirebaseVertexAI/Tests/Integration/Resources/GoogleService-Info.plist "$plist_secret"
- name: Xcode
run: sudo xcode-select -s /Applications/${{ matrix.xcode }}.app/Contents/Developer
- name: Initialize xcodebuild
run: scripts/setup_spm_tests.sh
- uses: nick-fields/retry@v3
with:
timeout_minutes: 120
max_attempts: 3
retry_on: error
retry_wait_seconds: 120
command: scripts/build.sh FirebaseVertexAIIntegration ${{ matrix.target }} spm

sample:
strategy:
matrix:
Expand Down
77 changes: 77 additions & 0 deletions FirebaseVertexAI/Tests/Integration/IntegrationTests.swift
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.
8 changes: 8 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,14 @@ let package = Package(
.headerSearchPath("../../../"),
]
),
.testTarget(
name: "FirebaseVertexAIIntegration",
dependencies: ["FirebaseVertexAI", "SharedTestUtilities"],
path: "FirebaseVertexAI/Tests/Integration",
resources: [
.process("Resources"),
]
),
] + firestoreTargets(),
cLanguageStandard: .c99,
cxxLanguageStandard: CXXLanguageStandard.gnucxx14
Expand Down
Binary file added scripts/gha-encrypted/vertexai-integration.plist.gpg
Binary file not shown.
77 changes: 77 additions & 0 deletions scripts/spm_test_schemes/FirebaseVertexAIIntegration.xcscheme
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>
Loading