|
| 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 | + |
| 15 | +import FirebaseVertexAI |
| 16 | +import Foundation |
| 17 | +import XCTest |
| 18 | + |
| 19 | +@available(iOS 15.0, macOS 11.0, macCatalyst 15.0, *) |
| 20 | +final class GenerationConfigTests: XCTestCase { |
| 21 | + let encoder = JSONEncoder() |
| 22 | + |
| 23 | + override func setUp() { |
| 24 | + encoder.outputFormatting = .init( |
| 25 | + arrayLiteral: .prettyPrinted, .sortedKeys, .withoutEscapingSlashes |
| 26 | + ) |
| 27 | + } |
| 28 | + |
| 29 | + // MARK: GenerationConfig Encoding |
| 30 | + |
| 31 | + func testEncodeGenerationConfig_default() throws { |
| 32 | + let generationConfig = GenerationConfig() |
| 33 | + |
| 34 | + let jsonData = try encoder.encode(generationConfig) |
| 35 | + |
| 36 | + let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8)) |
| 37 | + XCTAssertEqual(json, """ |
| 38 | + { |
| 39 | +
|
| 40 | + } |
| 41 | + """) |
| 42 | + } |
| 43 | + |
| 44 | + func testEncodeGenerationConfig_allOptions() throws { |
| 45 | + let temperature: Float = 0.5 |
| 46 | + let topP: Float = 0.75 |
| 47 | + let topK = 40 |
| 48 | + let candidateCount = 2 |
| 49 | + let maxOutputTokens = 256 |
| 50 | + let stopSequences = ["END", "DONE"] |
| 51 | + let responseMIMEType = "text/plain" |
| 52 | + let generationConfig = GenerationConfig( |
| 53 | + temperature: temperature, |
| 54 | + topP: topP, |
| 55 | + topK: topK, |
| 56 | + candidateCount: candidateCount, |
| 57 | + maxOutputTokens: maxOutputTokens, |
| 58 | + stopSequences: stopSequences, |
| 59 | + responseMIMEType: responseMIMEType |
| 60 | + ) |
| 61 | + |
| 62 | + let jsonData = try encoder.encode(generationConfig) |
| 63 | + |
| 64 | + let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8)) |
| 65 | + XCTAssertEqual(json, """ |
| 66 | + { |
| 67 | + "candidateCount" : \(candidateCount), |
| 68 | + "maxOutputTokens" : \(maxOutputTokens), |
| 69 | + "responseMIMEType" : "\(responseMIMEType)", |
| 70 | + "stopSequences" : [ |
| 71 | + "END", |
| 72 | + "DONE" |
| 73 | + ], |
| 74 | + "temperature" : \(temperature), |
| 75 | + "topK" : \(topK), |
| 76 | + "topP" : \(topP) |
| 77 | + } |
| 78 | + """) |
| 79 | + } |
| 80 | + |
| 81 | + func testEncodeGenerationConfig_responseMIMEType() throws { |
| 82 | + let mimeType = "image/jpeg" |
| 83 | + let generationConfig = GenerationConfig(responseMIMEType: mimeType) |
| 84 | + |
| 85 | + let jsonData = try encoder.encode(generationConfig) |
| 86 | + |
| 87 | + let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8)) |
| 88 | + XCTAssertEqual(json, """ |
| 89 | + { |
| 90 | + "responseMIMEType" : "\(mimeType)" |
| 91 | + } |
| 92 | + """) |
| 93 | + } |
| 94 | +} |
0 commit comments