Skip to content
Open
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 @@ -35,6 +35,14 @@
<optional>true</optional>
</dependency>

<!-- Google GenAI Image -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-google-genai-image</artifactId>
<version>${project.parent.version}</version>
<optional>true</optional>
</dependency>

<!-- Spring AI auto configurations -->

<dependency>
Expand All @@ -61,6 +69,12 @@
<version>${project.parent.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-model-image-observation</artifactId>
<version>${project.parent.version}</version>
</dependency>

<!-- Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2023-present the original author or authors.
*
* 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
*
* https://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.
*/

package org.springframework.ai.model.google.genai.autoconfigure.image;

import io.micrometer.observation.ObservationRegistry;

import org.springframework.ai.google.genai.image.GoogleGenAiImageConnectionDetails;
import org.springframework.ai.google.genai.image.GoogleGenAiImageModel;
import org.springframework.ai.image.observation.ImageModelObservationConvention;
import org.springframework.ai.model.SpringAIModelProperties;
import org.springframework.ai.model.SpringAIModels;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.retry.RetryTemplate;

/**
* Auto-configuration for Google GenAI Image.
*
* @author Olivier Le Quellec
* @since 1.1.0
*/
@AutoConfiguration
@ConditionalOnClass(GoogleGenAiImageModel.class)
@ConditionalOnProperty(name = SpringAIModelProperties.IMAGE_MODEL, havingValue = SpringAIModels.GOOGLE_GEN_AI,
matchIfMissing = true)
@EnableConfigurationProperties(GoogleGenAiImageProperties.class)
public class GoogleGenAiImageAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public GoogleGenAiImageModel googleGenAiImageModel(GoogleGenAiImageConnectionDetails connectionDetails,
GoogleGenAiImageProperties imageProperties, ObjectProvider<RetryTemplate> retryTemplate,
ObjectProvider<ObservationRegistry> observationRegistry,
ObjectProvider<ImageModelObservationConvention> observationConvention) {

var imageModel = new GoogleGenAiImageModel(connectionDetails, imageProperties.toOptions(),
retryTemplate.getIfUnique(() -> RetryUtils.DEFAULT_RETRY_TEMPLATE),
observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP));

observationConvention.ifAvailable(imageModel::setObservationConvention);

return imageModel;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2023-present the original author or authors.
*
* 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
*
* https://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.
*/

package org.springframework.ai.model.google.genai.autoconfigure.image;

import java.io.IOException;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.genai.Client;

import org.springframework.ai.google.genai.image.GoogleGenAiImageConnectionDetails;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Auto-configuration for Google GenAI Image Connection.
*
* @author Olivier Le Quellec
* @since 1.1.0
*/
@AutoConfiguration
@ConditionalOnClass({ Client.class, GoogleGenAiImageConnectionDetails.class })
@EnableConfigurationProperties(GoogleGenAiImageConnectionProperties.class)
public class GoogleGenAiImageConnectionAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public GoogleGenAiImageConnectionDetails googleGenAiImageConnectionDetails(
GoogleGenAiImageConnectionProperties connectionProperties) throws IOException {

var connectionBuilder = GoogleGenAiImageConnectionDetails.builder();

if (StringUtils.hasText(connectionProperties.getApiKey())) {
// Gemini Developer API mode
connectionBuilder.apiKey(connectionProperties.getApiKey());
}
else {
// Vertex AI mode
Assert.hasText(connectionProperties.getProjectId(), "Google GenAI project-id must be set!");
Assert.hasText(connectionProperties.getLocation(), "Google GenAI location must be set!");

connectionBuilder.projectId(connectionProperties.getProjectId())
.location(connectionProperties.getLocation());

if (connectionProperties.getCredentialsUri() != null) {
GoogleCredentials credentials = GoogleCredentials
.fromStream(connectionProperties.getCredentialsUri().getInputStream());
// Note: Credentials are handled automatically by the SDK when using
// Vertex AI mode
}
}

return connectionBuilder.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2023-present the original author or authors.
*
* 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
*
* https://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.
*/

package org.springframework.ai.model.google.genai.autoconfigure.image;

import org.jspecify.annotations.Nullable;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.Resource;

/**
* Connection properties for the Google GenAI Image.
*
* @author Olivier Le Quellec
* @since 1.1.0
*/
@ConfigurationProperties(GoogleGenAiImageConnectionProperties.CONFIG_PREFIX)
public class GoogleGenAiImageConnectionProperties {

public static final String CONFIG_PREFIX = "spring.ai.google.genai.image";

/**
* Google GenAI API Key (for Gemini Developer API mode).
*/
private @Nullable String apiKey;

/**
* Google Cloud project ID (for Vertex AI mode).
*/
private @Nullable String projectId;

/**
* Google Cloud location (for Vertex AI mode).
*/
private @Nullable String location;

/**
* URI to Google Cloud credentials (optional, for Vertex AI mode).
*/
private @Nullable Resource credentialsUri;

/**
* Whether to use Vertex AI mode. If false, uses Gemini Developer API mode. This is
* automatically determined based on whether apiKey or projectId is set.
*/
private boolean vertexAi;

public @Nullable String getApiKey() {
return this.apiKey;
}

public void setApiKey(@Nullable String apiKey) {
this.apiKey = apiKey;
}

public @Nullable String getProjectId() {
return this.projectId;
}

public void setProjectId(@Nullable String projectId) {
this.projectId = projectId;
}

public @Nullable String getLocation() {
return this.location;
}

public void setLocation(@Nullable String location) {
this.location = location;
}

public @Nullable Resource getCredentialsUri() {
return this.credentialsUri;
}

public void setCredentialsUri(@Nullable Resource credentialsUri) {
this.credentialsUri = credentialsUri;
}

public boolean isVertexAi() {
return this.vertexAi;
}

public void setVertexAi(boolean vertexAi) {
this.vertexAi = vertexAi;
}

}
Loading