Skip to content

chore(vertexai): add imagen developer api support #17321

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 1 commit into from
Apr 30, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,12 @@ class _GenerativeAISampleState extends State<GenerativeAISample> {

ImagenModel _initializeImagenModel(FirebaseVertexAI instance) {
var generationConfig = ImagenGenerationConfig(
negativePrompt: 'frog',
numberOfImages: 1,
aspectRatio: ImagenAspectRatio.square1x1,
imageFormat: ImagenFormat.jpeg(compressionQuality: 75),
);
return instance.imagenModel(
model: 'imagen-3.0-generate-001',
model: 'imagen-3.0-generate-002',
generationConfig: generationConfig,
safetySettings: ImagenSafetySettings(
ImagenSafetyFilterLevel.blockLowAndAbove,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,27 @@ class _ImagenPageState extends State<ImagenPage> {
_loading = true;
});

var response = await widget.model.generateImages(prompt);
try {
var response = await widget.model.generateImages(prompt);

if (response.images.isNotEmpty) {
var imagenImage = response.images[0];
if (response.images.isNotEmpty) {
var imagenImage = response.images[0];

_generatedContent.add(
MessageData(
image: Image.memory(imagenImage.bytesBase64Encoded),
text: prompt,
fromUser: false,
),
);
} else {
// Handle the case where no images were generated
_showError('Error: No images were generated.');
_generatedContent.add(
MessageData(
image: Image.memory(imagenImage.bytesBase64Encoded),
text: prompt,
fromUser: false,
),
);
} else {
// Handle the case where no images were generated
_showError('Error: No images were generated.');
}
} catch (e) {
_showError(e.toString());
}

setState(() {
_loading = false;
_scrollDown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class FirebaseVertexAI extends FirebasePluginPlatform {
app: app,
location: location,
model: model,
useVertexBackend: _useVertexBackend,
generationConfig: generationConfig,
safetySettings: safetySettings,
appCheck: appCheck,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class ImagenModel extends BaseApiClientModel {
{required FirebaseApp app,
required String model,
required String location,
required bool useVertexBackend,
FirebaseAppCheck? appCheck,
FirebaseAuth? auth,
ImagenGenerationConfig? generationConfig,
Expand All @@ -37,11 +38,9 @@ final class ImagenModel extends BaseApiClientModel {
_safetySettings = safetySettings,
super(
serializationStrategy: VertexSerialization(),
modelUri: _VertexUri(
model: model,
app: app,
location: location,
),
modelUri: useVertexBackend
? _VertexUri(app: app, model: model, location: location)
: _GoogleAIUri(app: app, model: model),
client: HttpApiClient(
apiKey: app.options.apiKey,
requestHeaders: BaseModel.firebaseTokens(appCheck, auth, app)));
Expand Down Expand Up @@ -118,6 +117,7 @@ ImagenModel createImagenModel({
required FirebaseApp app,
required String location,
required String model,
required bool useVertexBackend,
FirebaseAppCheck? appCheck,
FirebaseAuth? auth,
ImagenGenerationConfig? generationConfig,
Expand All @@ -129,6 +129,7 @@ ImagenModel createImagenModel({
appCheck: appCheck,
auth: auth,
location: location,
useVertexBackend: useVertexBackend,
safetySettings: safetySettings,
generationConfig: generationConfig,
);
Loading