GenerationConfig.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2023 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 struct defining model parameters to be used when sending generative AI
  16. /// requests to the backend model.
  17. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  18. public struct GenerationConfig: Sendable {
  19. /// Controls the degree of randomness in token selection.
  20. let temperature: Float?
  21. /// Controls diversity of generated text.
  22. let topP: Float?
  23. /// Limits the number of highest probability words considered.
  24. let topK: Int?
  25. /// The number of response variations to return.
  26. let candidateCount: Int?
  27. /// Maximum number of tokens that can be generated in the response.
  28. let maxOutputTokens: Int?
  29. /// Controls the likelihood of repeating the same words or phrases already generated in the text.
  30. let presencePenalty: Float?
  31. /// Controls the likelihood of repeating words, with the penalty increasing for each repetition.
  32. let frequencyPenalty: Float?
  33. /// A set of up to 5 `String`s that will stop output generation.
  34. let stopSequences: [String]?
  35. /// Output response MIME type of the generated candidate text.
  36. let responseMIMEType: String?
  37. /// Output schema of the generated candidate text.
  38. let responseSchema: Schema?
  39. /// Supported modalities of the response.
  40. let responseModalities: [ResponseModality]?
  41. /// Configuration for controlling the "thinking" behavior of compatible Gemini models.
  42. let thinkingConfig: ThinkingConfig?
  43. /// Creates a new `GenerationConfig` value.
  44. ///
  45. /// See the
  46. /// [Configure model parameters](https://firebase.google.com/docs/vertex-ai/model-parameters)
  47. /// guide and the
  48. /// [Cloud documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference#generationconfig)
  49. /// for more details.
  50. ///
  51. /// - Parameters:
  52. /// - temperature:Controls the randomness of the language model's output. Higher values (for
  53. /// example, 1.0) make the text more random and creative, while lower values (for example,
  54. /// 0.1) make it more focused and deterministic.
  55. ///
  56. /// > Note: A temperature of 0 means that the highest probability tokens are always selected.
  57. /// > In this case, responses for a given prompt are mostly deterministic, but a small amount
  58. /// > of variation is still possible.
  59. ///
  60. /// > Important: The range of supported temperature values depends on the model; see the
  61. /// > [documentation](https://firebase.google.com/docs/vertex-ai/model-parameters?platform=ios#temperature)
  62. /// > for more details.
  63. /// - topP: Controls diversity of generated text. Higher values (e.g., 0.9) produce more diverse
  64. /// text, while lower values (e.g., 0.5) make the output more focused.
  65. ///
  66. /// The supported range is 0.0 to 1.0.
  67. ///
  68. /// > Important: The default `topP` value depends on the model; see the
  69. /// > [documentation](https://firebase.google.com/docs/vertex-ai/model-parameters?platform=ios#top-p)
  70. /// > for more details.
  71. /// - topK: Limits the number of highest probability words the model considers when generating
  72. /// text. For example, a topK of 40 means only the 40 most likely words are considered for the
  73. /// next token. A higher value increases diversity, while a lower value makes the output more
  74. /// deterministic.
  75. ///
  76. /// The supported range is 1 to 40.
  77. ///
  78. /// > Important: Support for `topK` and the default value depends on the model; see the
  79. /// [documentation](https://firebase.google.com/docs/vertex-ai/model-parameters?platform=ios#top-k)
  80. /// for more details.
  81. /// - candidateCount: The number of response variations to return; defaults to 1 if not set.
  82. /// Support for multiple candidates depends on the model; see the
  83. /// [Cloud documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference#generationconfig)
  84. /// for more details.
  85. /// - maxOutputTokens: Maximum number of tokens that can be generated in the response.
  86. /// See the configure model parameters [documentation](https://firebase.google.com/docs/vertex-ai/model-parameters?platform=ios#max-output-tokens)
  87. /// for more details.
  88. /// - presencePenalty: Controls the likelihood of repeating the same words or phrases already
  89. /// generated in the text. Higher values increase the penalty of repetition, resulting in more
  90. /// diverse output.
  91. ///
  92. /// > Note: While both `presencePenalty` and `frequencyPenalty` discourage repetition,
  93. /// > `presencePenalty` applies the same penalty regardless of how many times the word/phrase
  94. /// > has already appeared, whereas `frequencyPenalty` increases the penalty for *each*
  95. /// > repetition of a word/phrase.
  96. ///
  97. /// > Important: The range of supported `presencePenalty` values depends on the model; see the
  98. /// > [Cloud documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference#generationconfig)
  99. /// > for more details
  100. /// - frequencyPenalty: Controls the likelihood of repeating words or phrases, with the penalty
  101. /// increasing for each repetition. Higher values increase the penalty of repetition,
  102. /// resulting in more diverse output.
  103. ///
  104. /// > Note: While both `frequencyPenalty` and `presencePenalty` discourage repetition,
  105. /// > `frequencyPenalty` increases the penalty for *each* repetition of a word/phrase, whereas
  106. /// > `presencePenalty` applies the same penalty regardless of how many times the word/phrase
  107. /// > has already appeared.
  108. ///
  109. /// > Important: The range of supported `frequencyPenalty` values depends on the model; see
  110. /// > the
  111. /// > [Cloud documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference#generationconfig)
  112. /// > for more details
  113. /// - stopSequences: A set of up to 5 `String`s that will stop output generation. If specified,
  114. /// the API will stop at the first appearance of a stop sequence. The stop sequence will not
  115. /// be included as part of the response. See the
  116. /// [Cloud documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/inference#generationconfig)
  117. /// for more details.
  118. /// - responseMIMEType: Output response MIME type of the generated candidate text.
  119. ///
  120. /// Supported MIME types:
  121. /// - `text/plain`: Text output; the default behavior if unspecified.
  122. /// - `application/json`: JSON response in the candidates.
  123. /// - `text/x.enum`: For classification tasks, output an enum value as defined in the
  124. /// `responseSchema`.
  125. /// - responseSchema: Output schema of the generated candidate text. If set, a compatible
  126. /// `responseMIMEType` must also be set.
  127. ///
  128. /// Compatible MIME types:
  129. /// - `application/json`: Schema for JSON response.
  130. ///
  131. /// Refer to the
  132. /// [Generate structured
  133. /// output](https://firebase.google.com/docs/vertex-ai/structured-output?platform=ios) guide
  134. /// for more details.
  135. /// - responseModalities: The data types (modalities) that may be returned in model responses.
  136. ///
  137. /// See the [multimodal
  138. /// responses](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal-response-generation)
  139. /// documentation for more details.
  140. ///
  141. /// > Warning: Specifying response modalities is a **Public Preview** feature, which means
  142. /// > that it is not subject to any SLA or deprecation policy and could change in
  143. /// > backwards-incompatible ways.
  144. /// - thinkingConfig: Configuration for controlling the "thinking" behavior of compatible Gemini
  145. /// models; see ``ThinkingConfig`` for more details.
  146. public init(temperature: Float? = nil, topP: Float? = nil, topK: Int? = nil,
  147. candidateCount: Int? = nil, maxOutputTokens: Int? = nil,
  148. presencePenalty: Float? = nil, frequencyPenalty: Float? = nil,
  149. stopSequences: [String]? = nil, responseMIMEType: String? = nil,
  150. responseSchema: Schema? = nil, responseModalities: [ResponseModality]? = nil,
  151. thinkingConfig: ThinkingConfig? = nil) {
  152. // Explicit init because otherwise if we re-arrange the above variables it changes the API
  153. // surface.
  154. self.temperature = temperature
  155. self.topP = topP
  156. self.topK = topK
  157. self.candidateCount = candidateCount
  158. self.maxOutputTokens = maxOutputTokens
  159. self.presencePenalty = presencePenalty
  160. self.frequencyPenalty = frequencyPenalty
  161. self.stopSequences = stopSequences
  162. self.responseMIMEType = responseMIMEType
  163. self.responseSchema = responseSchema
  164. self.responseModalities = responseModalities
  165. self.thinkingConfig = thinkingConfig
  166. }
  167. }
  168. // MARK: - Codable Conformances
  169. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  170. extension GenerationConfig: Encodable {
  171. enum CodingKeys: String, CodingKey {
  172. case temperature
  173. case topP
  174. case topK
  175. case candidateCount
  176. case maxOutputTokens
  177. case presencePenalty
  178. case frequencyPenalty
  179. case stopSequences
  180. case responseMIMEType = "responseMimeType"
  181. case responseSchema
  182. case responseModalities
  183. case thinkingConfig
  184. }
  185. }