FunctionCalling.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 {
  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. }
  47. /// A helper tool that the model may use when generating responses.
  48. ///
  49. /// A `Tool` is a piece of code that enables the system to interact with external systems to perform
  50. /// an action, or set of actions, outside of knowledge and scope of the model.
  51. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  52. public struct Tool {
  53. /// A list of `FunctionDeclarations` available to the model.
  54. let functionDeclarations: [FunctionDeclaration]?
  55. init(functionDeclarations: [FunctionDeclaration]?) {
  56. self.functionDeclarations = functionDeclarations
  57. }
  58. /// Creates a tool that allows the model to perform function calling.
  59. ///
  60. /// Function calling can be used to provide data to the model that was not known at the time it
  61. /// was trained (for example, the current date or weather conditions) or to allow it to interact
  62. /// with external systems (for example, making an API request or querying/updating a database).
  63. /// For more details and use cases, see [Introduction to function
  64. /// calling](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/function-calling).
  65. ///
  66. /// - Parameters:
  67. /// - functionDeclarations: A list of `FunctionDeclarations` available to the model that can be
  68. /// used for function calling.
  69. /// The model or system does not execute the function. Instead the defined function may be
  70. /// returned as a ``FunctionCallPart`` with arguments to the client side for execution. The
  71. /// model may decide to call none, some or all of the declared functions; this behavior may be
  72. /// configured by specifying a ``ToolConfig`` when instantiating the model. When a
  73. /// ``FunctionCallPart`` is received, the next conversation turn may contain a
  74. /// ``FunctionResponsePart`` in ``ModelContent/parts`` with a ``ModelContent/role`` of
  75. /// `"function"`; this response contains the result of executing the function on the client,
  76. /// providing generation context for the model's next turn.
  77. public static func functionDeclarations(_ functionDeclarations: [FunctionDeclaration]) -> Tool {
  78. return self.init(functionDeclarations: functionDeclarations)
  79. }
  80. }
  81. /// Configuration for specifying function calling behavior.
  82. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  83. public struct FunctionCallingConfig {
  84. /// Defines the execution behavior for function calling by defining the execution mode.
  85. enum Mode: String {
  86. case auto = "AUTO"
  87. case any = "ANY"
  88. case none = "NONE"
  89. }
  90. /// Specifies the mode in which function calling should execute.
  91. let mode: Mode?
  92. /// A set of function names that, when provided, limits the functions the model will call.
  93. let allowedFunctionNames: [String]?
  94. init(mode: FunctionCallingConfig.Mode? = nil, allowedFunctionNames: [String]? = nil) {
  95. self.mode = mode
  96. self.allowedFunctionNames = allowedFunctionNames
  97. }
  98. /// Creates a function calling config where the model calls functions at its discretion.
  99. ///
  100. /// > Note: This is the default behavior.
  101. public static func auto() -> FunctionCallingConfig {
  102. return FunctionCallingConfig(mode: .auto)
  103. }
  104. /// Creates a function calling config where the model will always call a provided function.
  105. ///
  106. /// - Parameters:
  107. /// - allowedFunctionNames: A set of function names that, when provided, limits the functions
  108. /// that the model will call.
  109. public static func any(allowedFunctionNames: [String]? = nil) -> FunctionCallingConfig {
  110. return FunctionCallingConfig(mode: .any, allowedFunctionNames: allowedFunctionNames)
  111. }
  112. /// Creates a function calling config where the model will never call a function.
  113. ///
  114. /// > Note: This can also be achieved by not passing any ``FunctionDeclaration`` tools when
  115. /// > instantiating the model.
  116. public static func none() -> FunctionCallingConfig {
  117. return FunctionCallingConfig(mode: FunctionCallingConfig.Mode.none)
  118. }
  119. }
  120. /// Tool configuration for any `Tool` specified in the request.
  121. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  122. public struct ToolConfig {
  123. let functionCallingConfig: FunctionCallingConfig?
  124. public init(functionCallingConfig: FunctionCallingConfig? = nil) {
  125. self.functionCallingConfig = functionCallingConfig
  126. }
  127. }
  128. // MARK: - Codable Conformance
  129. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  130. extension FunctionDeclaration: Encodable {
  131. enum CodingKeys: String, CodingKey {
  132. case name
  133. case description
  134. case parameters
  135. }
  136. public func encode(to encoder: Encoder) throws {
  137. var container = encoder.container(keyedBy: CodingKeys.self)
  138. try container.encode(name, forKey: .name)
  139. try container.encode(description, forKey: .description)
  140. try container.encode(parameters, forKey: .parameters)
  141. }
  142. }
  143. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  144. extension Tool: Encodable {}
  145. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  146. extension FunctionCallingConfig: Encodable {}
  147. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  148. extension FunctionCallingConfig.Mode: Encodable {}
  149. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  150. extension ToolConfig: Encodable {}