GenerationConfig.swift 9.3 KB

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