15
15
import Foundation
16
16
17
17
/// A predicted function call returned from the model.
18
- ///
19
- /// REST Docs: https://ai.google.dev/api/rest/v1beta/Content#functioncall
20
- public struct FunctionCall : Equatable , Encodable {
18
+ public struct FunctionCall : Equatable {
21
19
/// The name of the function to call.
22
20
public let name : String
23
21
24
22
/// The function parameters and values.
25
23
public let args : JSONObject
26
24
}
27
25
28
- // REST Docs: https://ai.google.dev/api/rest/v1beta/Tool#schema
29
- public class Schema : Encodable {
26
+ /// A `Schema` object allows the definition of input and output data types.
27
+ ///
28
+ /// These types can be objects, but also primitives and arrays. Represents a select subset of an
29
+ /// [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema).
30
+ public class Schema {
31
+ /// The data type.
30
32
let type : DataType
31
33
34
+ /// The format of the data.
32
35
let format : String ?
33
36
37
+ /// A brief description of the parameter.
34
38
let description : String ?
35
39
40
+ /// Indicates if the value may be null.
36
41
let nullable : Bool ?
37
42
43
+ /// Possible values of the element of type ``DataType/string`` with "enum" format.
38
44
let enumValues : [ String ] ?
39
45
46
+ /// Schema of the elements of type ``DataType/array``.
40
47
let items : Schema ?
41
48
49
+ /// Properties of type ``DataType/object``.
42
50
let properties : [ String : Schema ] ?
43
51
44
- let required : [ String ] ?
52
+ /// Required properties of type ``DataType/object``.
53
+ let requiredProperties : [ String ] ?
54
+
55
+ enum CodingKeys : String , CodingKey {
56
+ case type
57
+ case format
58
+ case description
59
+ case nullable
60
+ case enumValues = " enum "
61
+ case items
62
+ case properties
63
+ case requiredProperties = " required "
64
+ }
45
65
66
+ /// Constructs a new `Schema`.
67
+ ///
68
+ /// - Parameters:
69
+ /// - type: The data type.
70
+ /// - format: The format of the data; used only for primitive datatypes.
71
+ /// Supported formats:
72
+ /// - ``DataType/integer``: int32, int64
73
+ /// - ``DataType/number``: float, double
74
+ /// - ``DataType/string``: enum
75
+ /// - description: A brief description of the parameter; may be formatted as Markdown.
76
+ /// - nullable: Indicates if the value may be null.
77
+ /// - enumValues: Possible values of the element of type ``DataType/string`` with "enum" format.
78
+ /// For example, an enum `Direction` may be defined as `["EAST", NORTH", "SOUTH", "WEST"]`.
79
+ /// - items: Schema of the elements of type ``DataType/array``.
80
+ /// - properties: Properties of type ``DataType/object``.
81
+ /// - requiredProperties: Required properties of type ``DataType/object``.
46
82
public init ( type: DataType , format: String ? = nil , description: String ? = nil ,
47
83
nullable: Bool ? = nil ,
48
84
enumValues: [ String ] ? = nil , items: Schema ? = nil ,
49
85
properties: [ String : Schema ] ? = nil ,
50
- required : [ String ] ? = nil ) {
86
+ requiredProperties : [ String ] ? = nil ) {
51
87
self . type = type
52
88
self . format = format
53
89
self . description = description
54
90
self . nullable = nullable
55
91
self . enumValues = enumValues
56
92
self . items = items
57
93
self . properties = properties
58
- self . required = required
94
+ self . requiredProperties = requiredProperties
59
95
}
60
96
}
61
97
62
- // REST Docs: https://ai.google.dev/api/rest/v1beta/Tool#Type
63
- public enum DataType : String , Encodable {
98
+ /// A data type.
99
+ ///
100
+ /// Contains the set of OpenAPI [data types](https://spec.openapis.org/oas/v3.0.3#data-types).
101
+ public enum DataType : String {
102
+ /// A `String` type.
64
103
case string = " STRING "
104
+
105
+ /// A floating-point number type.
65
106
case number = " NUMBER "
107
+
108
+ /// An integer type.
66
109
case integer = " INTEGER "
110
+
111
+ /// A boolean type.
67
112
case boolean = " BOOLEAN "
113
+
114
+ /// An array type.
68
115
case array = " ARRAY "
116
+
117
+ /// An object type.
69
118
case object = " OBJECT "
70
119
}
71
120
72
- // REST Docs: https://ai.google.dev/api/rest/v1beta/Tool#FunctionDeclaration
121
+ /// Structured representation of a function declaration.
122
+ ///
123
+ /// This `FunctionDeclaration` is a representation of a block of code that can be used as a ``Tool``
124
+ /// by the model and executed by the client.
73
125
public struct FunctionDeclaration {
126
+ /// The name of the function.
74
127
let name : String
75
128
129
+ /// A brief description of the function.
76
130
let description : String
77
131
78
- let parameters : Schema
132
+ /// Describes the parameters to this function; must be of type ``DataType/object``.
133
+ let parameters : Schema ?
79
134
80
- let function : ( ( JSONObject ) async throws -> JSONObject ) ?
81
-
82
- public init ( name: String , description: String , parameters: Schema ,
83
- function: ( ( JSONObject ) async throws -> JSONObject ) ? ) {
135
+ /// Constructs a new `FunctionDeclaration`.
136
+ ///
137
+ /// - Parameters:
138
+ /// - name: The name of the function; must be a-z, A-Z, 0-9, or contain underscores and dashes,
139
+ /// with a maximum length of 63.
140
+ /// - description: A brief description of the function.
141
+ /// - parameters: Describes the parameters to this function; the keys are parameter names and
142
+ /// the values are ``Schema`` objects describing them.
143
+ /// - requiredParameters: A list of required parameters by name.
144
+ public init ( name: String , description: String , parameters: [ String : Schema ] ? ,
145
+ requiredParameters: [ String ] ? ) {
84
146
self . name = name
85
147
self . description = description
86
- self . parameters = parameters
87
- self . function = function
148
+ self . parameters = Schema (
149
+ type: . object,
150
+ properties: parameters,
151
+ requiredProperties: requiredParameters
152
+ )
88
153
}
89
154
}
90
155
91
- // REST Docs: https://ai.google.dev/api/rest/v1beta/Tool
92
- public struct Tool : Encodable {
156
+ /// Helper tools that the model may use to generate response.
157
+ ///
158
+ /// A `Tool` is a piece of code that enables the system to interact with external systems to
159
+ /// perform an action, or set of actions, outside of knowledge and scope of the model.
160
+ public struct Tool {
161
+ /// A list of `FunctionDeclarations` available to the model.
93
162
let functionDeclarations : [ FunctionDeclaration ] ?
94
163
164
+ /// Constructs a new `Tool`.
165
+ ///
166
+ /// - Parameters:
167
+ /// - functionDeclarations: A list of `FunctionDeclarations` available to the model that can be
168
+ /// used for function calling.
169
+ /// The model or system does not execute the function. Instead the defined function may be
170
+ /// returned as a ``FunctionCall`` in ``ModelContent/Part/functionCall(_:)`` with arguments to
171
+ /// the client side for execution. The model may decide to call a subset of these functions by
172
+ /// populating ``FunctionCall`` in the response. The next conversation turn may contain a
173
+ /// ``FunctionResponse`` in ``ModelContent/Part/functionResponse(_:)`` with the
174
+ /// ``ModelContent/role`` "function", providing generation context for the next model turn.
95
175
public init ( functionDeclarations: [ FunctionDeclaration ] ? ) {
96
176
self . functionDeclarations = functionDeclarations
97
177
}
98
178
}
99
179
100
- // REST Docs: https://ai.google.dev/api/rest/v1beta/Content#functionresponse
101
- public struct FunctionResponse : Equatable , Encodable {
180
+ /// Configuration for specifying function calling behavior.
181
+ public struct FunctionCallingConfig {
182
+ /// Defines the execution behavior for function calling by defining the
183
+ /// execution mode.
184
+ public enum Mode : String {
185
+ /// The default behavior for function calling. The model calls functions to answer queries at
186
+ /// its discretion.
187
+ case auto = " AUTO "
188
+
189
+ /// The model always predicts a provided function call to answer every query.
190
+ case any = " ANY "
191
+
192
+ /// The model will never predict a function call to answer a query. This can also be achieved by
193
+ /// not passing any tools to the model.
194
+ case none = " NONE "
195
+ }
196
+
197
+ /// Specifies the mode in which function calling should execute. If
198
+ /// unspecified, the default value will be set to AUTO.
199
+ let mode : Mode ?
200
+
201
+ /// A set of function names that, when provided, limits the functions the model
202
+ /// will call.
203
+ ///
204
+ /// This should only be set when the Mode is ANY. Function names
205
+ /// should match [FunctionDeclaration.name]. With mode set to ANY, model will
206
+ /// predict a function call from the set of function names provided.
207
+ let allowedFunctionNames : [ String ] ?
208
+
209
+ public init ( mode: FunctionCallingConfig . Mode ? = nil , allowedFunctionNames: [ String ] ? = nil ) {
210
+ self . mode = mode
211
+ self . allowedFunctionNames = allowedFunctionNames
212
+ }
213
+ }
214
+
215
+ /// Tool configuration for any `Tool` specified in the request.
216
+ public struct ToolConfig {
217
+ let functionCallingConfig : FunctionCallingConfig ?
218
+
219
+ public init ( functionCallingConfig: FunctionCallingConfig ? = nil ) {
220
+ self . functionCallingConfig = functionCallingConfig
221
+ }
222
+ }
223
+
224
+ /// Result output from a ``FunctionCall``.
225
+ ///
226
+ /// Contains a string representing the `FunctionDeclaration.name` and a structured JSON object
227
+ /// containing any output from the function is used as context to the model. This should contain the
228
+ /// result of a ``FunctionCall`` made based on model prediction.
229
+ public struct FunctionResponse : Equatable {
230
+ /// The name of the function that was called.
102
231
let name : String
103
232
233
+ /// The function's response.
104
234
let response : JSONObject
235
+
236
+ /// Constructs a new `FunctionResponse`.
237
+ ///
238
+ /// - Parameters:
239
+ /// - name: The name of the function that was called.
240
+ /// - response: The function's response.
241
+ public init ( name: String , response: JSONObject ) {
242
+ self . name = name
243
+ self . response = response
244
+ }
105
245
}
106
246
107
247
// MARK: - Codable Conformance
@@ -123,6 +263,8 @@ extension FunctionCall: Decodable {
123
263
}
124
264
}
125
265
266
+ extension FunctionCall : Encodable { }
267
+
126
268
extension FunctionDeclaration : Encodable {
127
269
enum CodingKeys : String , CodingKey {
128
270
case name
@@ -137,3 +279,17 @@ extension FunctionDeclaration: Encodable {
137
279
try container. encode ( parameters, forKey: . parameters)
138
280
}
139
281
}
282
+
283
+ extension Schema : Encodable { }
284
+
285
+ extension DataType : Encodable { }
286
+
287
+ extension Tool : Encodable { }
288
+
289
+ extension FunctionCallingConfig : Encodable { }
290
+
291
+ extension FunctionCallingConfig . Mode : Encodable { }
292
+
293
+ extension ToolConfig : Encodable { }
294
+
295
+ extension FunctionResponse : Encodable { }
0 commit comments