GenerationConfigTests.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 11.0, macCatalyst 15.0, tvOS 15.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 stopSequences = ["END", "DONE"]
  42. let responseMIMEType = "text/plain"
  43. let generationConfig = GenerationConfig(
  44. temperature: temperature,
  45. topP: topP,
  46. topK: topK,
  47. candidateCount: candidateCount,
  48. maxOutputTokens: maxOutputTokens,
  49. stopSequences: stopSequences,
  50. responseMIMEType: responseMIMEType
  51. )
  52. let jsonData = try encoder.encode(generationConfig)
  53. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  54. XCTAssertEqual(json, """
  55. {
  56. "candidateCount" : \(candidateCount),
  57. "maxOutputTokens" : \(maxOutputTokens),
  58. "responseMIMEType" : "\(responseMIMEType)",
  59. "stopSequences" : [
  60. "END",
  61. "DONE"
  62. ],
  63. "temperature" : \(temperature),
  64. "topK" : \(topK),
  65. "topP" : \(topP)
  66. }
  67. """)
  68. }
  69. func testEncodeGenerationConfig_responseMIMEType() throws {
  70. let mimeType = "image/jpeg"
  71. let generationConfig = GenerationConfig(responseMIMEType: mimeType)
  72. let jsonData = try encoder.encode(generationConfig)
  73. let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8))
  74. XCTAssertEqual(json, """
  75. {
  76. "responseMIMEType" : "\(mimeType)"
  77. }
  78. """)
  79. }
  80. }