Skip to content

Commit 3229b0f

Browse files
committed
Add system instruction support in Vertex AI (#12749)
1 parent 3493ffb commit 3229b0f

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

FirebaseVertexAI/Sources/GenerateContentRequest.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct GenerateContentRequest {
2323
let safetySettings: [SafetySetting]?
2424
let tools: [Tool]?
2525
let toolConfig: ToolConfig?
26+
let systemInstruction: ModelContent?
2627
let isStreaming: Bool
2728
let options: RequestOptions
2829
}
@@ -35,6 +36,7 @@ extension GenerateContentRequest: Encodable {
3536
case safetySettings
3637
case tools
3738
case toolConfig
39+
case systemInstruction
3840
}
3941
}
4042

FirebaseVertexAI/Sources/GenerativeModel.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public final class GenerativeModel {
4040
/// Tool configuration for any `Tool` specified in the request.
4141
let toolConfig: ToolConfig?
4242

43+
/// Instructions that direct the model to behave a certain way.
44+
let systemInstruction: ModelContent?
45+
4346
/// Configuration parameters for sending requests to the backend.
4447
let requestOptions: RequestOptions
4548

@@ -53,6 +56,8 @@ public final class GenerativeModel {
5356
/// - safetySettings: A value describing what types of harmful content your model should allow.
5457
/// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
5558
/// - toolConfig: Tool configuration for any `Tool` specified in the request.
59+
/// - systemInstruction: Instructions that direct the model to behave a certain way; currently
60+
/// only text content is supported.
5661
/// - requestOptions: Configuration parameters for sending requests to the backend.
5762
/// - urlSession: The `URLSession` to use for requests; defaults to `URLSession.shared`.
5863
init(name: String,
@@ -61,6 +66,7 @@ public final class GenerativeModel {
6166
safetySettings: [SafetySetting]? = nil,
6267
tools: [Tool]?,
6368
toolConfig: ToolConfig? = nil,
69+
systemInstruction: ModelContent? = nil,
6470
requestOptions: RequestOptions,
6571
appCheck: AppCheckInterop?,
6672
urlSession: URLSession = .shared) {
@@ -74,6 +80,7 @@ public final class GenerativeModel {
7480
self.safetySettings = safetySettings
7581
self.tools = tools
7682
self.toolConfig = toolConfig
83+
self.systemInstruction = systemInstruction
7784
self.requestOptions = requestOptions
7885

7986
Logging.default.info("""
@@ -121,6 +128,7 @@ public final class GenerativeModel {
121128
safetySettings: safetySettings,
122129
tools: tools,
123130
toolConfig: toolConfig,
131+
systemInstruction: systemInstruction,
124132
isStreaming: false,
125133
options: requestOptions)
126134
response = try await generativeAIService.loadRequest(request: generateContentRequest)
@@ -194,6 +202,7 @@ public final class GenerativeModel {
194202
safetySettings: safetySettings,
195203
tools: tools,
196204
toolConfig: toolConfig,
205+
systemInstruction: systemInstruction,
197206
isStreaming: true,
198207
options: requestOptions)
199208

FirebaseVertexAI/Sources/VertexAI.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ public class VertexAI: NSObject {
5959
/// - safetySettings: A value describing what types of harmful content your model should allow.
6060
/// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
6161
/// - toolConfig: Tool configuration for any `Tool` specified in the request.
62+
/// - systemInstruction: Instructions that direct the model to behave a certain way; currently
63+
/// only text content is supported.
6264
/// - requestOptions: Configuration parameters for sending requests to the backend.
6365
public func generativeModel(modelName: String,
6466
generationConfig: GenerationConfig? = nil,
6567
safetySettings: [SafetySetting]? = nil,
6668
tools: [Tool]? = nil,
6769
toolConfig: ToolConfig? = nil,
70+
systemInstruction: ModelContent? = nil,
6871
requestOptions: RequestOptions = RequestOptions())
6972
-> GenerativeModel {
7073
let modelResourceName = modelResourceName(modelName: modelName, location: location)
@@ -80,6 +83,7 @@ public class VertexAI: NSObject {
8083
safetySettings: safetySettings,
8184
tools: tools,
8285
toolConfig: toolConfig,
86+
systemInstruction: systemInstruction,
8387
requestOptions: requestOptions,
8488
appCheck: appCheck
8589
)

FirebaseVertexAI/Tests/Unit/VertexAIAPITests.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ final class VertexAIAPITests: XCTestCase {
3232
maxOutputTokens: 256,
3333
stopSequences: ["..."])
3434
let filters = [SafetySetting(harmCategory: .dangerousContent, threshold: .blockOnlyHigh)]
35+
let systemInstruction = ModelContent(role: "system", parts: [.text("Talk like a pirate.")])
3536

3637
// Instantiate Vertex AI SDK - Default App
3738
let vertexAI = VertexAI.vertexAI()
@@ -53,11 +54,17 @@ final class VertexAIAPITests: XCTestCase {
5354
generationConfig: config
5455
)
5556

57+
let _ = vertexAI.generativeModel(
58+
modelName: "gemini-1.0-pro",
59+
systemInstruction: systemInstruction
60+
)
61+
5662
// All arguments passed.
5763
let genAI = vertexAI.generativeModel(
5864
modelName: "gemini-1.0-pro",
5965
generationConfig: config, // Optional
60-
safetySettings: filters // Optional
66+
safetySettings: filters, // Optional
67+
systemInstruction: systemInstruction // Optional
6168
)
6269

6370
// Full Typed Usage

0 commit comments

Comments
 (0)