FunctionCalling.swift 9.7 KB

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