GenerationConfig.swift 8.3 KB

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