Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions firebaseai/src/Candidate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ public enum FinishReason {
/// Token generation was stopped because the function call generated by the model was invalid.
/// </summary>
MalformedFunctionCall,
/// <summary>
/// Image generation stopped because of safety reasons.
/// </summary>
ImageSafety,
/// <summary>
/// Image generation stopped because generated images has other prohibited content.
/// </summary>
ImageProhibitedContent,
/// <summary>
/// Image generation stopped due to recitation.
/// </summary>
ImageRecitation,
/// <summary>
/// Image generation stopped because of other miscellaneous issue.
/// </summary>
ImageOther,
/// <summary>
/// The model was expected to generate an image, but none was generated.
/// </summary>
NoImage,
}

/// <summary>
Expand Down Expand Up @@ -130,6 +150,11 @@ private static FinishReason ParseFinishReason(string str) {
"PROHIBITED_CONTENT" => Firebase.AI.FinishReason.ProhibitedContent,
"SPII" => Firebase.AI.FinishReason.SPII,
"MALFORMED_FUNCTION_CALL" => Firebase.AI.FinishReason.MalformedFunctionCall,
"IMAGE_SAFETY" => Firebase.AI.FinishReason.ImageSafety,
"IMAGE_PROHIBITED_CONTENT" => Firebase.AI.FinishReason.ImageProhibitedContent,
"IMAGE_RECITATION" => Firebase.AI.FinishReason.ImageRecitation,
"IMAGE_OTHER" => Firebase.AI.FinishReason.ImageOther,
"NO_IMAGE" => Firebase.AI.FinishReason.NoImage,
_ => Firebase.AI.FinishReason.Unknown,
};
}
Expand Down
6 changes: 5 additions & 1 deletion firebaseai/src/GenerationConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public readonly struct GenerationConfig {
private readonly Schema _responseSchema;
private readonly List<ResponseModality> _responseModalities;
private readonly ThinkingConfig? _thinkingConfig;
private readonly ImageConfig? _imageConfig;

/// <summary>
/// Creates a new `GenerationConfig` value.
Expand Down Expand Up @@ -168,7 +169,8 @@ public GenerationConfig(
string responseMimeType = null,
Schema responseSchema = null,
IEnumerable<ResponseModality> responseModalities = null,
ThinkingConfig? thinkingConfig = null) {
ThinkingConfig? thinkingConfig = null,
ImageConfig? imageConfig = null) {
_temperature = temperature;
_topP = topP;
_topK = topK;
Expand All @@ -182,6 +184,7 @@ public GenerationConfig(
_responseModalities = responseModalities != null ?
new List<ResponseModality>(responseModalities) : null;
_thinkingConfig = thinkingConfig;
_imageConfig = imageConfig;
}

/// <summary>
Expand All @@ -205,6 +208,7 @@ internal Dictionary<string, object> ToJson() {
_responseModalities.Select(EnumConverters.ResponseModalityToString).ToList();
}
if (_thinkingConfig != null) jsonDict["thinkingConfig"] = _thinkingConfig?.ToJson();
if (_imageConfig != null) jsonDict["imageConfig"] = _imageConfig?.ToJson();

return jsonDict;
}
Expand Down
107 changes: 107 additions & 0 deletions firebaseai/src/ImageConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2025 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 not 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.
*/

namespace Firebase.AI {

/// <summary>
/// An aspect ratio for images.
/// </summary>
public enum AspectRatio {
/// <summary>
/// Square (1:1) aspect ratio.
/// </summary>
SQUARE_1_1,
/// <summary>
/// Portrait (2:3) aspect ratio.
/// </summary>
PORTRAIT_2_3,
/// <summary>
/// Landscape (3:2) aspect ratio.
/// </summary>
LANDSCAPE_3_2,
/// <summary>
/// Portrait (3:4) aspect ratio.
/// </summary>
PORTRAIT_3_4,
/// <summary>
/// Landscape (4:3) aspect ratio.
/// </summary>
LANDSCAPE_4_3,
/// <summary>
/// Portrait (4:5) aspect ratio.
/// </summary>
PORTRAIT_4_5,
/// <summary>
/// Landscape (5:4) aspect ratio.
/// </summary>
LANDSCAPE_5_4,
/// <summary>
/// Portrait (9:16) aspect ratio.
/// </summary>
PORTRAIT_9_16,
/// <summary>
/// Landscape (16:9) aspect ratio.
/// </summary>
LANDSCAPE_16_9,
/// <summary>
/// Landscape (21:9) aspect ratio.
/// </summary>
LANDSCAPE_21_9,
}

/// <summary>
/// Configuration options for generating images.
/// </summary>
public readonly struct ImageConfig {
/// <summary>
/// The aspect ratio of generated images.
/// </summary>
public AspectRatio? AspectRatio { get; }

/// <summary>
/// Initializes configuration options for generating images.
/// </summary>
/// <param name="aspectRatio">The aspect ratio of generated images.</param>
public ImageConfig(AspectRatio? aspectRatio = null) {
AspectRatio = aspectRatio;
}

private static string ConvertAspectRatio(AspectRatio aspectRatio) {
return aspectRatio switch {
AspectRatio.SQUARE_1_1 => "1:1",
AspectRatio.PORTRAIT_2_3 => "2:3",
AspectRatio.LANDSCAPE_3_2 => "3:2",
AspectRatio.PORTRAIT_3_4 => "3:4",
AspectRatio.LANDSCAPE_4_3 => "4:3",
AspectRatio.PORTRAIT_4_5 => "4:5",
AspectRatio.LANDSCAPE_5_4 => "5:4",
AspectRatio.PORTRAIT_9_16 => "9:16",
AspectRatio.LANDSCAPE_16_9 => "16:9",
AspectRatio.LANDSCAPE_21_9 => "21:9",
_ => aspectRatio.ToString(), // Fallback
};
}

internal Dictionary<string, object> ToJson() {
var jsonDict = new System.Collections.Generic.Dictionary<string, object>();
if (AspectRatio.HasValue) {
jsonDict["aspectRatio"] = ConvertAspectRatio(AspectRatio.Value);
}
return jsonDict;
}
}

}