Tool.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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: Sendable {
  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 tool that allows the generative model to connect to Google Search to access and incorporate
  48. /// up-to-date information from the web into its responses.
  49. ///
  50. /// > Important: When using this feature, you are required to comply with the
  51. /// "Grounding with Google Search" usage requirements for your chosen API provider:
  52. /// [Gemini Developer API](https://ai.google.dev/gemini-api/terms#grounding-with-google-search)
  53. /// or Vertex AI Gemini API (see [Service Terms](https://cloud.google.com/terms/service-terms)
  54. /// section within the Service Specific Terms).
  55. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  56. public struct GoogleSearch: Sendable {
  57. public init() {}
  58. }
  59. /// A helper tool that the model may use when generating responses.
  60. ///
  61. /// A `Tool` is a piece of code that enables the system to interact with external systems to perform
  62. /// an action, or set of actions, outside of knowledge and scope of the model.
  63. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  64. public struct Tool: Sendable {
  65. /// A list of `FunctionDeclarations` available to the model.
  66. let functionDeclarations: [FunctionDeclaration]?
  67. /// Specifies the Google Search configuration.
  68. let googleSearch: GoogleSearch?
  69. let codeExecution: CodeExecution?
  70. let urlContext: URLContext?
  71. init(functionDeclarations: [FunctionDeclaration]? = nil,
  72. googleSearch: GoogleSearch? = nil,
  73. urlContext: URLContext? = nil,
  74. codeExecution: CodeExecution? = nil) {
  75. self.functionDeclarations = functionDeclarations
  76. self.googleSearch = googleSearch
  77. self.urlContext = urlContext
  78. self.codeExecution = codeExecution
  79. }
  80. /// Creates a tool that allows the model to perform function calling.
  81. ///
  82. /// Function calling can be used to provide data to the model that was not known at the time it
  83. /// was trained (for example, the current date or weather conditions) or to allow it to interact
  84. /// with external systems (for example, making an API request or querying/updating a database).
  85. /// For more details and use cases, see [Function calling using the Gemini
  86. /// API](http://firebase.google.com/docs/vertex-ai/function-calling?platform=ios).
  87. ///
  88. /// - Parameters:
  89. /// - functionDeclarations: A list of `FunctionDeclarations` available to the model that can be
  90. /// used for function calling.
  91. /// The model or system does not execute the function. Instead the defined function may be
  92. /// returned as a ``FunctionCallPart`` with arguments to the client side for execution. The
  93. /// model may decide to call none, some or all of the declared functions; this behavior may be
  94. /// configured by specifying a ``ToolConfig`` when instantiating the model. When a
  95. /// ``FunctionCallPart`` is received, the next conversation turn may contain a
  96. /// ``FunctionResponsePart`` in ``ModelContent/parts`` with a ``ModelContent/role`` of
  97. /// `"function"`; this response contains the result of executing the function on the client,
  98. /// providing generation context for the model's next turn.
  99. public static func functionDeclarations(_ functionDeclarations: [FunctionDeclaration]) -> Tool {
  100. return self.init(functionDeclarations: functionDeclarations)
  101. }
  102. /// Creates a tool that allows the model to use Grounding with Google Search.
  103. ///
  104. /// Grounding with Google Search can be used to allow the model to connect to Google Search to
  105. /// access and incorporate up-to-date information from the web into it's responses.
  106. ///
  107. /// > Important: When using this feature, you are required to comply with the
  108. /// "Grounding with Google Search" usage requirements for your chosen API provider:
  109. /// [Gemini Developer API](https://ai.google.dev/gemini-api/terms#grounding-with-google-search)
  110. /// or Vertex AI Gemini API (see [Service Terms](https://cloud.google.com/terms/service-terms)
  111. /// section within the Service Specific Terms).
  112. ///
  113. /// - Parameters:
  114. /// - googleSearch: An empty ``GoogleSearch`` object. The presence of this object in the list
  115. /// of tools enables the model to use Google Search.
  116. ///
  117. /// - Returns: A `Tool` configured for Google Search.
  118. public static func googleSearch(_ googleSearch: GoogleSearch = GoogleSearch()) -> Tool {
  119. return self.init(googleSearch: googleSearch)
  120. }
  121. /// Creates a tool that allows you to provide additional context to the models in the form of
  122. /// public web URLs.
  123. ///
  124. /// By including URLs in your request, the Gemini model will access the content from those pages
  125. /// to inform and enhance its response.
  126. ///
  127. /// > Warning: URL context is a **Public Preview** feature, which means that it is not subject to
  128. /// > any SLA or deprecation policy and could change in backwards-incompatible ways.
  129. public static func urlContext() -> Tool {
  130. return self.init(urlContext: URLContext())
  131. }
  132. /// Creates a tool that allows the model to execute code.
  133. ///
  134. /// For more details, see ``CodeExecution``.
  135. public static func codeExecution() -> Tool {
  136. return self.init(codeExecution: CodeExecution())
  137. }
  138. }
  139. /// Configuration for specifying function calling behavior.
  140. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  141. public struct FunctionCallingConfig: Sendable {
  142. /// Defines the execution behavior for function calling by defining the execution mode.
  143. enum Mode: String {
  144. case auto = "AUTO"
  145. case any = "ANY"
  146. case none = "NONE"
  147. }
  148. /// Specifies the mode in which function calling should execute.
  149. let mode: Mode?
  150. /// A set of function names that, when provided, limits the functions the model will call.
  151. let allowedFunctionNames: [String]?
  152. init(mode: FunctionCallingConfig.Mode? = nil, allowedFunctionNames: [String]? = nil) {
  153. self.mode = mode
  154. self.allowedFunctionNames = allowedFunctionNames
  155. }
  156. /// Creates a function calling config where the model calls functions at its discretion.
  157. ///
  158. /// > Note: This is the default behavior.
  159. public static func auto() -> FunctionCallingConfig {
  160. return FunctionCallingConfig(mode: .auto)
  161. }
  162. /// Creates a function calling config where the model will always call a provided function.
  163. ///
  164. /// - Parameters:
  165. /// - allowedFunctionNames: A set of function names that, when provided, limits the functions
  166. /// that the model will call.
  167. public static func any(allowedFunctionNames: [String]? = nil) -> FunctionCallingConfig {
  168. return FunctionCallingConfig(mode: .any, allowedFunctionNames: allowedFunctionNames)
  169. }
  170. /// Creates a function calling config where the model will never call a function.
  171. ///
  172. /// > Note: This can also be achieved by not passing any ``FunctionDeclaration`` tools when
  173. /// > instantiating the model.
  174. public static func none() -> FunctionCallingConfig {
  175. return FunctionCallingConfig(mode: FunctionCallingConfig.Mode.none)
  176. }
  177. }
  178. /// Tool configuration for any `Tool` specified in the request.
  179. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  180. public struct ToolConfig: Sendable {
  181. let functionCallingConfig: FunctionCallingConfig?
  182. public init(functionCallingConfig: FunctionCallingConfig? = nil) {
  183. self.functionCallingConfig = functionCallingConfig
  184. }
  185. }
  186. // MARK: - Codable Conformance
  187. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  188. extension FunctionDeclaration: Encodable {
  189. enum CodingKeys: String, CodingKey {
  190. case name
  191. case description
  192. case parameters
  193. }
  194. public func encode(to encoder: Encoder) throws {
  195. var container = encoder.container(keyedBy: CodingKeys.self)
  196. try container.encode(name, forKey: .name)
  197. try container.encode(description, forKey: .description)
  198. try container.encode(parameters, forKey: .parameters)
  199. }
  200. }
  201. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  202. extension Tool: Encodable {}
  203. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  204. extension FunctionCallingConfig: Encodable {}
  205. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  206. extension FunctionCallingConfig.Mode: Encodable {}
  207. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  208. extension GoogleSearch: Encodable {}
  209. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  210. extension ToolConfig: Encodable {}