Skip to content

Commit 90cf519

Browse files
committed
Allow for a simpler way to set system instructions
Signed-off-by: Peter Friese <peter@peterfriese.de>
1 parent fb197c3 commit 90cf519

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Sources/GoogleAI/GenerativeModel.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,40 @@ public final class GenerativeModel {
4545
/// Configuration parameters for sending requests to the backend.
4646
let requestOptions: RequestOptions
4747

48+
/// Initializes a new remote model with the given parameters.
49+
///
50+
/// - Parameters:
51+
/// - name: The name of the model to use, e.g., `"gemini-1.5-pro-latest"`; see
52+
/// [Gemini models](https://ai.google.dev/models/gemini) for a list of supported model names.
53+
/// - apiKey: The API key for your project.
54+
/// - generationConfig: The content generation parameters your model should use.
55+
/// - safetySettings: A value describing what types of harmful content your model should allow.
56+
/// - 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.
59+
/// - toolConfig: Tool configuration for any `Tool` specified in the request.
60+
/// - requestOptions Configuration parameters for sending requests to the backend.
61+
public convenience init(name: String,
62+
apiKey: String,
63+
generationConfig: GenerationConfig? = nil,
64+
safetySettings: [SafetySetting]? = nil,
65+
tools: [Tool]? = nil,
66+
toolConfig: ToolConfig? = nil,
67+
systemInstruction: any ThrowingPartsRepresentable...,
68+
requestOptions: RequestOptions = RequestOptions()) {
69+
self.init(
70+
name: name,
71+
apiKey: apiKey,
72+
generationConfig: generationConfig,
73+
safetySettings: safetySettings,
74+
tools: tools,
75+
toolConfig: toolConfig,
76+
systemInstruction: try? ModelContent(role: "system", parts: systemInstruction),
77+
requestOptions: requestOptions,
78+
urlSession: .shared
79+
)
80+
}
81+
4882
/// Initializes a new remote model with the given parameters.
4983
///
5084
/// - Parameters:

Tests/GoogleAITests/GoogleAITests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ final class GoogleGenerativeAITests: XCTestCase {
5050
systemInstruction: systemInstruction
5151
)
5252

53+
let _ = GenerativeModel(
54+
name: "gemini-1.5-pro-latest",
55+
apiKey: "API_KEY",
56+
systemInstruction: "Talk like a pirate"
57+
)
58+
5359
// All arguments passed.
5460
let genAI = GenerativeModel(name: "gemini-1.5-pro-latest",
5561
apiKey: "API_KEY",

0 commit comments

Comments
 (0)