Skip to content

Commit f946592

Browse files
authored
[Vertex AI] Add internal apiVersion parameter to RequestOptions (#14356)
1 parent 28f057d commit f946592

File tree

6 files changed

+145
-4
lines changed

6 files changed

+145
-4
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
/// Versions of the Vertex AI in Firebase server API.
16+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
17+
// TODO(#14405): Make `APIVersion`, `v1` and `v1beta` public in Firebase 12.
18+
struct APIVersion {
19+
/// The stable channel for version 1 of the API.
20+
static let v1 = APIVersion(versionIdentifier: "v1")
21+
22+
/// The beta channel for version 1 of the API.
23+
static let v1beta = APIVersion(versionIdentifier: "v1beta")
24+
25+
let versionIdentifier: String
26+
27+
init(versionIdentifier: String) {
28+
self.versionIdentifier = versionIdentifier
29+
}
30+
}

FirebaseVertexAI/Sources/GenerativeAIRequest.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,30 @@ protocol GenerativeAIRequest: Encodable {
2525

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

3334
/// The API version to use in requests to the backend.
34-
let apiVersion = "v1beta"
35+
let apiVersion: String
3536

3637
/// Initializes a request options object.
3738
///
3839
/// - Parameters:
39-
/// - timeout The request’s timeout interval in seconds; defaults to 180 seconds.
40-
public init(timeout: TimeInterval = 180.0) {
40+
/// - timeout: The request’s timeout interval in seconds; defaults to 180 seconds.
41+
/// - apiVersion: The API version to use in requests to the backend.
42+
init(timeout: TimeInterval = 180.0, apiVersion: APIVersion) {
4143
self.timeout = timeout
44+
self.apiVersion = apiVersion.versionIdentifier
45+
}
46+
47+
/// Initializes a request options object.
48+
///
49+
/// - Parameters:
50+
/// - timeout: The request’s timeout interval in seconds; defaults to 180 seconds.
51+
public init(timeout: TimeInterval = 180.0) {
52+
self.init(timeout: timeout, apiVersion: .v1beta)
4253
}
4354
}

FirebaseVertexAI/Tests/TestApp/Tests/Integration/IntegrationTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import FirebaseVertexAI
1919
import VertexAITestApp
2020
import XCTest
2121

22+
// TODO(#14405): Migrate to Swift Testing and parameterize tests to run on both `v1` and `v1beta`.
2223
final class IntegrationTests: XCTestCase {
2324
// Set temperature, topP and topK to lowest allowed values to make responses more deterministic.
2425
let generationConfig = GenerationConfig(
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
import XCTest
16+
17+
@testable import FirebaseVertexAI
18+
19+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
20+
final class APIVersionTests: XCTestCase {
21+
func testInitialize_v1() {
22+
let apiVersion: APIVersion = .v1
23+
24+
XCTAssertEqual(apiVersion.versionIdentifier, "v1")
25+
}
26+
27+
func testInitialize_v1beta() {
28+
let apiVersion: APIVersion = .v1beta
29+
30+
XCTAssertEqual(apiVersion.versionIdentifier, "v1beta")
31+
}
32+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
import XCTest
16+
17+
@testable import FirebaseVertexAI
18+
19+
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
20+
final class RequestOptionsTests: XCTestCase {
21+
let defaultTimeout: TimeInterval = 180.0
22+
let defaultAPIVersion = APIVersion.v1beta.versionIdentifier
23+
24+
func testInitialize_defaultValues() {
25+
let requestOptions = RequestOptions()
26+
27+
XCTAssertEqual(requestOptions.timeout, defaultTimeout)
28+
XCTAssertEqual(requestOptions.apiVersion, defaultAPIVersion)
29+
}
30+
31+
func testInitialize_timeout() {
32+
let expectedTimeout = 60.0
33+
34+
let requestOptions = RequestOptions(timeout: expectedTimeout)
35+
36+
XCTAssertEqual(requestOptions.timeout, expectedTimeout)
37+
XCTAssertEqual(requestOptions.apiVersion, defaultAPIVersion)
38+
}
39+
40+
func testInitialize_apiVersion_v1() {
41+
let requestOptions = RequestOptions(apiVersion: .v1)
42+
43+
XCTAssertEqual(requestOptions.timeout, defaultTimeout)
44+
XCTAssertEqual(requestOptions.apiVersion, APIVersion.v1.versionIdentifier)
45+
}
46+
47+
func testInitialize_apiVersion_v1beta() {
48+
let requestOptions = RequestOptions(apiVersion: .v1beta)
49+
50+
XCTAssertEqual(requestOptions.timeout, defaultTimeout)
51+
XCTAssertEqual(requestOptions.apiVersion, APIVersion.v1beta.versionIdentifier)
52+
}
53+
54+
func testInitialize_allOptions() {
55+
let expectedTimeout = 30.0
56+
let expectedAPIVersion = APIVersion.v1
57+
58+
let requestOptions = RequestOptions(timeout: expectedTimeout, apiVersion: expectedAPIVersion)
59+
60+
XCTAssertEqual(requestOptions.timeout, expectedTimeout)
61+
XCTAssertEqual(requestOptions.apiVersion, expectedAPIVersion.versionIdentifier)
62+
}
63+
}

FirebaseVertexAI/Tests/Unit/VertexAIAPITests.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ final class VertexAIAPITests: XCTestCase {
3838
parts: TextPart("Talk like a pirate.")
3939
)
4040

41+
let requestOptions = RequestOptions()
42+
let _ = RequestOptions(timeout: 30.0)
43+
4144
// Instantiate Vertex AI SDK - Default App
4245
let vertexAI = VertexAI.vertexAI()
4346
let _ = VertexAI.vertexAI(location: "my-location")
@@ -70,7 +73,8 @@ final class VertexAIAPITests: XCTestCase {
7073
modelName: "gemini-1.0-pro",
7174
generationConfig: config, // Optional
7275
safetySettings: filters, // Optional
73-
systemInstruction: systemInstruction // Optional
76+
systemInstruction: systemInstruction, // Optional
77+
requestOptions: requestOptions // Optional
7478
)
7579

7680
// Full Typed Usage

0 commit comments

Comments
 (0)