Skip to content

Commit bca0c8b

Browse files
andrewheardG.Dev.Ssomsak
authored and
G.Dev.Ssomsak
committed
Remove public Codable (Decodable and/or Encodable) conformance (google-gemini#142)
1 parent 9821bd5 commit bca0c8b

6 files changed

+456
-267
lines changed

Sources/GoogleAI/CountTokensRequest.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ struct CountTokensRequest {
2121
let options: RequestOptions
2222
}
2323

24-
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
25-
extension CountTokensRequest: Encodable {
26-
enum CodingKeys: CodingKey {
27-
case contents
28-
}
29-
}
30-
3124
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
3225
extension CountTokensRequest: GenerativeAIRequest {
3326
typealias Response = CountTokensResponse
@@ -39,7 +32,19 @@ extension CountTokensRequest: GenerativeAIRequest {
3932

4033
/// The model's response to a count tokens request.
4134
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
42-
public struct CountTokensResponse: Decodable {
35+
public struct CountTokensResponse {
4336
/// The total number of tokens in the input given to the model as a prompt.
4437
public let totalTokens: Int
4538
}
39+
40+
// MARK: - Codable Conformances
41+
42+
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
43+
extension CountTokensRequest: Encodable {
44+
enum CodingKeys: CodingKey {
45+
case contents
46+
}
47+
}
48+
49+
@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *)
50+
extension CountTokensResponse: Decodable {}

Sources/GoogleAI/FunctionCalling.swift

Lines changed: 178 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,93 +15,233 @@
1515
import Foundation
1616

1717
/// 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 {
2119
/// The name of the function to call.
2220
public let name: String
2321

2422
/// The function parameters and values.
2523
public let args: JSONObject
2624
}
2725

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.
3032
let type: DataType
3133

34+
/// The format of the data.
3235
let format: String?
3336

37+
/// A brief description of the parameter.
3438
let description: String?
3539

40+
/// Indicates if the value may be null.
3641
let nullable: Bool?
3742

43+
/// Possible values of the element of type ``DataType/string`` with "enum" format.
3844
let enumValues: [String]?
3945

46+
/// Schema of the elements of type ``DataType/array``.
4047
let items: Schema?
4148

49+
/// Properties of type ``DataType/object``.
4250
let properties: [String: Schema]?
4351

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+
}
4565

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``.
4682
public init(type: DataType, format: String? = nil, description: String? = nil,
4783
nullable: Bool? = nil,
4884
enumValues: [String]? = nil, items: Schema? = nil,
4985
properties: [String: Schema]? = nil,
50-
required: [String]? = nil) {
86+
requiredProperties: [String]? = nil) {
5187
self.type = type
5288
self.format = format
5389
self.description = description
5490
self.nullable = nullable
5591
self.enumValues = enumValues
5692
self.items = items
5793
self.properties = properties
58-
self.required = required
94+
self.requiredProperties = requiredProperties
5995
}
6096
}
6197

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.
64103
case string = "STRING"
104+
105+
/// A floating-point number type.
65106
case number = "NUMBER"
107+
108+
/// An integer type.
66109
case integer = "INTEGER"
110+
111+
/// A boolean type.
67112
case boolean = "BOOLEAN"
113+
114+
/// An array type.
68115
case array = "ARRAY"
116+
117+
/// An object type.
69118
case object = "OBJECT"
70119
}
71120

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.
73125
public struct FunctionDeclaration {
126+
/// The name of the function.
74127
let name: String
75128

129+
/// A brief description of the function.
76130
let description: String
77131

78-
let parameters: Schema
132+
/// Describes the parameters to this function; must be of type ``DataType/object``.
133+
let parameters: Schema?
79134

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]?) {
84146
self.name = name
85147
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+
)
88153
}
89154
}
90155

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.
93162
let functionDeclarations: [FunctionDeclaration]?
94163

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.
95175
public init(functionDeclarations: [FunctionDeclaration]?) {
96176
self.functionDeclarations = functionDeclarations
97177
}
98178
}
99179

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.
102231
let name: String
103232

233+
/// The function's response.
104234
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+
}
105245
}
106246

107247
// MARK: - Codable Conformance
@@ -123,6 +263,8 @@ extension FunctionCall: Decodable {
123263
}
124264
}
125265

266+
extension FunctionCall: Encodable {}
267+
126268
extension FunctionDeclaration: Encodable {
127269
enum CodingKeys: String, CodingKey {
128270
case name
@@ -137,3 +279,17 @@ extension FunctionDeclaration: Encodable {
137279
try container.encode(parameters, forKey: .parameters)
138280
}
139281
}
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

Comments
 (0)