-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[Vertex AI] Add ImageGenerationParameters for input to predict call #14208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
FirebaseVertexAI/Sources/Types/Internal/Imagen/ImageGenerationOutputOptions.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
struct ImageGenerationOutputOptions { | ||
let mimeType: String | ||
let compressionQuality: Int? | ||
} | ||
|
||
// MARK: - Codable Conformance | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
extension ImageGenerationOutputOptions: Encodable {} |
62 changes: 62 additions & 0 deletions
62
FirebaseVertexAI/Sources/Types/Internal/Imagen/ImageGenerationParameters.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
struct ImageGenerationParameters { | ||
let sampleCount: Int? | ||
let storageURI: String? | ||
let seed: Int32? | ||
let negativePrompt: String? | ||
let aspectRatio: String? | ||
let safetyFilterLevel: String? | ||
let personGeneration: String? | ||
let outputOptions: ImageGenerationOutputOptions? | ||
let addWatermark: Bool? | ||
let includeResponsibleAIFilterReason: Bool? | ||
} | ||
|
||
// MARK: - Codable Conformance | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
extension ImageGenerationParameters: Encodable { | ||
enum CodingKeys: String, CodingKey { | ||
case sampleCount | ||
case storageURI = "storageUri" | ||
case seed | ||
case negativePrompt | ||
case aspectRatio | ||
case safetyFilterLevel = "safetySetting" | ||
case personGeneration | ||
case outputOptions | ||
case addWatermark | ||
case includeResponsibleAIFilterReason = "includeRaiReason" | ||
} | ||
|
||
func encode(to encoder: any Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encodeIfPresent(sampleCount, forKey: .sampleCount) | ||
try container.encodeIfPresent(storageURI, forKey: .storageURI) | ||
try container.encodeIfPresent(seed, forKey: .seed) | ||
try container.encodeIfPresent(negativePrompt, forKey: .negativePrompt) | ||
try container.encodeIfPresent(aspectRatio, forKey: .aspectRatio) | ||
try container.encodeIfPresent(safetyFilterLevel, forKey: .safetyFilterLevel) | ||
try container.encodeIfPresent(personGeneration, forKey: .personGeneration) | ||
try container.encodeIfPresent(outputOptions, forKey: .outputOptions) | ||
try container.encodeIfPresent(addWatermark, forKey: .addWatermark) | ||
try container.encodeIfPresent( | ||
includeResponsibleAIFilterReason, | ||
forKey: .includeResponsibleAIFilterReason | ||
) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
FirebaseVertexAI/Tests/Unit/Types/Imagen/ImageGenerationOutputOptionsTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import XCTest | ||
|
||
@testable import FirebaseVertexAI | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
final class ImageGenerationOutputOptionsTests: XCTestCase { | ||
let encoder = JSONEncoder() | ||
|
||
override func setUp() { | ||
encoder.outputFormatting = [.sortedKeys, .prettyPrinted, .withoutEscapingSlashes] | ||
} | ||
|
||
// MARK: - Encoding Tests | ||
|
||
func testEncodeOutputOptions_jpeg_defaultCompressionQuality() throws { | ||
let mimeType = "image/jpeg" | ||
let options = ImageGenerationOutputOptions(mimeType: mimeType, compressionQuality: nil) | ||
|
||
let jsonData = try encoder.encode(options) | ||
|
||
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8)) | ||
XCTAssertEqual(json, """ | ||
{ | ||
"mimeType" : "\(mimeType)" | ||
} | ||
""") | ||
} | ||
|
||
func testEncodeOutputOptions_jpeg_customCompressionQuality() throws { | ||
let mimeType = "image/jpeg" | ||
let quality = 50 | ||
let options = ImageGenerationOutputOptions(mimeType: mimeType, compressionQuality: quality) | ||
|
||
let jsonData = try encoder.encode(options) | ||
|
||
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8)) | ||
XCTAssertEqual(json, """ | ||
{ | ||
"compressionQuality" : \(quality), | ||
"mimeType" : "\(mimeType)" | ||
} | ||
""") | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
FirebaseVertexAI/Tests/Unit/Types/Imagen/ImageGenerationParametersTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import XCTest | ||
|
||
@testable import FirebaseVertexAI | ||
|
||
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *) | ||
final class ImageGenerationParametersTests: XCTestCase { | ||
let encoder = JSONEncoder() | ||
|
||
override func setUp() { | ||
encoder.outputFormatting = [.sortedKeys, .prettyPrinted, .withoutEscapingSlashes] | ||
} | ||
|
||
// MARK: - Encoding Tests | ||
|
||
func testEncodeParameters_allSpecified() throws { | ||
let sampleCount = 4 | ||
let storageURI = "gs://bucket/folder" | ||
let seed: Int32 = 1_076_107_968 | ||
let negativePrompt = "test-negative-prompt" | ||
let aspectRatio = "16:9" | ||
let safetyFilterLevel = "block_low_and_above" | ||
let personGeneration = "allow_adult" | ||
let mimeType = "image/png" | ||
let outputOptions = ImageGenerationOutputOptions(mimeType: mimeType, compressionQuality: nil) | ||
let addWatermark = false | ||
let includeRAIReason = true | ||
let parameters = ImageGenerationParameters( | ||
sampleCount: sampleCount, | ||
storageURI: storageURI, | ||
seed: seed, | ||
negativePrompt: negativePrompt, | ||
aspectRatio: aspectRatio, | ||
safetyFilterLevel: safetyFilterLevel, | ||
personGeneration: personGeneration, | ||
outputOptions: outputOptions, | ||
addWatermark: addWatermark, | ||
includeResponsibleAIFilterReason: includeRAIReason | ||
) | ||
|
||
let jsonData = try encoder.encode(parameters) | ||
|
||
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8)) | ||
XCTAssertEqual(json, """ | ||
{ | ||
"addWatermark" : \(addWatermark), | ||
"aspectRatio" : "\(aspectRatio)", | ||
"includeRaiReason" : \(includeRAIReason), | ||
"negativePrompt" : "\(negativePrompt)", | ||
"outputOptions" : { | ||
"mimeType" : "\(mimeType)" | ||
}, | ||
"personGeneration" : "\(personGeneration)", | ||
"safetySetting" : "\(safetyFilterLevel)", | ||
"sampleCount" : \(sampleCount), | ||
"seed" : \(seed), | ||
"storageUri" : "\(storageURI)" | ||
} | ||
""") | ||
} | ||
|
||
func testEncodeParameters_someSpecified() throws { | ||
let sampleCount = 2 | ||
let aspectRatio = "3:4" | ||
let safetyFilterLevel = "block_medium_and_above" | ||
let addWatermark = true | ||
let parameters = ImageGenerationParameters( | ||
sampleCount: sampleCount, | ||
storageURI: nil, | ||
seed: nil, | ||
negativePrompt: nil, | ||
aspectRatio: aspectRatio, | ||
safetyFilterLevel: safetyFilterLevel, | ||
personGeneration: nil, | ||
outputOptions: nil, | ||
addWatermark: addWatermark, | ||
includeResponsibleAIFilterReason: nil | ||
) | ||
|
||
let jsonData = try encoder.encode(parameters) | ||
|
||
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8)) | ||
XCTAssertEqual(json, """ | ||
{ | ||
"addWatermark" : \(addWatermark), | ||
"aspectRatio" : "\(aspectRatio)", | ||
"safetySetting" : "\(safetyFilterLevel)", | ||
"sampleCount" : \(sampleCount) | ||
} | ||
""") | ||
} | ||
|
||
func testEncodeParameters_noneSpecified() throws { | ||
let parameters = ImageGenerationParameters( | ||
sampleCount: nil, | ||
storageURI: nil, | ||
seed: nil, | ||
negativePrompt: nil, | ||
aspectRatio: nil, | ||
safetyFilterLevel: nil, | ||
personGeneration: nil, | ||
outputOptions: nil, | ||
addWatermark: nil, | ||
includeResponsibleAIFilterReason: nil | ||
) | ||
|
||
let jsonData = try encoder.encode(parameters) | ||
|
||
let json = try XCTUnwrap(String(data: jsonData, encoding: .utf8)) | ||
XCTAssertEqual(json, """ | ||
{ | ||
|
||
} | ||
""") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why different names?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO
safetyFilterLevel
better expresses the meaning "Adds a filter level to safety filtering." Also planning to groupsafetyFilterLevel
(safetySetting
),personGeneration
andincludeResponsibleAIFilterReason
(includeRaiReason
) into aSafetySettings
struct in the public API so this will be more distinct. That said, all the naming is still in flux.