GenerationConfig.swift 8.8 KB

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