Skip to content

Commit 9e19563

Browse files
authored
[Vertex AI] Update logging details from Google AI SDK (#12933)
1 parent e690fcd commit 9e19563

File tree

5 files changed

+55
-29
lines changed

5 files changed

+55
-29
lines changed

FirebaseVertexAI/Sources/GenerateContentResponse.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public struct GenerateContentResponse {
4242
/// The response's content as text, if it exists.
4343
public var text: String? {
4444
guard let candidate = candidates.first else {
45-
Logging.default.error("Could not get text from a response that had no candidates.")
45+
Logging.default
46+
.error("[FirebaseVertexAI] Could not get text from a response that had no candidates.")
4647
return nil
4748
}
4849
let textValues: [String] = candidate.content.parts.compactMap { part in
@@ -52,7 +53,8 @@ public struct GenerateContentResponse {
5253
return text
5354
}
5455
guard textValues.count > 0 else {
55-
Logging.default.error("Could not get a text part from the first candidate.")
56+
Logging.default
57+
.error("[FirebaseVertexAI] Could not get a text part from the first candidate.")
5658
return nil
5759
}
5860
return textValues.joined(separator: " ")
@@ -319,7 +321,7 @@ extension FinishReason: Decodable {
319321
let value = try decoder.singleValueContainer().decode(String.self)
320322
guard let decodedFinishReason = FinishReason(rawValue: value) else {
321323
Logging.default
322-
.error("[GoogleGenerativeAI] Unrecognized FinishReason with value \"\(value)\".")
324+
.error("[FirebaseVertexAI] Unrecognized FinishReason with value \"\(value)\".")
323325
self = .unknown
324326
return
325327
}
@@ -334,7 +336,7 @@ extension PromptFeedback.BlockReason: Decodable {
334336
let value = try decoder.singleValueContainer().decode(String.self)
335337
guard let decodedBlockReason = PromptFeedback.BlockReason(rawValue: value) else {
336338
Logging.default
337-
.error("[GoogleGenerativeAI] Unrecognized BlockReason with value \"\(value)\".")
339+
.error("[FirebaseVertexAI] Unrecognized BlockReason with value \"\(value)\".")
338340
self = .unknown
339341
return
340342
}

FirebaseVertexAI/Sources/GenerativeAIService.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ struct GenerativeAIService {
5656

5757
// Verify the status code is 200
5858
guard response.statusCode == 200 else {
59-
Logging.default.error("[GoogleGenerativeAI] The server responded with an error: \(response)")
59+
Logging.default.error("[FirebaseVertexAI] The server responded with an error: \(response)")
6060
if let responseString = String(data: data, encoding: .utf8) {
61-
Logging.network.error("[GoogleGenerativeAI] Response payload: \(responseString)")
61+
Logging.network.error("[FirebaseVertexAI] Response payload: \(responseString)")
6262
}
6363

6464
throw parseError(responseData: data)
@@ -105,13 +105,13 @@ struct GenerativeAIService {
105105
// Verify the status code is 200
106106
guard response.statusCode == 200 else {
107107
Logging.default
108-
.error("[GoogleGenerativeAI] The server responded with an error: \(response)")
108+
.error("[FirebaseVertexAI] The server responded with an error: \(response)")
109109
var responseBody = ""
110110
for try await line in stream.lines {
111111
responseBody += line + "\n"
112112
}
113113

114-
Logging.network.error("[GoogleGenerativeAI] Response payload: \(responseBody)")
114+
Logging.network.error("[FirebaseVertexAI] Response payload: \(responseBody)")
115115
continuation.finish(throwing: parseError(responseBody: responseBody))
116116

117117
return
@@ -123,7 +123,7 @@ struct GenerativeAIService {
123123
let decoder = JSONDecoder()
124124
decoder.keyDecodingStrategy = .convertFromSnakeCase
125125
for try await line in stream.lines {
126-
Logging.network.debug("[GoogleGenerativeAI] Stream response: \(line)")
126+
Logging.network.debug("[FirebaseVertexAI] Stream response: \(line)")
127127

128128
if line.hasPrefix("data:") {
129129
// We can assume 5 characters since it's utf-8 encoded, removing `data:`.
@@ -176,7 +176,7 @@ struct GenerativeAIService {
176176
urlRequest.setValue(tokenResult.token, forHTTPHeaderField: "X-Firebase-AppCheck")
177177
if let error = tokenResult.error {
178178
Logging.default
179-
.debug("[GoogleGenerativeAI] Failed to fetch AppCheck token. Error: \(error)")
179+
.debug("[FirebaseVertexAI] Failed to fetch AppCheck token. Error: \(error)")
180180
}
181181
}
182182

@@ -200,7 +200,7 @@ struct GenerativeAIService {
200200
guard let response = urlResponse as? HTTPURLResponse else {
201201
Logging.default
202202
.error(
203-
"[GoogleGenerativeAI] Response wasn't an HTTP response, internal error \(urlResponse)"
203+
"[FirebaseVertexAI] Response wasn't an HTTP response, internal error \(urlResponse)"
204204
)
205205
throw NSError(
206206
domain: "com.google.generative-ai",
@@ -248,9 +248,9 @@ struct GenerativeAIService {
248248
return try JSONDecoder().decode(type, from: data)
249249
} catch {
250250
if let json = String(data: data, encoding: .utf8) {
251-
Logging.network.error("[GoogleGenerativeAI] JSON response: \(json)")
251+
Logging.network.error("[FirebaseVertexAI] JSON response: \(json)")
252252
}
253-
Logging.default.error("[GoogleGenerativeAI] Error decoding server JSON: \(error)")
253+
Logging.default.error("[FirebaseVertexAI] Error decoding server JSON: \(error)")
254254
throw error
255255
}
256256
}
@@ -278,7 +278,7 @@ struct GenerativeAIService {
278278
private func printCURLCommand(from request: URLRequest) {
279279
let command = cURLCommand(from: request)
280280
Logging.verbose.debug("""
281-
[GoogleGenerativeAI] Creating request with the equivalent cURL command:
281+
[FirebaseVertexAI] Creating request with the equivalent cURL command:
282282
----- cURL command -----
283283
\(command, privacy: .private)
284284
------------------------

FirebaseVertexAI/Sources/GenerativeModel.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,23 @@ public final class GenerativeModel {
8686
self.systemInstruction = systemInstruction
8787
self.requestOptions = requestOptions
8888

89-
Logging.default.info("""
90-
[GoogleGenerativeAI] Model \(
91-
name,
92-
privacy: .public
93-
) initialized. To enable additional logging, add \
94-
`\(Logging.enableArgumentKey, privacy: .public)` as a launch argument in Xcode.
95-
""")
96-
Logging.verbose.debug("[GoogleGenerativeAI] Verbose logging enabled.")
89+
if Logging.additionalLoggingEnabled() {
90+
if ProcessInfo.processInfo.arguments.contains(Logging.migrationEnableArgumentKey) {
91+
Logging.verbose.debug("""
92+
[FirebaseVertexAI] Verbose logging enabled with the \
93+
\(Logging.migrationEnableArgumentKey, privacy: .public) launch argument; please migrate to \
94+
the \(Logging.enableArgumentKey, privacy: .public) argument to ensure future compatibility.
95+
""")
96+
} else {
97+
Logging.verbose.debug("[FirebaseVertexAI] Verbose logging enabled.")
98+
}
99+
} else {
100+
Logging.default.info("""
101+
[FirebaseVertexAI] To enable additional logging, add \
102+
`\(Logging.enableArgumentKey, privacy: .public)` as a launch argument in Xcode.
103+
""")
104+
}
105+
Logging.default.debug("[FirebaseVertexAI] Model \(name, privacy: .public) initialized.")
97106
}
98107

99108
/// Generates content from String and/or image inputs, given to the model as a prompt, that are

FirebaseVertexAI/Sources/Logging.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ import OSLog
1818
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
1919
struct Logging {
2020
/// Subsystem that should be used for all Loggers.
21-
static let subsystem = "com.google.generative-ai"
21+
static let subsystem = "com.google.firebase.vertex-ai"
2222

2323
/// Default category used for most loggers, unless specialized.
2424
static let defaultCategory = ""
2525

2626
/// The argument required to enable additional logging.
27-
static let enableArgumentKey = "-GoogleGenerativeAIDebugLogEnabled"
27+
static let enableArgumentKey = "-FIRDebugEnabled"
28+
29+
/// The argument required to enable additional logging in the Google AI SDK; used for migration.
30+
///
31+
/// To facillitate migration between the SDKs, this launch argument is also accepted to enable
32+
/// additional logging at this time, though it is expected to be removed in the future.
33+
static let migrationEnableArgumentKey = "-GoogleGenerativeAIDebugLogEnabled"
2834

2935
// No initializer available.
3036
@available(*, unavailable)
@@ -36,7 +42,7 @@ struct Logging {
3642

3743
/// A non default
3844
static var network: Logger = {
39-
if ProcessInfo.processInfo.arguments.contains(enableArgumentKey) {
45+
if additionalLoggingEnabled() {
4046
return Logger(subsystem: subsystem, category: "NetworkResponse")
4147
} else {
4248
// Return a valid logger that's using `OSLog.disabled` as the logger, hiding everything.
@@ -46,11 +52,20 @@ struct Logging {
4652

4753
///
4854
static var verbose: Logger = {
49-
if ProcessInfo.processInfo.arguments.contains(enableArgumentKey) {
55+
if additionalLoggingEnabled() {
5056
return Logger(subsystem: subsystem, category: defaultCategory)
5157
} else {
5258
// Return a valid logger that's using `OSLog.disabled` as the logger, hiding everything.
5359
return Logger(.disabled)
5460
}
5561
}()
62+
63+
/// Returns `true` if additional logging has been enabled via a launch argument.
64+
static func additionalLoggingEnabled() -> Bool {
65+
let arguments = ProcessInfo.processInfo.arguments
66+
if arguments.contains(enableArgumentKey) || arguments.contains(migrationEnableArgumentKey) {
67+
return true
68+
}
69+
return false
70+
}
5671
}

FirebaseVertexAI/Sources/Safety.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ extension SafetyRating.HarmProbability: Codable {
148148
let value = try decoder.singleValueContainer().decode(String.self)
149149
guard let decodedProbability = SafetyRating.HarmProbability(rawValue: value) else {
150150
Logging.default
151-
.error("[GoogleGenerativeAI] Unrecognized HarmProbability with value \"\(value)\".")
151+
.error("[FirebaseVertexAI] Unrecognized HarmProbability with value \"\(value)\".")
152152
self = .unknown
153153
return
154154
}
@@ -169,7 +169,7 @@ extension SafetySetting.HarmCategory: Codable {
169169
let value = try decoder.singleValueContainer().decode(String.self)
170170
guard let decodedCategory = SafetySetting.HarmCategory(rawValue: value) else {
171171
Logging.default
172-
.error("[GoogleGenerativeAI] Unrecognized HarmCategory with value \"\(value)\".")
172+
.error("[FirebaseVertexAI] Unrecognized HarmCategory with value \"\(value)\".")
173173
self = .unknown
174174
return
175175
}
@@ -184,7 +184,7 @@ extension SafetySetting.BlockThreshold: Codable {
184184
let value = try decoder.singleValueContainer().decode(String.self)
185185
guard let decodedThreshold = SafetySetting.BlockThreshold(rawValue: value) else {
186186
Logging.default
187-
.error("[GoogleGenerativeAI] Unrecognized BlockThreshold with value \"\(value)\".")
187+
.error("[FirebaseVertexAI] Unrecognized BlockThreshold with value \"\(value)\".")
188188
self = .unknown
189189
return
190190
}

0 commit comments

Comments
 (0)