FunctionCalling.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. /// Structured representation of a function declaration.
  16. ///
  17. /// This `FunctionDeclaration` is a representation of a block of code that can be used as a ``Tool``
  18. /// by the model and executed by the client.
  19. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  20. public struct FunctionDeclaration: Equatable {
  21. /// The name of the function.
  22. let name: String
  23. /// A brief description of the function.
  24. let description: String
  25. /// Describes the parameters to this function; must be of type `DataType.object`.
  26. let parameters: Schema?
  27. /// Constructs a new `FunctionDeclaration`.
  28. ///
  29. /// - Parameters:
  30. /// - name: The name of the function; must be a-z, A-Z, 0-9, or contain underscores and dashes,
  31. /// with a maximum length of 63.
  32. /// - description: A brief description of the function.
  33. /// - parameters: Describes the parameters to this function.
  34. /// - optionalParameters: The names of parameters that may be omitted by the model in function
  35. /// calls; by default, all parameters are considered required.
  36. public init(name: String, description: String, parameters: [String: Schema],
  37. optionalParameters: [String] = []) {
  38. self.name = name
  39. self.description = description
  40. self.parameters = Schema.object(
  41. properties: parameters,
  42. optionalProperties: optionalParameters,
  43. nullable: false
  44. )
  45. }
  46. /// Constructs a new `FunctionDeclaration`.
  47. ///
  48. /// - Parameters:
  49. /// - jsonString: A string representing a function declaration in JSON format; see the [REST
  50. /// documentation]( https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1/Tool#FunctionDeclaration)
  51. /// for details.
  52. public init(jsonString: String) throws {
  53. guard let jsonData = jsonString.data(using: .utf8) else {
  54. throw DecodingError.dataCorrupted(DecodingError.Context(
  55. codingPath: [],
  56. debugDescription: "Could not parse JSON string as UTF8."
  57. ))
  58. }
  59. self = try JSONDecoder().decode(Self.self, from: jsonData)
  60. }
  61. }
  62. /// A helper tool that the model may use when generating responses.
  63. ///
  64. /// A `Tool` is a piece of code that enables the system to interact with external systems to perform
  65. /// an action, or set of actions, outside of knowledge and scope of the model.
  66. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  67. public struct Tool {
  68. /// A list of `FunctionDeclarations` available to the model.
  69. let functionDeclarations: [FunctionDeclaration]?
  70. init(functionDeclarations: [FunctionDeclaration]?) {
  71. self.functionDeclarations = functionDeclarations
  72. }
  73. /// Creates a tool that allows the model to perform function calling.
  74. ///
  75. /// Function calling can be used to provide data to the model that was not known at the time it
  76. /// was trained (for example, the current date or weather conditions) or to allow it to interact
  77. /// with external systems (for example, making an API request or querying/updating a database).
  78. /// For more details and use cases, see [Introduction to function
  79. /// calling](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling).
  80. ///
  81. /// - Parameters:
  82. /// - functionDeclarations: A list of `FunctionDeclarations` available to the model that can be
  83. /// used for function calling.
  84. /// The model or system does not execute the function. Instead the defined function may be
  85. /// returned as a ``FunctionCallPart`` with arguments to the client side for execution. The
  86. /// model may decide to call none, some or all of the declared functions; this behavior may be
  87. /// configured by specifying a ``ToolConfig`` when instantiating the model. When a
  88. /// ``FunctionCallPart`` is received, the next conversation turn may contain a
  89. /// ``FunctionResponsePart`` in ``ModelContent/parts`` with a ``ModelContent/role`` of
  90. /// `"function"`; this response contains the result of executing the function on the client,
  91. /// providing generation context for the model's next turn.
  92. public static func functionDeclarations(_ functionDeclarations: [FunctionDeclaration]) -> Tool {
  93. return self.init(functionDeclarations: functionDeclarations)
  94. }
  95. }
  96. /// Configuration for specifying function calling behavior.
  97. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  98. public struct FunctionCallingConfig {
  99. /// Defines the execution behavior for function calling by defining the execution mode.
  100. enum Mode: String {
  101. case auto = "AUTO"
  102. case any = "ANY"
  103. case none = "NONE"
  104. }
  105. /// Specifies the mode in which function calling should execute.
  106. let mode: Mode?
  107. /// A set of function names that, when provided, limits the functions the model will call.
  108. let allowedFunctionNames: [String]?
  109. init(mode: FunctionCallingConfig.Mode? = nil, allowedFunctionNames: [String]? = nil) {
  110. self.mode = mode
  111. self.allowedFunctionNames = allowedFunctionNames
  112. }
  113. /// Creates a function calling config where the model calls functions at its discretion.
  114. ///
  115. /// > Note: This is the default behavior.
  116. public static func auto() -> FunctionCallingConfig {
  117. return FunctionCallingConfig(mode: .auto)
  118. }
  119. /// Creates a function calling config where the model will always call a provided function.
  120. ///
  121. /// - Parameters:
  122. /// - allowedFunctionNames: A set of function names that, when provided, limits the functions
  123. /// that the model will call.
  124. public static func any(allowedFunctionNames: [String]? = nil) -> FunctionCallingConfig {
  125. return FunctionCallingConfig(mode: .any, allowedFunctionNames: allowedFunctionNames)
  126. }
  127. /// Creates a function calling config where the model will never call a function.
  128. ///
  129. /// > Note: This can also be achieved by not passing any ``FunctionDeclaration`` tools when
  130. /// > instantiating the model.
  131. public static func none() -> FunctionCallingConfig {
  132. return FunctionCallingConfig(mode: FunctionCallingConfig.Mode.none)
  133. }
  134. }
  135. /// Tool configuration for any `Tool` specified in the request.
  136. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  137. public struct ToolConfig {
  138. let functionCallingConfig: FunctionCallingConfig?
  139. public init(functionCallingConfig: FunctionCallingConfig? = nil) {
  140. self.functionCallingConfig = functionCallingConfig
  141. }
  142. }
  143. // MARK: - Codable Conformance
  144. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  145. extension FunctionDeclaration: Codable {
  146. enum CodingKeys: String, CodingKey {
  147. case name
  148. case description
  149. case parameters
  150. }
  151. public func encode(to encoder: Encoder) throws {
  152. var container = encoder.container(keyedBy: CodingKeys.self)
  153. try container.encode(name, forKey: .name)
  154. try container.encode(description, forKey: .description)
  155. try container.encode(parameters, forKey: .parameters)
  156. }
  157. }
  158. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  159. extension Tool: Encodable {}
  160. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  161. extension FunctionCallingConfig: Encodable {}
  162. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  163. extension FunctionCallingConfig.Mode: Encodable {}
  164. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  165. extension ToolConfig: Encodable {}