Skip to content

Commit

Permalink
[Vertex AI] Add internal apiVersion parameter to RequestOptions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewheard authored Jan 30, 2025
1 parent 28f057d commit f946592
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 4 deletions.
30 changes: 30 additions & 0 deletions FirebaseVertexAI/Sources/APIVersion.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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.

/// Versions of the Vertex AI in Firebase server API.
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
// TODO(#14405): Make `APIVersion`, `v1` and `v1beta` public in Firebase 12.
struct APIVersion {
/// The stable channel for version 1 of the API.
static let v1 = APIVersion(versionIdentifier: "v1")

/// The beta channel for version 1 of the API.
static let v1beta = APIVersion(versionIdentifier: "v1beta")

let versionIdentifier: String

init(versionIdentifier: String) {
self.versionIdentifier = versionIdentifier
}
}
17 changes: 14 additions & 3 deletions FirebaseVertexAI/Sources/GenerativeAIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,30 @@ protocol GenerativeAIRequest: Encodable {

/// Configuration parameters for sending requests to the backend.
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
// TODO(#14405): Make the `apiVersion` constructor public in Firebase 12 with a default of `.v1`.
public struct RequestOptions {
/// The request’s timeout interval in seconds; if not specified uses the default value for a
/// `URLRequest`.
let timeout: TimeInterval

/// The API version to use in requests to the backend.
let apiVersion = "v1beta"
let apiVersion: String

/// Initializes a request options object.
///
/// - Parameters:
/// - timeout The request’s timeout interval in seconds; defaults to 180 seconds.
public init(timeout: TimeInterval = 180.0) {
/// - timeout: The request’s timeout interval in seconds; defaults to 180 seconds.
/// - apiVersion: The API version to use in requests to the backend.
init(timeout: TimeInterval = 180.0, apiVersion: APIVersion) {
self.timeout = timeout
self.apiVersion = apiVersion.versionIdentifier
}

/// Initializes a request options object.
///
/// - Parameters:
/// - timeout: The request’s timeout interval in seconds; defaults to 180 seconds.
public init(timeout: TimeInterval = 180.0) {
self.init(timeout: timeout, apiVersion: .v1beta)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import FirebaseVertexAI
import VertexAITestApp
import XCTest

// TODO(#14405): Migrate to Swift Testing and parameterize tests to run on both `v1` and `v1beta`.
final class IntegrationTests: XCTestCase {
// Set temperature, topP and topK to lowest allowed values to make responses more deterministic.
let generationConfig = GenerationConfig(
Expand Down
32 changes: 32 additions & 0 deletions FirebaseVertexAI/Tests/Unit/APIVersionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 XCTest

@testable import FirebaseVertexAI

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
final class APIVersionTests: XCTestCase {
func testInitialize_v1() {
let apiVersion: APIVersion = .v1

XCTAssertEqual(apiVersion.versionIdentifier, "v1")
}

func testInitialize_v1beta() {
let apiVersion: APIVersion = .v1beta

XCTAssertEqual(apiVersion.versionIdentifier, "v1beta")
}
}
63 changes: 63 additions & 0 deletions FirebaseVertexAI/Tests/Unit/RequestOptionsTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 XCTest

@testable import FirebaseVertexAI

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
final class RequestOptionsTests: XCTestCase {
let defaultTimeout: TimeInterval = 180.0
let defaultAPIVersion = APIVersion.v1beta.versionIdentifier

func testInitialize_defaultValues() {
let requestOptions = RequestOptions()

XCTAssertEqual(requestOptions.timeout, defaultTimeout)
XCTAssertEqual(requestOptions.apiVersion, defaultAPIVersion)
}

func testInitialize_timeout() {
let expectedTimeout = 60.0

let requestOptions = RequestOptions(timeout: expectedTimeout)

XCTAssertEqual(requestOptions.timeout, expectedTimeout)
XCTAssertEqual(requestOptions.apiVersion, defaultAPIVersion)
}

func testInitialize_apiVersion_v1() {
let requestOptions = RequestOptions(apiVersion: .v1)

XCTAssertEqual(requestOptions.timeout, defaultTimeout)
XCTAssertEqual(requestOptions.apiVersion, APIVersion.v1.versionIdentifier)
}

func testInitialize_apiVersion_v1beta() {
let requestOptions = RequestOptions(apiVersion: .v1beta)

XCTAssertEqual(requestOptions.timeout, defaultTimeout)
XCTAssertEqual(requestOptions.apiVersion, APIVersion.v1beta.versionIdentifier)
}

func testInitialize_allOptions() {
let expectedTimeout = 30.0
let expectedAPIVersion = APIVersion.v1

let requestOptions = RequestOptions(timeout: expectedTimeout, apiVersion: expectedAPIVersion)

XCTAssertEqual(requestOptions.timeout, expectedTimeout)
XCTAssertEqual(requestOptions.apiVersion, expectedAPIVersion.versionIdentifier)
}
}
6 changes: 5 additions & 1 deletion FirebaseVertexAI/Tests/Unit/VertexAIAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ final class VertexAIAPITests: XCTestCase {
parts: TextPart("Talk like a pirate.")
)

let requestOptions = RequestOptions()
let _ = RequestOptions(timeout: 30.0)

// Instantiate Vertex AI SDK - Default App
let vertexAI = VertexAI.vertexAI()
let _ = VertexAI.vertexAI(location: "my-location")
Expand Down Expand Up @@ -70,7 +73,8 @@ final class VertexAIAPITests: XCTestCase {
modelName: "gemini-1.0-pro",
generationConfig: config, // Optional
safetySettings: filters, // Optional
systemInstruction: systemInstruction // Optional
systemInstruction: systemInstruction, // Optional
requestOptions: requestOptions // Optional
)

// Full Typed Usage
Expand Down

0 comments on commit f946592

Please sign in to comment.