Skip to content

Commit 742d7b0

Browse files
committed
[Vertex AI] Bypass proxy for testing against staging
1 parent 16381c7 commit 742d7b0

File tree

7 files changed

+24
-20
lines changed

7 files changed

+24
-20
lines changed

FirebaseVertexAI/Sources/Constants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Foundation
1717
/// Constants associated with the Vertex AI for Firebase SDK.
1818
enum Constants {
1919
/// The Vertex AI backend endpoint URL.
20-
static let baseURL = "https://firebasevertexai.googleapis.com"
20+
static let baseURL = "staging-aiplatform.sandbox.googleapis.com"
2121

2222
/// The base reverse-DNS name for `NSError` or `CustomNSError` error domains.
2323
///

FirebaseVertexAI/Sources/CountTokensRequest.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Foundation
1717
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
1818
struct CountTokensRequest {
1919
let model: String
20+
let location: String
2021

2122
let contents: [ModelContent]
2223
let systemInstruction: ModelContent?
@@ -31,7 +32,9 @@ extension CountTokensRequest: GenerativeAIRequest {
3132
typealias Response = CountTokensResponse
3233

3334
var url: URL {
34-
URL(string: "\(Constants.baseURL)/\(options.apiVersion)/\(model):countTokens")!
35+
URL(
36+
string: "https://\(location)-\(Constants.baseURL)/\(options.apiVersion)/\(model):countTokens"
37+
)!
3538
}
3639
}
3740

FirebaseVertexAI/Sources/GenerateContentRequest.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Foundation
1818
struct GenerateContentRequest {
1919
/// Model name.
2020
let model: String
21+
let location: String
2122
let contents: [ModelContent]
2223
let generationConfig: GenerationConfig?
2324
let safetySettings: [SafetySetting]?
@@ -45,7 +46,7 @@ extension GenerateContentRequest: GenerativeAIRequest {
4546
typealias Response = GenerateContentResponse
4647

4748
var url: URL {
48-
let modelURL = "\(Constants.baseURL)/\(options.apiVersion)/\(model)"
49+
let modelURL = "https://\(location)-\(Constants.baseURL)/\(options.apiVersion)/\(model)"
4950
if isStreaming {
5051
return URL(string: "\(modelURL):streamGenerateContent?alt=sse")!
5152
} else {

FirebaseVertexAI/Sources/GenerativeAIRequest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public struct RequestOptions {
3131
let timeout: TimeInterval
3232

3333
/// The API version to use in requests to the backend.
34-
let apiVersion = "v1beta"
34+
let apiVersion = "v1beta1"
3535

3636
/// Initializes a request options object.
3737
///

FirebaseVertexAI/Sources/GenerativeAIService.swift

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -180,28 +180,20 @@ struct GenerativeAIService {
180180
private func urlRequest<T: GenerativeAIRequest>(request: T) async throws -> URLRequest {
181181
var urlRequest = URLRequest(url: request.url)
182182
urlRequest.httpMethod = "POST"
183-
urlRequest.setValue(apiKey, forHTTPHeaderField: "x-goog-api-key")
183+
guard let accessToken = ProcessInfo.processInfo.environment["GCLOUD_ACCESS_TOKEN"] else {
184+
fatalError("""
185+
Missing access token; run `gcloud auth print-access-token` and add an environment variable \
186+
`GCLOUD_ACCESS_TOKEN` with the printed value.
187+
Note: This value will only be valid for 60 minutes.
188+
""")
189+
}
190+
urlRequest.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
184191
urlRequest.setValue(
185192
"\(GenerativeAIService.languageTag) \(GenerativeAIService.firebaseVersionTag)",
186193
forHTTPHeaderField: "x-goog-api-client"
187194
)
188195
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
189196

190-
if let appCheck {
191-
let tokenResult = await appCheck.getToken(forcingRefresh: false)
192-
urlRequest.setValue(tokenResult.token, forHTTPHeaderField: "X-Firebase-AppCheck")
193-
if let error = tokenResult.error {
194-
VertexLog.error(
195-
code: .appCheckTokenFetchFailed,
196-
"Failed to fetch AppCheck token. Error: \(error)"
197-
)
198-
}
199-
}
200-
201-
if let auth, let authToken = try await auth.getToken(forcingRefresh: false) {
202-
urlRequest.setValue("Firebase \(authToken)", forHTTPHeaderField: "Authorization")
203-
}
204-
205197
let encoder = JSONEncoder()
206198
encoder.keyEncodingStrategy = .convertToSnakeCase
207199
urlRequest.httpBody = try encoder.encode(request)

FirebaseVertexAI/Sources/GenerativeModel.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public final class GenerativeModel {
2626
/// The backing service responsible for sending and receiving model requests to the backend.
2727
let generativeAIService: GenerativeAIService
2828

29+
let location: String
30+
2931
/// Configuration parameters used for the MultiModalModel.
3032
let generationConfig: GenerationConfig?
3133

@@ -61,6 +63,7 @@ public final class GenerativeModel {
6163
init(name: String,
6264
projectID: String,
6365
apiKey: String,
66+
location: String = "us-central1",
6467
generationConfig: GenerationConfig? = nil,
6568
safetySettings: [SafetySetting]? = nil,
6669
tools: [Tool]?,
@@ -78,6 +81,7 @@ public final class GenerativeModel {
7881
auth: auth,
7982
urlSession: urlSession
8083
)
84+
self.location = location
8185
self.generationConfig = generationConfig
8286
self.safetySettings = safetySettings
8387
self.tools = tools
@@ -125,6 +129,7 @@ public final class GenerativeModel {
125129
try content.throwIfError()
126130
let response: GenerateContentResponse
127131
let generateContentRequest = GenerateContentRequest(model: modelResourceName,
132+
location: location,
128133
contents: content,
129134
generationConfig: generationConfig,
130135
safetySettings: safetySettings,
@@ -182,6 +187,7 @@ public final class GenerativeModel {
182187
-> AsyncThrowingStream<GenerateContentResponse, Error> {
183188
try content.throwIfError()
184189
let generateContentRequest = GenerateContentRequest(model: modelResourceName,
190+
location: location,
185191
contents: content,
186192
generationConfig: generationConfig,
187193
safetySettings: safetySettings,
@@ -253,6 +259,7 @@ public final class GenerativeModel {
253259
public func countTokens(_ content: [ModelContent]) async throws -> CountTokensResponse {
254260
let countTokensRequest = CountTokensRequest(
255261
model: modelResourceName,
262+
location: location,
256263
contents: content,
257264
systemInstruction: systemInstruction,
258265
tools: tools,

FirebaseVertexAI/Sources/VertexAI.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public class VertexAI {
9393
name: modelResourceName(modelName: modelName),
9494
projectID: projectID,
9595
apiKey: apiKey,
96+
location: location,
9697
generationConfig: generationConfig,
9798
safetySettings: safetySettings,
9899
tools: tools,

0 commit comments

Comments
 (0)