Skip to content

Commit 3d26504

Browse files
authored
Add system instruction support (#129)
1 parent 2063447 commit 3d26504

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Sources/GoogleAI/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

Sources/GoogleAI/GenerativeModel.swift

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

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

@@ -51,6 +54,8 @@ public final class GenerativeModel {
5154
/// - generationConfig: The content generation parameters your model should use.
5255
/// - safetySettings: A value describing what types of harmful content your model should allow.
5356
/// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
57+
/// - systemInstruction: Instructions that direct the model to behave a certain way; currently
58+
/// only text content is supported.
5459
/// - toolConfig: Tool configuration for any `Tool` specified in the request.
5560
/// - requestOptions Configuration parameters for sending requests to the backend.
5661
public convenience init(name: String,
@@ -59,6 +64,7 @@ public final class GenerativeModel {
5964
safetySettings: [SafetySetting]? = nil,
6065
tools: [Tool]? = nil,
6166
toolConfig: ToolConfig? = nil,
67+
systemInstruction: ModelContent? = nil,
6268
requestOptions: RequestOptions = RequestOptions()) {
6369
self.init(
6470
name: name,
@@ -67,6 +73,7 @@ public final class GenerativeModel {
6773
safetySettings: safetySettings,
6874
tools: tools,
6975
toolConfig: toolConfig,
76+
systemInstruction: systemInstruction,
7077
requestOptions: requestOptions,
7178
urlSession: .shared
7279
)
@@ -79,6 +86,7 @@ public final class GenerativeModel {
7986
safetySettings: [SafetySetting]? = nil,
8087
tools: [Tool]? = nil,
8188
toolConfig: ToolConfig? = nil,
89+
systemInstruction: ModelContent? = nil,
8290
requestOptions: RequestOptions = RequestOptions(),
8391
urlSession: URLSession) {
8492
modelResourceName = GenerativeModel.modelResourceName(name: name)
@@ -87,6 +95,7 @@ public final class GenerativeModel {
8795
self.safetySettings = safetySettings
8896
self.tools = tools
8997
self.toolConfig = toolConfig
98+
self.systemInstruction = systemInstruction
9099
self.requestOptions = requestOptions
91100

92101
Logging.default.info("""
@@ -134,6 +143,7 @@ public final class GenerativeModel {
134143
safetySettings: safetySettings,
135144
tools: tools,
136145
toolConfig: toolConfig,
146+
systemInstruction: systemInstruction,
137147
isStreaming: false,
138148
options: requestOptions)
139149
response = try await generativeAIService.loadRequest(request: generateContentRequest)
@@ -207,6 +217,7 @@ public final class GenerativeModel {
207217
safetySettings: safetySettings,
208218
tools: tools,
209219
toolConfig: toolConfig,
220+
systemInstruction: systemInstruction,
210221
isStreaming: true,
211222
options: requestOptions)
212223

Tests/GoogleAITests/GoogleAITests.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,24 @@ final class GoogleGenerativeAITests: XCTestCase {
3030
maxOutputTokens: 256,
3131
stopSequences: ["..."])
3232
let filters = [SafetySetting(harmCategory: .dangerousContent, threshold: .blockOnlyHigh)]
33+
let systemInstruction = ModelContent(role: "system", parts: [.text("Talk like a pirate.")])
3334

3435
// Permutations without optional arguments.
3536
let _ = GenerativeModel(name: "gemini-1.0-pro", apiKey: "API_KEY")
3637
let _ = GenerativeModel(name: "gemini-1.0-pro", apiKey: "API_KEY", safetySettings: filters)
3738
let _ = GenerativeModel(name: "gemini-1.0-pro", apiKey: "API_KEY", generationConfig: config)
39+
let _ = GenerativeModel(
40+
name: "gemini-1.0-pro",
41+
apiKey: "API_KEY",
42+
systemInstruction: systemInstruction
43+
)
3844

3945
// All arguments passed.
4046
let genAI = GenerativeModel(name: "gemini-1.0-pro",
4147
apiKey: "API_KEY",
4248
generationConfig: config, // Optional
43-
safetySettings: filters // Optional
49+
safetySettings: filters, // Optional
50+
systemInstruction: systemInstruction // Optional
4451
)
4552
// Full Typed Usage
4653
let pngData = Data() // ....

0 commit comments

Comments
 (0)