FunctionCalling.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, Sendable {
  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. /// Structured representation of a function declaration.
  23. ///
  24. /// This `FunctionDeclaration` is a representation of a block of code that can be used as a ``Tool``
  25. /// by the model and executed by the client.
  26. public struct FunctionDeclaration {
  27. /// The name of the function.
  28. let name: String
  29. /// A brief description of the function.
  30. let description: String
  31. /// Describes the parameters to this function; must be of type ``DataType/object``.
  32. let parameters: Schema?
  33. /// Constructs a new `FunctionDeclaration`.
  34. ///
  35. /// - Parameters:
  36. /// - name: The name of the function; must be a-z, A-Z, 0-9, or contain underscores and dashes,
  37. /// with a maximum length of 63.
  38. /// - description: A brief description of the function.
  39. /// - parameters: Describes the parameters to this function; the keys are parameter names and
  40. /// the values are ``Schema`` objects describing them.
  41. /// - requiredParameters: A list of required parameters by name.
  42. public init(name: String, description: String, parameters: [String: Schema]?,
  43. requiredParameters: [String]? = nil) {
  44. self.name = name
  45. self.description = description
  46. self.parameters = Schema(
  47. type: .object,
  48. properties: parameters,
  49. requiredProperties: requiredParameters
  50. )
  51. }
  52. }
  53. /// Helper tools that the model may use to generate response.
  54. ///
  55. /// A `Tool` is a piece of code that enables the system to interact with external systems to
  56. /// perform an action, or set of actions, outside of knowledge and scope of the model.
  57. public struct Tool {
  58. /// A list of `FunctionDeclarations` available to the model.
  59. let functionDeclarations: [FunctionDeclaration]?
  60. /// Constructs a new `Tool`.
  61. ///
  62. /// - Parameters:
  63. /// - functionDeclarations: A list of `FunctionDeclarations` available to the model that can be
  64. /// used for function calling.
  65. /// The model or system does not execute the function. Instead the defined function may be
  66. /// returned as a ``FunctionCall`` in ``ModelContent/Part/functionCall(_:)`` with arguments to
  67. /// the client side for execution. The model may decide to call a subset of these functions by
  68. /// populating ``FunctionCall`` in the response. The next conversation turn may contain a
  69. /// ``FunctionResponse`` in ``ModelContent/Part/functionResponse(_:)`` with the
  70. /// ``ModelContent/role`` "function", providing generation context for the next model turn.
  71. public init(functionDeclarations: [FunctionDeclaration]?) {
  72. self.functionDeclarations = functionDeclarations
  73. }
  74. }
  75. /// Configuration for specifying function calling behavior.
  76. public struct FunctionCallingConfig {
  77. /// Defines the execution behavior for function calling by defining the
  78. /// execution mode.
  79. public enum Mode: String {
  80. /// The default behavior for function calling. The model calls functions to answer queries at
  81. /// its discretion.
  82. case auto = "AUTO"
  83. /// The model always predicts a provided function call to answer every query.
  84. case any = "ANY"
  85. /// The model will never predict a function call to answer a query. This can also be achieved by
  86. /// not passing any tools to the model.
  87. case none = "NONE"
  88. }
  89. /// Specifies the mode in which function calling should execute. If
  90. /// unspecified, the default value will be set to AUTO.
  91. let mode: Mode?
  92. /// A set of function names that, when provided, limits the functions the model
  93. /// will call.
  94. ///
  95. /// This should only be set when the Mode is ANY. Function names
  96. /// should match [FunctionDeclaration.name]. With mode set to ANY, model will
  97. /// predict a function call from the set of function names provided.
  98. let allowedFunctionNames: [String]?
  99. public init(mode: FunctionCallingConfig.Mode? = nil, allowedFunctionNames: [String]? = nil) {
  100. self.mode = mode
  101. self.allowedFunctionNames = allowedFunctionNames
  102. }
  103. }
  104. /// Tool configuration for any `Tool` specified in the request.
  105. public struct ToolConfig {
  106. let functionCallingConfig: FunctionCallingConfig?
  107. public init(functionCallingConfig: FunctionCallingConfig? = nil) {
  108. self.functionCallingConfig = functionCallingConfig
  109. }
  110. }
  111. /// Result output from a ``FunctionCall``.
  112. ///
  113. /// Contains a string representing the `FunctionDeclaration.name` and a structured JSON object
  114. /// containing any output from the function is used as context to the model. This should contain the
  115. /// result of a ``FunctionCall`` made based on model prediction.
  116. public struct FunctionResponse: Equatable, Sendable {
  117. /// The name of the function that was called.
  118. let name: String
  119. /// The function's response.
  120. let response: JSONObject
  121. /// Constructs a new `FunctionResponse`.
  122. ///
  123. /// - Parameters:
  124. /// - name: The name of the function that was called.
  125. /// - response: The function's response.
  126. public init(name: String, response: JSONObject) {
  127. self.name = name
  128. self.response = response
  129. }
  130. }
  131. // MARK: - Codable Conformance
  132. extension FunctionCall: Decodable {
  133. enum CodingKeys: CodingKey {
  134. case name
  135. case args
  136. }
  137. public init(from decoder: Decoder) throws {
  138. let container = try decoder.container(keyedBy: CodingKeys.self)
  139. name = try container.decode(String.self, forKey: .name)
  140. if let args = try container.decodeIfPresent(JSONObject.self, forKey: .args) {
  141. self.args = args
  142. } else {
  143. args = JSONObject()
  144. }
  145. }
  146. }
  147. extension FunctionCall: Encodable {}
  148. extension FunctionDeclaration: Encodable {
  149. enum CodingKeys: String, CodingKey {
  150. case name
  151. case description
  152. case parameters
  153. }
  154. public func encode(to encoder: Encoder) throws {
  155. var container = encoder.container(keyedBy: CodingKeys.self)
  156. try container.encode(name, forKey: .name)
  157. try container.encode(description, forKey: .description)
  158. try container.encode(parameters, forKey: .parameters)
  159. }
  160. }
  161. extension Tool: Encodable {}
  162. extension FunctionCallingConfig: Encodable {}
  163. extension FunctionCallingConfig.Mode: Encodable {}
  164. extension ToolConfig: Encodable {}
  165. extension FunctionResponse: Codable {}