FunctionCalling.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. public struct FunctionDeclaration {
  20. /// The name of the function.
  21. let name: String
  22. /// A brief description of the function.
  23. let description: String
  24. /// Describes the parameters to this function; must be of type ``DataType/object``.
  25. let parameters: Schema?
  26. /// Constructs a new `FunctionDeclaration`.
  27. ///
  28. /// - Parameters:
  29. /// - name: The name of the function; must be a-z, A-Z, 0-9, or contain underscores and dashes,
  30. /// with a maximum length of 63.
  31. /// - description: A brief description of the function.
  32. /// - parameters: Describes the parameters to this function.
  33. public init(name: String, description: String, parameters: [String: Schema],
  34. optionalParameters: [String] = []) {
  35. self.name = name
  36. self.description = description
  37. self.parameters = Schema.object(
  38. properties: parameters,
  39. optionalProperties: optionalParameters,
  40. nullable: false
  41. )
  42. }
  43. }
  44. /// Helper tools that the model may use to generate response.
  45. ///
  46. /// A `Tool` is a piece of code that enables the system to interact with external systems to
  47. /// perform an action, or set of actions, outside of knowledge and scope of the model.
  48. public struct Tool {
  49. /// A list of `FunctionDeclarations` available to the model.
  50. let functionDeclarations: [FunctionDeclaration]?
  51. /// Constructs a new `Tool`.
  52. ///
  53. /// - Parameters:
  54. /// - functionDeclarations: A list of `FunctionDeclarations` available to the model that can be
  55. /// used for function calling.
  56. /// The model or system does not execute the function. Instead the defined function may be
  57. /// returned as a ``FunctionCall`` in ``ModelContent/Part/functionCall(_:)`` with arguments to
  58. /// the client side for execution. The model may decide to call a subset of these functions by
  59. /// populating ``FunctionCall`` in the response. The next conversation turn may contain a
  60. /// ``FunctionResponse`` in ``ModelContent/Part/functionResponse(_:)`` with the
  61. /// ``ModelContent/role`` "function", providing generation context for the next model turn.
  62. public init(functionDeclarations: [FunctionDeclaration]?) {
  63. self.functionDeclarations = functionDeclarations
  64. }
  65. }
  66. /// Configuration for specifying function calling behavior.
  67. public struct FunctionCallingConfig {
  68. /// Defines the execution behavior for function calling by defining the execution mode.
  69. public struct Mode: EncodableProtoEnum {
  70. enum Kind: String {
  71. case auto = "AUTO"
  72. case any = "ANY"
  73. case none = "NONE"
  74. }
  75. /// The default behavior for function calling.
  76. ///
  77. /// The model calls functions to answer queries at its discretion.
  78. public static let auto = Mode(kind: .auto)
  79. /// The model always predicts a provided function call to answer every query.
  80. public static let any = Mode(kind: .any)
  81. /// The model will never predict a function call to answer a query.
  82. ///
  83. /// > Note: This can also be achieved by not passing any ``FunctionDeclaration`` tools
  84. /// > when instantiating the model.
  85. public static let none = Mode(kind: .none)
  86. let rawValue: String
  87. }
  88. /// Specifies the mode in which function calling should execute.
  89. let mode: Mode?
  90. /// A set of function names that, when provided, limits the functions the model will call.
  91. let allowedFunctionNames: [String]?
  92. /// Creates a new `FunctionCallingConfig`.
  93. ///
  94. /// - Parameters:
  95. /// - mode: Specifies the mode in which function calling should execute; if unspecified, the
  96. /// default behavior will be ``Mode/auto``.
  97. /// - allowedFunctionNames: A set of function names that, when provided, limits the functions
  98. /// the model will call.
  99. /// Note: This should only be set when the ``Mode`` is ``Mode/any``. Function names should match
  100. /// `[FunctionDeclaration.name]`. With mode set to ``Mode/any``, the model will predict a
  101. /// function call from the set of function names provided.
  102. public init(mode: FunctionCallingConfig.Mode? = nil, allowedFunctionNames: [String]? = nil) {
  103. self.mode = mode
  104. self.allowedFunctionNames = allowedFunctionNames
  105. }
  106. }
  107. /// Tool configuration for any `Tool` specified in the request.
  108. public struct ToolConfig {
  109. let functionCallingConfig: FunctionCallingConfig?
  110. public init(functionCallingConfig: FunctionCallingConfig? = nil) {
  111. self.functionCallingConfig = functionCallingConfig
  112. }
  113. }
  114. // MARK: - Codable Conformance
  115. extension FunctionDeclaration: Encodable {
  116. enum CodingKeys: String, CodingKey {
  117. case name
  118. case description
  119. case parameters
  120. }
  121. public func encode(to encoder: Encoder) throws {
  122. var container = encoder.container(keyedBy: CodingKeys.self)
  123. try container.encode(name, forKey: .name)
  124. try container.encode(description, forKey: .description)
  125. try container.encode(parameters, forKey: .parameters)
  126. }
  127. }
  128. extension Tool: Encodable {}
  129. extension FunctionCallingConfig: Encodable {}
  130. extension FunctionCallingConfig.Mode: Encodable {}
  131. extension ToolConfig: Encodable {}