Google GenAI イメージ
The Gemini 2.5 Flash Image Model (英語) and Gemini 3.1 Flash Image Model (英語) provides image generation using Google’s image models through either the Gemini Developer API or Vertex AI.
They provide high-quality image generation and conversational editing at a mainstream price point and low latency.
It serves as the high-efficiency counterpart to Gemini 3 Pro Image Model (英語) , optimized for speed and high-volume developer use cases.
この実装では、次の 2 つの認証モードが提供されます。
Gemini 開発者 API : API キーを使用して素早いプロトタイピングと開発を行う
Vertex AI : エンタープライズ機能を備えた本番環境の デプロイには Google クラウド認証情報を使用します
前提条件
次のいずれかの認証方法を選択します。
オプション 1: Gemini 開発者 API (API キー)
Google AI スタジオ (英語) から API キーを取得する
API キーを環境変数またはアプリケーションのプロパティとして設定します
オプション 2: Vertex AI (Google クラウド)
OS に適した gcloud CLI をインストールします。
次のコマンドを実行して認証します。
PROJECT_IDを Google Cloud プロジェクト ID に置き換え、ACCOUNTを Google Cloud ユーザー名に置き換えます。
gcloud config set project <PROJECT_ID> &&
gcloud auth application-default login <ACCOUNT>リポジトリと BOM の追加
Spring AI の成果物は、Maven Central リポジトリと Spring Snapshot リポジトリに公開されています。これらのリポジトリをビルドシステムに追加するには、アーティファクトリポジトリのセクションを参照してください。
依存関係の管理を容易にするため、Spring AI には部品表(BOM)が用意されており、プロジェクト全体で一貫したバージョンの Spring AI が使用されるようになっています。Spring AI の部品表をビルドシステムに追加するには、依存関係管理のセクションを参照してください。
自動構成
Spring AI の自動構成およびスターターモジュールのアーティファクト名に大幅な変更がありました。詳細については、アップグレードノートを参照してください。 |
Spring AI provides Spring Boot auto-configuration for the Google GenAI image Model. To enable it add the following dependency to your project’s Maven pom.xml file:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-google-genai-image</artifactId>
</dependency> または、Gradle build.gradle ビルドファイルに保存します。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-google-genai-image'
}| 依存関係管理のセクションを参照して、Spring AI の部品表をビルドファイルに追加してください。 |
Image generation Properties
接続プロパティ
The prefix spring.ai.google.genai.image is used as the property prefix that lets you connect to Google GenAI image generation API.
The connection properties are shared with the Google GenAI Chat module. If you’re using both chat and image, you only need to configure the connection once using either |
| プロパティ | 説明 | デフォルト |
|---|---|---|
spring.ai.google.genai.image.api-key | Gemini 開発者 API の API キー。指定すると、クライアントは Vertex AI ではなく Gemini 開発者 API を使用します。 | - |
spring.ai.google.genai.image.project-id | Google クラウドプラットフォームプロジェクト ID (Vertex AI モードに必要) | - |
spring.ai.google.genai.image.location | Google クラウド領域 (Vertex AI モードに必要) | - |
spring.ai.google.genai.image.credentials-uri | Google クラウド認証情報への URI。指定すると、認証用の | - |
Enabling and disabling of the auto-configurations are now configured via top level properties with the prefix To enable, spring.ai.model.image=google-genai (デフォルトで有効になっています) 無効にするには、spring.ai.model.image=none (または google-genai と一致しない値) この変更は、複数のモデルの構成を可能にするために行われます。 |
Image generation Properties
The prefix spring.ai.google.genai.image is the property prefix that lets you configure the image model implementation for Google GenAI.
| プロパティ | 説明 | デフォルト |
|---|---|---|
spring.ai.model.image | Enable Google GenAI API model. | google-genai |
spring.ai.google.genai.image.n | The number of images to generate. Must be between 1 and 4. | - |
spring.ai.google.genai.image.model | The Google GenAI model (英語) to use. Supported models include | - |
spring.ai.google.genai.image.aspect-ratio | The aspect ratio of the generated images. Supported values are 1:1, 3:4, 4:3, 9:16, and 16:9 | 1:1 |
spring.ai.google.genai.image.seed | Random seed for image generation. This is not available when | - |
spring.ai.google.genai.image.safety-filter-level | Filter level for safety filtering. Supported values are BLOCK_LOW_AND_ABOVE, BLOCK_MEDIUM_AND_ABOVE, BLOCK_ONLY_HIGH and BLOCK_NONE. | - |
spring.ai.google.genai.image.person-generation | Allows generation of people by the model. Supported values are DONT_ALLOW, ALLOW_ADULT and ALLOW_ALL. | - |
spring.ai.google.genai.image.output-mime-type | MIME type of the generated image. | - |
spring.ai.google.genai.image.output-compression-quality | Compression quality of the generated image (for | - |
spring.ai.google.genai.image.labels | User specified labels to track billing usage. | - |
spring.ai.google.genai.image.image-size | The size of the largest dimension of the generated image. Supported sizes are 1K and 2K. | - |
spring.ai.google.genai.image.temperature | Controls the degree of randomness in token selection. Lower values produce less open-ended responses, higher values produce more diverse or creative results. | - |
spring.ai.google.genai.image.top-p | Tokens are selected from the most to least probable until the sum of their probabilities equals this value. | - |
spring.ai.google.genai.image.top-k | For each token selection step, the | - |
spring.ai.google.genai.image.max-output-tokens | Maximum number of tokens that can be generated in the response. | - |
サンプルコントローラー
新しい Spring Boot プロジェクトを作成し、spring-ai-starter-model-google-genai-image を pom (または gradle) の依存関係に追加します。
Add a application.properties file, under the src/main/resources directory, to enable and configure the Google GenAI image model:
Gemini 開発者 API の使用 (API キー)
spring.ai.google.genai.image.api-key=YOUR_API_KEY
spring.ai.google.genai.image.model=gemini-2.5-flash-imageVertex AI の使用
spring.ai.google.genai.image.project-id=YOUR_PROJECT_ID
spring.ai.google.genai.image.location=YOUR_PROJECT_LOCATION
spring.ai.google.genai.image.model=gemini-2.5-flash-imageThis will create a GoogleGenAiImageModel implementation that you can inject into your class. Here is an example of a simple @Controller class that uses the image model for images generations.
@RestController
public class ImageController {
private final ImageModel imageModel;
@Autowired
public ImageController(ImageModel imageModel) {
this.imageModel = imageModel;
}
@GetMapping("/ai/image")
public Map generate(@RequestParam(value = "message", defaultValue = "A painting of a sunset over a mountain") String message) {
ImagePrompt imagePrompt = new ImagePrompt(message);
ImageResponse imageResponse = this.imageModel.call(imagePrompt);
return Map.of("images", imageResponse);
}
}手動構成
GoogleGenAiImageModel [GitHub] (英語) は ImageModel を実装します。
spring-ai-google-genai-image 依存関係をプロジェクトの Maven pom.xml ファイルに追加します。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-google-genai-image</artifactId>
</dependency> または、Gradle build.gradle ビルドファイルに保存します。
dependencies {
implementation 'org.springframework.ai:spring-ai-google-genai-image'
}| 依存関係管理のセクションを参照して、Spring AI の部品表をビルドファイルに追加してください。 |
Next, create a GoogleGenAiImageModel and use it for images:
API キーの使用
GoogleGenAiImageConnectionDetails connectionDetails =
GoogleGenAiImageConnectionDetails.builder()
.apiKey(System.getenv("GOOGLE_API_KEY"))
.build();
GoogleGenAiImageOptions options = GoogleGenAiImageOptions.builder()
.model(GoogleGenAiImageOptions.DEFAULT_MODEL_NAME)
.build();
var imageModel = new GoogleGenAiImageModel(connectionDetails, options);
ImageResponse imageResponse = imageModel
.call(new ImagePrompt("A painting of a sunset over a mountain"));Vertex AI の使用
GoogleGenAiImageConnectionDetails connectionDetails =
GoogleGenAiImageConnectionDetails.builder()
.projectId(System.getenv("GOOGLE_CLOUD_PROJECT"))
.location(System.getenv("GOOGLE_CLOUD_LOCATION"))
.build();
GoogleGenAiImageOptions options = GoogleGenAiImageOptions.builder()
.model(GoogleGenAiImageOptions.DEFAULT_MODEL_NAME)
.build();
var imageModel = new GoogleGenAiImageModel(connectionDetails, options);
ImageResponse imageResponse = imageModel
.call(new ImagePrompt("A painting of a sunset over a mountain"));Safety Filter Levels
The Google GenAI image API supports different filter levels for safety filtering:
BLOCK_LOW_AND_ABOVE: Block when low, medium or high probability of unsafe contentBLOCK_MEDIUM_AND_ABOVE: 安全でないコンテンツの可能性が高い場合はブロックするBLOCK_ONLY_HIGH: Block when high probability of unsafe contentBLOCK_NONE: Always show regardless of probability of unsafe contentSAFETY_FILTER_LEVEL_UNSPECIFIED: Threshold is unspecified, block using default threshold
Person Generation
The Google GenAI image API allows the model to generate images of people with different levels:
DONT_ALLOW: Block generation of images of peopleALLOW_ADULT: Generate images of adults, but not childrenALLOW_ALL: Generate images that include adults and childrenPERSON_GENERATION_UNSPECIFIED: Threshold is unspecified, generate images of people using default threshold