GenerationConfig.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  18. public struct GenerationConfig {
  19. /// The temperature controls the degree of randomness in the output produced by the model.
  20. ///
  21. /// The temperature is used for sampling during response generation, which occurs when ``topP``
  22. /// and ``topK`` are applied. Temperature controls the degree of randomness in token selection.
  23. /// Lower temperatures are good for prompts that require a less open-ended or creative response,
  24. /// while higher temperatures can lead to more diverse or creative results. A temperature of `0`
  25. /// means that the highest probability tokens are always selected. In this case, responses for a
  26. /// given prompt are *mostly* deterministic, but a small amount of variation is still possible.
  27. ///
  28. /// Each model has its own temperature range and default value; see [Experiment with parameter
  29. /// values](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/prompts/adjust-parameter-values#temperature)
  30. /// for more details.
  31. public let temperature: Float?
  32. /// The `topP` parameter changes how the model selects tokens for output.
  33. /// Tokens are selected from the most to least probable until the sum of
  34. /// their probabilities equals the `topP` value. For example, if tokens A, B,
  35. /// and C have probabilities of 0.3, 0.2, and 0.1 respectively and the topP
  36. /// value is 0.5, then the model will select either A or B as the next token
  37. /// by using the `temperature` and exclude C as a candidate.
  38. /// Defaults to 0.95 if unset.
  39. public let topP: Float?
  40. /// The `topK` parameter changes how the model selects tokens for output. A
  41. /// `topK` of 1 means the selected token is the most probable among all the
  42. /// tokens in the model's vocabulary, while a `topK` of 3 means that the next
  43. /// token is selected from among the 3 most probable using the `temperature`.
  44. /// For each token selection step, the `topK` tokens with the highest
  45. /// probabilities are sampled. Tokens are then further filtered based on
  46. /// `topP` with the final token selected using `temperature` sampling.
  47. /// Defaults to 40 if unspecified.
  48. public let topK: Int?
  49. /// The maximum number of generated response messages to return. This value
  50. /// must be between [1, 8], inclusive. If unset, this will default to 1.
  51. ///
  52. /// - Note: Only unique candidates are returned. Higher temperatures are more
  53. /// likely to produce unique candidates. Setting `temperature` to 0 will
  54. /// always produce exactly one candidate regardless of the
  55. /// `candidateCount`.
  56. public let candidateCount: Int?
  57. /// Specifies the maximum number of tokens that can be generated in the
  58. /// response. The number of tokens per word varies depending on the
  59. /// language outputted. The maximum value is capped at 1024. Defaults to 0
  60. /// (unbounded).
  61. public let maxOutputTokens: Int?
  62. /// A set of up to 5 `String`s that will stop output generation. If
  63. /// specified, the API will stop at the first appearance of a stop sequence.
  64. /// The stop sequence will not be included as part of the response.
  65. public let stopSequences: [String]?
  66. /// Output response MIME type of the generated candidate text.
  67. ///
  68. /// Supported MIME types:
  69. /// - `text/plain`: Text output; the default behavior if unspecified.
  70. /// - `application/json`: JSON response in the candidates.
  71. public let responseMIMEType: String?
  72. /// Output schema of the generated candidate text.
  73. /// If set, a compatible ``responseMIMEType`` must also be set.
  74. ///
  75. /// Compatible MIME types:
  76. /// - `application/json`: Schema for JSON response.
  77. ///
  78. /// Refer to the [Control generated
  79. /// output](https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/control-generated-output)
  80. /// guide for more details.
  81. public let responseSchema: Schema?
  82. /// Creates a new `GenerationConfig` value.
  83. ///
  84. /// - Parameters:
  85. /// - temperature: The degree of randomness in the output produced by the model; see
  86. /// ``temperature`` for more details.
  87. /// - topP: See ``topP``
  88. /// - topK: See ``topK``
  89. /// - candidateCount: See ``candidateCount``
  90. /// - maxOutputTokens: See ``maxOutputTokens``
  91. /// - stopSequences: See ``stopSequences``
  92. /// - responseMIMEType: See ``responseMIMEType``
  93. /// - responseSchema: See ``responseSchema``
  94. public init(temperature: Float? = nil, topP: Float? = nil, topK: Int? = nil,
  95. candidateCount: Int? = nil, maxOutputTokens: Int? = nil,
  96. stopSequences: [String]? = nil, responseMIMEType: String? = nil,
  97. responseSchema: Schema? = nil) {
  98. // Explicit init because otherwise if we re-arrange the above variables it changes the API
  99. // surface.
  100. self.temperature = temperature
  101. self.topP = topP
  102. self.topK = topK
  103. self.candidateCount = candidateCount
  104. self.maxOutputTokens = maxOutputTokens
  105. self.stopSequences = stopSequences
  106. self.responseMIMEType = responseMIMEType
  107. self.responseSchema = responseSchema
  108. }
  109. }
  110. // MARK: - Codable Conformances
  111. @available(iOS 15.0, macOS 11.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  112. extension GenerationConfig: Encodable {}