@@ -36,9 +36,50 @@ public final class GenerativeModel {
36
36
/// A list of tools the model may use to generate the next response.
37
37
let tools : [ Tool ] ?
38
38
39
+ /// Tool configuration for any `Tool` specified in the request.
40
+ let toolConfig : ToolConfig ?
41
+
42
+ /// Instructions that direct the model to behave a certain way.
43
+ let systemInstruction : ModelContent ?
44
+
39
45
/// Configuration parameters for sending requests to the backend.
40
46
let requestOptions : RequestOptions
41
47
48
+ /// Initializes a new remote model with the given parameters.
49
+ ///
50
+ /// - Parameters:
51
+ /// - name: The name of the model to use, for example `"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, for example
59
+ /// `ModelContent(role: "system", parts: "You are a cat. Your name is Neko.")`.
60
+ /// - toolConfig: Tool configuration for any `Tool` specified in the request.
61
+ /// - requestOptions Configuration parameters for sending requests to the backend.
62
+ public convenience init ( name: String ,
63
+ apiKey: String ,
64
+ generationConfig: GenerationConfig ? = nil ,
65
+ safetySettings: [ SafetySetting ] ? = nil ,
66
+ tools: [ Tool ] ? = nil ,
67
+ toolConfig: ToolConfig ? = nil ,
68
+ systemInstruction: ModelContent ? = nil ,
69
+ requestOptions: RequestOptions = RequestOptions ( ) ) {
70
+ self . init (
71
+ name: name,
72
+ apiKey: apiKey,
73
+ generationConfig: generationConfig,
74
+ safetySettings: safetySettings,
75
+ tools: tools,
76
+ toolConfig: toolConfig,
77
+ systemInstruction: systemInstruction,
78
+ requestOptions: requestOptions,
79
+ urlSession: . shared
80
+ )
81
+ }
82
+
42
83
/// Initializes a new remote model with the given parameters.
43
84
///
44
85
/// - Parameters:
@@ -48,19 +89,29 @@ public final class GenerativeModel {
48
89
/// - generationConfig: The content generation parameters your model should use.
49
90
/// - safetySettings: A value describing what types of harmful content your model should allow.
50
91
/// - tools: A list of ``Tool`` objects that the model may use to generate the next response.
92
+ /// - systemInstruction: Instructions that direct the model to behave a certain way; currently
93
+ /// only text content is supported, e.g., "You are a cat. Your name is Neko."
94
+ /// - toolConfig: Tool configuration for any `Tool` specified in the request.
51
95
/// - requestOptions Configuration parameters for sending requests to the backend.
52
96
public convenience init ( name: String ,
53
97
apiKey: String ,
54
98
generationConfig: GenerationConfig ? = nil ,
55
99
safetySettings: [ SafetySetting ] ? = nil ,
56
100
tools: [ Tool ] ? = nil ,
101
+ toolConfig: ToolConfig ? = nil ,
102
+ systemInstruction: String ... ,
57
103
requestOptions: RequestOptions = RequestOptions ( ) ) {
58
104
self . init (
59
105
name: name,
60
106
apiKey: apiKey,
61
107
generationConfig: generationConfig,
62
108
safetySettings: safetySettings,
63
109
tools: tools,
110
+ toolConfig: toolConfig,
111
+ systemInstruction: ModelContent (
112
+ role: " system " ,
113
+ parts: systemInstruction. map { ModelContent . Part. text ( $0) }
114
+ ) ,
64
115
requestOptions: requestOptions,
65
116
urlSession: . shared
66
117
)
@@ -72,13 +123,17 @@ public final class GenerativeModel {
72
123
generationConfig: GenerationConfig ? = nil ,
73
124
safetySettings: [ SafetySetting ] ? = nil ,
74
125
tools: [ Tool ] ? = nil ,
126
+ toolConfig: ToolConfig ? = nil ,
127
+ systemInstruction: ModelContent ? = nil ,
75
128
requestOptions: RequestOptions = RequestOptions ( ) ,
76
129
urlSession: URLSession ) {
77
130
modelResourceName = GenerativeModel . modelResourceName ( name: name)
78
131
generativeAIService = GenerativeAIService ( apiKey: apiKey, urlSession: urlSession)
79
132
self . generationConfig = generationConfig
80
133
self . safetySettings = safetySettings
81
134
self . tools = tools
135
+ self . toolConfig = toolConfig
136
+ self . systemInstruction = systemInstruction
82
137
self . requestOptions = requestOptions
83
138
84
139
Logging . default. info ( """
@@ -99,7 +154,7 @@ public final class GenerativeModel {
99
154
/// [zero-shot](https://developers.google.com/machine-learning/glossary/generative#zero-shot-prompting)
100
155
/// or "direct" prompts. For
101
156
/// [few-shot](https://developers.google.com/machine-learning/glossary/generative#few-shot-prompting)
102
- /// prompts, see `` generateContent(_:)-58rm0` `.
157
+ /// prompts, see `generateContent(_ content: @autoclosure () throws -> [ModelContent]) `.
103
158
///
104
159
/// - Parameter content: The input(s) given to the model as a prompt (see
105
160
/// ``ThrowingPartsRepresentable``
@@ -125,6 +180,8 @@ public final class GenerativeModel {
125
180
generationConfig: generationConfig,
126
181
safetySettings: safetySettings,
127
182
tools: tools,
183
+ toolConfig: toolConfig,
184
+ systemInstruction: systemInstruction,
128
185
isStreaming: false ,
129
186
options: requestOptions)
130
187
response = try await generativeAIService. loadRequest ( request: generateContentRequest)
@@ -156,7 +213,7 @@ public final class GenerativeModel {
156
213
/// [zero-shot](https://developers.google.com/machine-learning/glossary/generative#zero-shot-prompting)
157
214
/// or "direct" prompts. For
158
215
/// [few-shot](https://developers.google.com/machine-learning/glossary/generative#few-shot-prompting)
159
- /// prompts, see `` generateContent(_:)-58rm0` `.
216
+ /// prompts, see `generateContent(_ content: @autoclosure () throws -> [ModelContent]) `.
160
217
///
161
218
/// - Parameter content: The input(s) given to the model as a prompt (see
162
219
/// ``ThrowingPartsRepresentable``
@@ -197,6 +254,8 @@ public final class GenerativeModel {
197
254
generationConfig: generationConfig,
198
255
safetySettings: safetySettings,
199
256
tools: tools,
257
+ toolConfig: toolConfig,
258
+ systemInstruction: systemInstruction,
200
259
isStreaming: true ,
201
260
options: requestOptions)
202
261
@@ -243,7 +302,7 @@ public final class GenerativeModel {
243
302
/// [zero-shot](https://developers.google.com/machine-learning/glossary/generative#zero-shot-prompting)
244
303
/// or "direct" prompts. For
245
304
/// [few-shot](https://developers.google.com/machine-learning/glossary/generative#few-shot-prompting)
246
- /// input, see `` countTokens(_:)-9spwl` `.
305
+ /// input, see `countTokens(_ content: @autoclosure () throws -> [ModelContent]) `.
247
306
///
248
307
/// - Parameter content: The input(s) given to the model as a prompt (see
249
308
/// ``ThrowingPartsRepresentable``
@@ -301,7 +360,7 @@ public final class GenerativeModel {
301
360
}
302
361
}
303
362
304
- /// See `` GenerativeModel/ countTokens(_:)-9spwl` `.
363
+ /// An error thrown in ` GenerativeModel. countTokens(_:)`.
305
364
@available ( iOS 15 . 0 , macOS 11 . 0 , macCatalyst 15 . 0 , * )
306
365
public enum CountTokensError : Error {
307
366
case internalError( underlying: Error )
0 commit comments