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
1 change: 1 addition & 0 deletions packages/firebase_ai/firebase_ai/lib/firebase_ai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export 'src/error.dart'
ServerException,
UnsupportedUserLocation;
export 'src/firebase_ai.dart' show FirebaseAI;
export 'src/image_config.dart' show ImageConfig, AspectRatio;
export 'src/imagen/imagen_api.dart'
show
ImagenSafetySettings,
Expand Down
30 changes: 29 additions & 1 deletion packages/firebase_ai/firebase_ai/lib/src/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import 'content.dart';
import 'error.dart';
import 'image_config.dart';
import 'schema.dart';
import 'tool.dart' show Tool, ToolConfig;

Expand Down Expand Up @@ -752,7 +753,23 @@ enum FinishReason {
recitation('RECITATION'),

/// Unknown reason.
other('OTHER');
other('OTHER'),

/// Image generation stopped because generated images has safety issues.
imageSafety('IMAGE_SAFETY'),

/// Image generation stopped because generated images has other prohibited
/// content.
imageProhibitedContent('IMAGE_PROHIBITED_CONTENT'),

/// Image generation stopped due to recitation.
imageRecitation('IMAGE_RECITATION'),

/// Image generation stopped because of other miscellaneous issue.
imageOther('IMAGE_OTHER'),

/// The model was expected to generate an image, but none was generated.
noImage('NO_IMAGE');

const FinishReason(this._jsonString);

Expand All @@ -770,6 +787,11 @@ enum FinishReason {
'SAFETY' => FinishReason.safety,
'RECITATION' => FinishReason.recitation,
'OTHER' => FinishReason.other,
'IMAGE_SAFETY' => FinishReason.imageSafety,
'IMAGE_PROHIBITED_CONTENT' => FinishReason.imageProhibitedContent,
'IMAGE_RECITATION' => FinishReason.imageRecitation,
'IMAGE_OTHER' => FinishReason.imageOther,
'NO_IMAGE' => FinishReason.noImage,
_ => throw FormatException('Unhandled FinishReason format', jsonObject),
};
}
Expand Down Expand Up @@ -1105,6 +1127,7 @@ final class GenerationConfig extends BaseGenerationConfig {
this.responseSchema,
this.responseJsonSchema,
this.thinkingConfig,
this.imageConfig,
}) : assert(responseSchema == null || responseJsonSchema == null,
'responseSchema and responseJsonSchema cannot both be set.');

Expand Down Expand Up @@ -1153,6 +1176,9 @@ final class GenerationConfig extends BaseGenerationConfig {
/// support thinking.
final ThinkingConfig? thinkingConfig;

/// Config for image generation.
final ImageConfig? imageConfig;

@override
Map<String, Object?> toJson() => {
...super.toJson(),
Expand All @@ -1167,6 +1193,8 @@ final class GenerationConfig extends BaseGenerationConfig {
'responseJsonSchema': responseJsonSchema,
if (thinkingConfig case final thinkingConfig?)
'thinkingConfig': thinkingConfig.toJson(),
if (imageConfig case final imageConfig?)
'imageConfig': imageConfig.toJson(),
};
}

Expand Down
89 changes: 89 additions & 0 deletions packages/firebase_ai/firebase_ai/lib/src/image_config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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.

/// The aspect ratio for the image.
///
/// The default value is "1:1".
enum AspectRatio {
/// 1:1 aspect ratio.
ratio1x1('1:1'),

/// 2:3 aspect ratio.
ratio2x3('2:3'),

/// 3:2 aspect ratio.
ratio3x2('3:2'),

/// 3:4 aspect ratio.
ratio3x4('3:4'),

/// 4:3 aspect ratio.
ratio4x3('4:3'),

/// 4:5 aspect ratio.
ratio4x5('4:5'),

/// 5:4 aspect ratio.
ratio5x4('5:4'),

/// 9:16 aspect ratio.
ratio9x16('9:16'),

/// 16:9 aspect ratio.
ratio16x9('16:9'),

/// 21:9 aspect ratio.
ratio21x9('21:9');

const AspectRatio(this._jsonString);

final String _jsonString;

/// Convert to json format.
String toJson() => _jsonString;

/// Parse the json to [AspectRatio] object.
static AspectRatio parseValue(Object jsonObject) {
return switch (jsonObject) {
'1:1' => AspectRatio.ratio1x1,
'2:3' => AspectRatio.ratio2x3,
'3:2' => AspectRatio.ratio3x2,
'3:4' => AspectRatio.ratio3x4,
'4:3' => AspectRatio.ratio4x3,
'4:5' => AspectRatio.ratio4x5,
'5:4' => AspectRatio.ratio5x4,
'9:16' => AspectRatio.ratio9x16,
'16:9' => AspectRatio.ratio16x9,
'21:9' => AspectRatio.ratio21x9,
_ => throw FormatException('Unhandled AspectRatio format', jsonObject),
};
}

@override
String toString() => name;
}

/// Configuration options for image generation.
final class ImageConfig {
// ignore: public_member_api_docs
ImageConfig({this.aspectRatio});

/// The aspect ratio for the image. The default value is "1:1".
final AspectRatio? aspectRatio;

// ignore: public_member_api_docs
Map<String, dynamic> toJson() => {
if (aspectRatio != null) 'aspectRatio': aspectRatio!.toJson(),
};
}
Loading