GenerationConfigTests.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2024 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 FirebaseVertexAI
  15. import Foundation
  16. import XCTest
  17. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  18. final class GenerationConfigTests: XCTestCase {
  19. let encoder = JSONEncoder()
  20. override func setUp() {
  21. encoder.outputFormatting = .init(
  22. arrayLiteral: .prettyPrinted, .sortedKeys, .withoutEscapingSlashes
  23. )
  24. }
  25. // MARK: GenerationConfig Encoding
  26. func testEncodeGenerationConfig_default() throws {
  27. let generationConfig = GenerationConfig()
  28. let jsonData = try encoder.encode(generationConfig)
  29. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  30. XCTAssertEqual(json, """
  31. {
  32. }
  33. """)
  34. }
  35. func testEncodeGenerationConfig_allOptions() throws {
  36. let temperature: Float = 0.5
  37. let topP: Float = 0.75
  38. let topK = 40
  39. let candidateCount = 2
  40. let maxOutputTokens = 256
  41. let presencePenalty: Float = 0.5
  42. let frequencyPenalty: Float = 0.75
  43. let stopSequences = ["END", "DONE"]
  44. let responseMIMEType = "application/json"
  45. let generationConfig = GenerationConfig(
  46. temperature: temperature,
  47. topP: topP,
  48. topK: topK,
  49. candidateCount: candidateCount,
  50. maxOutputTokens: maxOutputTokens,
  51. presencePenalty: presencePenalty,
  52. frequencyPenalty: frequencyPenalty,
  53. stopSequences: stopSequences,
  54. responseMIMEType: responseMIMEType,
  55. responseSchema: .array(items: .string())
  56. )
  57. let jsonData = try encoder.encode(generationConfig)
  58. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  59. XCTAssertEqual(json, """
  60. {
  61. "candidateCount" : \(candidateCount),
  62. "frequencyPenalty" : \(frequencyPenalty),
  63. "maxOutputTokens" : \(maxOutputTokens),
  64. "presencePenalty" : \(presencePenalty),
  65. "responseMimeType" : "\(responseMIMEType)",
  66. "responseSchema" : {
  67. "items" : {
  68. "nullable" : false,
  69. "type" : "STRING"
  70. },
  71. "nullable" : false,
  72. "type" : "ARRAY"
  73. },
  74. "stopSequences" : [
  75. "END",
  76. "DONE"
  77. ],
  78. "temperature" : \(temperature),
  79. "topK" : \(topK),
  80. "topP" : \(topP)
  81. }
  82. """)
  83. }
  84. func testEncodeGenerationConfig_jsonResponse() throws {
  85. let mimeType = "application/json"
  86. let generationConfig = GenerationConfig(
  87. responseMIMEType: mimeType,
  88. responseSchema: .object(properties: [
  89. "firstName": .string(),
  90. "lastName": .string(),
  91. "age": .integer(),
  92. ])
  93. )
  94. let jsonData = try encoder.encode(generationConfig)
  95. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  96. XCTAssertEqual(json, """
  97. {
  98. "responseMimeType" : "\(mimeType)",
  99. "responseSchema" : {
  100. "nullable" : false,
  101. "properties" : {
  102. "age" : {
  103. "nullable" : false,
  104. "type" : "INTEGER"
  105. },
  106. "firstName" : {
  107. "nullable" : false,
  108. "type" : "STRING"
  109. },
  110. "lastName" : {
  111. "nullable" : false,
  112. "type" : "STRING"
  113. }
  114. },
  115. "required" : [
  116. "age",
  117. "firstName",
  118. "lastName"
  119. ],
  120. "type" : "OBJECT"
  121. }
  122. }
  123. """)
  124. }
  125. }