Skip to content

Commit c9f1c41

Browse files
authored
[Vertex AI] Set ModelContent.role to nil in system instructions (#14558)
1 parent 42baeb3 commit c9f1c41

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

FirebaseVertexAI/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
- [feature] The Vertex AI Sample App now includes an image generation example.
44
- [changed] The Vertex AI Sample App is now part of the
55
[quickstart-ios repo](https://github.com/firebase/quickstart-ios/tree/main/vertexai).
6+
- [changed] The `role` in system instructions is now ignored; no code changes
7+
are required. (#14558)
68

79
# 11.9.0
810
- [feature] **Public Preview**: Added support for generating images using the

FirebaseVertexAI/Sources/GenerativeModel.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ public final class GenerativeModel: Sendable {
8181
self.safetySettings = safetySettings
8282
self.tools = tools
8383
self.toolConfig = toolConfig
84-
self.systemInstruction = systemInstruction
84+
self.systemInstruction = systemInstruction.map {
85+
// The `role` defaults to "user" but is ignored in system instructions. However, it is
86+
// erroneously counted towards the prompt and total token count in `countTokens` when using
87+
// the Developer API backend; set to `nil` to avoid token count discrepancies between
88+
// `countTokens` and `generateContent`.
89+
ModelContent(role: nil, parts: $0.parts)
90+
}
8591
self.requestOptions = requestOptions
8692

8793
if VertexLog.additionalLoggingEnabled() {

FirebaseVertexAI/Tests/Unit/VertexComponentTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,15 @@ class VertexComponentTests: XCTestCase {
218218
let app = try XCTUnwrap(VertexComponentTests.app)
219219
let vertex = VertexAI.vertexAI(app: app, location: location)
220220
let modelResourceName = vertex.modelResourceName(modelName: modelName)
221+
let expectedSystemInstruction = ModelContent(role: nil, parts: systemInstruction.parts)
221222

222223
let generativeModel = vertex.generativeModel(
223224
modelName: modelName,
224225
systemInstruction: systemInstruction
225226
)
226227

227228
XCTAssertEqual(generativeModel.modelResourceName, modelResourceName)
228-
XCTAssertEqual(generativeModel.systemInstruction, systemInstruction)
229+
XCTAssertEqual(generativeModel.systemInstruction, expectedSystemInstruction)
229230
XCTAssertEqual(generativeModel.apiConfig, VertexAI.defaultVertexAIAPIConfig)
230231
}
231232

@@ -237,14 +238,15 @@ class VertexComponentTests: XCTestCase {
237238
)
238239
let vertex = VertexAI.vertexAI(app: app, location: nil, apiConfig: apiConfig)
239240
let modelResourceName = vertex.modelResourceName(modelName: modelName)
241+
let expectedSystemInstruction = ModelContent(role: nil, parts: systemInstruction.parts)
240242

241243
let generativeModel = vertex.generativeModel(
242244
modelName: modelName,
243245
systemInstruction: systemInstruction
244246
)
245247

246248
XCTAssertEqual(generativeModel.modelResourceName, modelResourceName)
247-
XCTAssertEqual(generativeModel.systemInstruction, systemInstruction)
249+
XCTAssertEqual(generativeModel.systemInstruction, expectedSystemInstruction)
248250
XCTAssertEqual(generativeModel.apiConfig, apiConfig)
249251
}
250252
}

0 commit comments

Comments
 (0)