Google GenAI チャット

Google GenAI API (英語) を使用すると、開発者は Gemini 開発者 API または Vertex AI を介して、Google の Gemini モデルを用いた生成 AI アプリケーションを構築できます。Google GenAI API は、マルチモーダルプロンプトを入力としてサポートし、テキストまたはコードを出力します。マルチモーダルモデルは、イメージ、動画、テキストなど、複数のモダリティからの情報を処理できます。たとえば、クッキーの写真をモデルに送信し、そのクッキーのレシピを確認することができます。

Gemini は、Google と DeepMind によって開発された、マルチモーダルユースケース向けに設計された生成 AI モデルファミリーです。Gemini API を使用すると、Gemini 2.0 フラッシュ (英語) Gemini 2.0 フラッシュライト (英語) 、すべての Gemini プロ (英語) モデル、最新の Gemini 3 プロ (英語) までアクセスできます。

この実装では、次の 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>

自動構成

Spring AI 自動構成、スターターモジュールのアーティファクト名に大きな変更がありました。詳細については、アップグレードノートを参照してください。

Spring AI は、Google GenAI チャットクライアント用の Spring Boot 自動構成を提供します。これを有効にするには、プロジェクトの Maven pom.xml または Gradle build.gradle ビルドファイルに以下の依存関係を追加してください。

  • Maven

  • Gradle

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-google-genai</artifactId>
</dependency>
dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-google-genai'
}
Spring AI BOM をビルドファイルに追加するには、"依存関係管理" セクションを参照してください。

チャットのプロパティ

チャットの自動構成の有効化と無効化は、プレフィックス spring.ai.model.chat を持つ最上位プロパティを介して設定されるようになりました。

有効にするには、spring.ai.model.chat=google-genai (デフォルトで有効になっています)

無効にするには、spring.ai.model.chat=none (または google-genai と一致しない値)

この変更は、複数のモデルの構成を可能にするために行われます。

接続プロパティ

プレフィックス spring.ai.google.genai は、Google GenAI に接続できるようにするプロパティプレフィックスとして使用されます。

プロパティ 説明 デフォルト

spring.ai.model.chat

チャットモデルクライアントを有効にする

google-genai

spring.ai.google.genai.api-key

Gemini 開発者 API の API キー。指定すると、クライアントは Vertex AI ではなく Gemini 開発者 API を使用します。

-

spring.ai.google.genai.project-id

Google クラウドプラットフォームプロジェクト ID (Vertex AI モードに必要)

-

spring.ai.google.genai.location

Google クラウド領域 (Vertex AI モードに必要)

-

spring.ai.google.genai.credentials-uri

Google クラウド認証情報への URI。指定すると、認証用の GoogleCredentials インスタンスの作成に使用されます。

-

チャットモデルのプロパティ

プレフィックス spring.ai.google.genai.chat は、Google GenAI Chat のチャットモデルの実装を構成できるプロパティプレフィックスです。

プロパティ 説明 デフォルト

spring.ai.google.genai.chat.options.model

使用できる Google GenAI チャットモデル (英語) には gemini-2.0-flashgemini-2.0-flash-litegemini-progemini-1.5-flash が含まれます。

ジェミニ 2.0 フラッシュ

spring.ai.google.genai.chat.options.response-mime-type

生成された候補テキストのレスポンス MIME 型を出力します。

text/plain: (default) Text output or application/json: JSON response.

spring.ai.google.genai.chat.options.google-search-retrieval

Use Google search Grounding feature

true or false, default false.

spring.ai.google.genai.chat.options.temperature

Controls the randomness of the output. Values can range over [0.0,1.0], inclusive. A value closer to 1.0 will produce responses that are more varied, while a value closer to 0.0 will typically result in less surprising responses from the generative.

0.7

spring.ai.google.genai.chat.options.top-k

The maximum number of tokens to consider when sampling. The generative uses combined Top-k and nucleus sampling. Top-k sampling considers the set of topK most probable tokens.

-

spring.ai.google.genai.chat.options.top-p

The maximum cumulative probability of tokens to consider when sampling. The generative uses combined Top-k and nucleus sampling. Nucleus sampling considers the smallest set of tokens whose probability sum is at least topP.

-

spring.ai.google.genai.chat.options.candidate-count

The number of generated response messages to return. This value must be between [1, 8], inclusive. Defaults to 1.

1

spring.ai.google.genai.chat.options.max-output-tokens

The maximum number of tokens to generate.

-

spring.ai.google.genai.chat.options.frequency-penalty

Frequency penalties for reducing repetition.

-

spring.ai.google.genai.chat.options.presence-penalty

Presence penalties for reducing repetition.

-

spring.ai.google.genai.chat.options.thinking-budget

Thinking budget for the thinking process. See Thinking Configuration.

-

spring.ai.google.genai.chat.options.thinking-level

The level of thinking tokens the model should generate. Valid values: LOW, HIGH, THINKING_LEVEL_UNSPECIFIED. See Thinking Configuration.

-

spring.ai.google.genai.chat.options.include-thoughts

Enable thought signatures for function calling. Required for Gemini 3 Pro to avoid validation errors during the internal tool execution loop. See Thought Signatures.

false

spring.ai.google.genai.chat.options.tool-names

List of tools, identified by their names, to enable for function calling in a single prompt request. Tools with those names must exist in the ToolCallback registry.

-

spring.ai.google.genai.chat.options.tool-callbacks

Tool Callbacks to register with the ChatModel.

-

spring.ai.google.genai.chat.options.internal-tool-execution-enabled

If true, the tool execution should be performed, otherwise the response from the model is returned back to the user. Default is null, but if it’s null, ToolCallingChatOptions.DEFAULT_TOOL_EXECUTION_ENABLED which is true will take into account

-

spring.ai.google.genai.chat.options.safety-settings

List of safety settings to control safety filters, as defined by Google GenAI Safety Settings (英語) . Each safety setting can have a method, threshold, and category.

-

spring.ai.google.genai.chat.options.cached-content-name

The name of cached content to use for this request. When set along with use-cached-content=true, the cached content will be used as context. See Cached Content.

-

spring.ai.google.genai.chat.options.use-cached-content

Whether to use cached content if available. When true and cached-content-name is set, the system will use the cached content.

false

spring.ai.google.genai.chat.options.auto-cache-threshold

Automatically cache prompts that exceed this token threshold. When set, prompts larger than this value will be automatically cached for reuse. Set to null to disable auto-caching.

-

spring.ai.google.genai.chat.options.auto-cache-ttl

Time-to-live (Duration) for auto-cached content in ISO-8601 format (e.g., PT1H for 1 hour). Used when auto-caching is enabled.

PT1H

spring.ai.google.genai.chat.enable-cached-content

Enable the GoogleGenAiCachedContentService bean for managing cached content.

true

All properties prefixed with spring.ai.google.genai.chat.options can be overridden at runtime by adding a request specific Runtime options to the Prompt call.

Runtime options

The GoogleGenAiChatOptions.java [GitHub] (英語) provides model configurations, such as the temperature, the topK, etc.

On start-up, the default options can be configured with the GoogleGenAiChatModel(client, options) constructor or the spring.ai.google.genai.chat.options.* properties.

At runtime, you can override the default options by adding new, request specific, options to the Prompt call. For example, to override the default temperature for a specific request:

ChatResponse response = chatModel.call(
    new Prompt(
        "Generate the names of 5 famous pirates.",
        GoogleGenAiChatOptions.builder()
            .temperature(0.4)
        .build()
    ));
In addition to the model specific GoogleGenAiChatOptions you can use a portable ChatOptions [GitHub] (英語) instance, created with the ChatOptions#builder() [GitHub] (英語) .

Tool Calling

The Google GenAI model supports tool calling (function calling) capabilities, allowing models to use tools during conversations. Here’s an example of how to define and use @Tool-based tools:

public class WeatherService {

    @Tool(description = "Get the weather in location")
    public String weatherByLocation(@ToolParam(description= "City or state name") String location) {
        ...
    }
}

String response = ChatClient.create(this.chatModel)
        .prompt("What's the weather like in Boston?")
        .tools(new WeatherService())
        .call()
        .content();

You can use the java.util.function beans as tools as well:

@Bean
@Description("Get the weather in location. Return temperature in 36°F or 36°C format.")
public Function<Request, Response> weatherFunction() {
    return new MockWeatherService();
}

String response = ChatClient.create(this.chatModel)
        .prompt("What's the weather like in Boston?")
        .toolNames("weatherFunction")
        .inputType(Request.class)
        .call()
        .content();

Find more in Tools documentation.

Thinking Configuration

Gemini models support a "thinking" capability that allows the model to perform deeper reasoning before generating responses. This is controlled through the ThinkingConfig which includes three related options: thinkingBudget, thinkingLevel, and includeThoughts.

Thinking Level

The thinkingLevel option controls the depth of reasoning tokens the model generates. This is available for models that support thinking (e.g., Gemini 3 Pro Preview).

Value Description

LOW

最小限の思考。詳細な分析よりもスピードが優先されるシンプルなクエリに使用します。

HIGH

広範な思考。深い分析と段階的な推論を必要とする複雑な問題に活用します。

THINKING_LEVEL_UNSPECIFIED

モデルはデフォルトの動作を使用します。

プロパティによる設定

spring.ai.google.genai.chat.options.model=gemini-3-pro-preview
spring.ai.google.genai.chat.options.thinking-level=HIGH

プログラムによる構成

import org.springframework.ai.google.genai.common.GoogleGenAiThinkingLevel;

ChatResponse response = chatModel.call(
    new Prompt(
        "Explain the theory of relativity in simple terms.",
        GoogleGenAiChatOptions.builder()
            .model("gemini-3-pro-preview")
            .thinkingLevel(GoogleGenAiThinkingLevel.HIGH)
            .build()
    ));

予算を考える

thinkingBudget オプションは、思考プロセスのトークン予算を設定します。

  • Positive value : 思考のためのトークンの最大数 (例: 8192)

  • ゼロ (0) : 思考を完全に無効にする

  • 未設定 : モデルはクエリの複雑さに基づいて自動的に決定します

ChatResponse response = chatModel.call(
    new Prompt(
        "Solve this complex math problem step by step.",
        GoogleGenAiChatOptions.builder()
            .model("gemini-2.5-pro")
            .thinkingBudget(8192)
            .build()
    ));

オプションの互換性

thinkingLevel and thinkingBudget are mutually exclusive。同じリクエストで両方を使用することはできません。使用すると、API エラーが発生します。

  • Gemini 3 プロモデルには thinkingLevel (LOWHIGH)を使用してください。

  • Gemini 2.5 シリーズモデルでは thinkingBudget (トークンカウント)を使用します

includeThoughts は、thinkingLevel または thinkingBudget のいずれかと組み合わせることができます (両方を組み合わせることはできません)。

// For Gemini 3 Pro: use thinkingLevel + includeThoughts
ChatResponse response = chatModel.call(
    new Prompt(
        "Analyze this complex scenario.",
        GoogleGenAiChatOptions.builder()
            .model("gemini-3-pro-preview")
            .thinkingLevel(GoogleGenAiThinkingLevel.HIGH)
            .includeThoughts(true)
            .build()
    ));

// For Gemini 2.5: use thinkingBudget + includeThoughts
ChatResponse response = chatModel.call(
    new Prompt(
        "Analyze this complex scenario.",
        GoogleGenAiChatOptions.builder()
            .model("gemini-2.5-pro")
            .thinkingBudget(8192)
            .includeThoughts(true)
            .build()
    ));

モデルサポート

思考構成オプションはモデルによって異なります。

モデル thinkingLevelthinkingBudget ノート

Gemini 3 プロ (プレビュー)

✅ サポートされています

⚠️ 下位互換性のみ

thinkingLevel を使用してください。思考を無効化できません。グローバルエンドポイントが必要です。

Gemini 2.5 プロ

❌ サポートされていません

✅ サポートされています

thinkingBudget を使用します。無効にするには 0 に設定し、動的にするには -1 に設定します。

Gemini 2.5 フラッシュ

❌ サポートされていません

✅ サポートされています

thinkingBudget を使用します。無効にするには 0 に設定し、動的にするには -1 に設定します。

Gemini 2.5 フラッシュライト

❌ サポートされていません

✅ サポートされています

デフォルトでは思考は無効になっています。有効にするには thinkingBudget を設定してください。

Gemini 2.0 フラッシュ

❌ サポートされていません

❌ サポートされていません

思考は利用できません。

  • サポートされていないモデル (Gemini 2.5 以前など) で thinkingLevel を使用すると、API エラーが発生します。

  • Gemini 3 Pro Preview is only available on global endpoints. Set spring.ai.google.genai.location=global or GOOGLE_CLOUD_LOCATION=global.

  • Check the Google GenAI Thinking documentation (英語) for the latest model capabilities.

Enabling thinking features increases token usage and API costs. Use appropriately based on the complexity of your queries.

Thought Signatures

Gemini 3 Pro introduces thought signatures, which are opaque byte arrays that preserve the model’s reasoning context during function calling. When includeThoughts is enabled, the model returns thought signatures that must be passed back within the same turn during the internal tool execution loop.

When Thought Signatures Matter

IMPORTANT : Thought signature validation only applies to the current turn - specifically during the internal tool execution loop when the model makes function calls (both parallel and sequential). The API does not validate thought signatures for previous turns in conversation history.

  • Validation is enforced for function calls within the current turn only

  • Previous turn signatures do not need to be preserved

  • Missing signatures in the current turn’s function calls result in HTTP 400 errors for Gemini 3 Pro

  • For parallel function calls, only the first functionCall part carries the signature

For Gemini 2.5 Pro and earlier models, thought signatures are optional and the API is lenient.

構成

Enable thought signatures using configuration properties:

spring.ai.google.genai.chat.options.model=gemini-3-pro-preview
spring.ai.google.genai.chat.options.include-thoughts=true

Or programmatically at runtime:

ChatResponse response = chatModel.call(
    new Prompt(
        "Your question here",
        GoogleGenAiChatOptions.builder()
            .model("gemini-3-pro-preview")
            .includeThoughts(true)
            .toolCallbacks(callbacks)
            .build()
    ));

Automatic Handling

Spring AI automatically handles thought signatures during the internal tool execution loop. When internalToolExecutionEnabled is true (the default), Spring AI:

  1. 抽出 thought signatures from model responses

  2. Attaches them to the correct functionCall parts when sending back function responses

  3. Propagates them correctly during function calls within a single turn (both parallel and sequential)

You don’t need to manually manage thought signatures - Spring AI ensures they are properly attached to functionCall parts as required by the API specification.

Example with Function Calling

@Bean
@Description("Get the weather in a location")
public Function<WeatherRequest, WeatherResponse> weatherFunction() {
    return new WeatherService();
}

// Enable includeThoughts for Gemini 3 Pro with function calling
String response = ChatClient.create(this.chatModel)
        .prompt("What's the weather like in Boston?")
        .options(GoogleGenAiChatOptions.builder()
            .model("gemini-3-pro-preview")
            .includeThoughts(true)
            .build())
        .toolNames("weatherFunction")
        .call()
        .content();

Manual Tool Execution Mode

If you set internalToolExecutionEnabled=false to manually control the tool execution loop, you must handle thought signatures yourself when using Gemini 3 Pro with includeThoughts=true.

Requirements for manual tool execution with thought signatures:

  1. Extract thought signatures from the response metadata:

    AssistantMessage assistantMessage = response.getResult().getOutput();
    Map<String, Object> metadata = assistantMessage.getMetadata();
    List<byte[]> thoughtSignatures = (List<byte[]>) metadata.get("thoughtSignatures");
  2. When sending back function responses, include the original AssistantMessage with its metadata intact in your message history. Spring AI will automatically attach the thought signatures to the correct functionCall parts.

  3. For Gemini 3 Pro, failing to preserve thought signatures during the current turn will result in HTTP 400 errors from the API.

Only the current turn’s function calls require thought signatures. When starting a new conversation turn (after completing a function calling round), you do not need to preserve the previous turn’s signatures.
Enabling includeThoughts increases token usage as thought processes are included in responses. This impacts API costs but provides better reasoning transparency.

マルチモーダル

マルチモダリティとは、textpdfimagesaudio やその他のデータ形式を含むさまざまな(入力)ソースからの情報を同時に理解して処理するモデルの機能を指します。

イメージ、音声、ビデオ

Google の Gemini AI モデルは、テキスト、コード、オーディオ、イメージ、ビデオを理解して統合することにより、この機能をサポートします。詳細については、ブログ投稿 Gemini の導入 (英語) を参照してください。

Spring AI の Message インターフェースは、メディア型を導入することでマルチモーダル AI モデルをサポートします。この型には、生のメディアデータに Spring の org.springframework.util.MimeType および java.lang.Object を使用して、メッセージ内のメディア添付ファイルに関するデータと情報が含まれます。

以下は、GoogleGenAiChatModelIT.java [GitHub] (英語) から抽出された簡単なコード例で、ユーザーテキストとイメージの組み合わせを示しています。

byte[] data = new ClassPathResource("/vertex-test.png").getContentAsByteArray();

var userMessage = UserMessage.builder()
			.text("Explain what do you see o this picture?")
			.media(List.of(new Media(MimeTypeUtils.IMAGE_PNG, data)))
			.build();

ChatResponse response = chatModel.call(new Prompt(List.of(this.userMessage)));

PDF

Google GenAI provides support for PDF input types. Use the application/pdf media type to attach a PDF file to the message:

var pdfData = new ClassPathResource("/spring-ai-reference-overview.pdf");

var userMessage = UserMessage.builder()
			.text("You are a very professional document summarization specialist. Please summarize the given document.")
			.media(List.of(new Media(new MimeType("application", "pdf"), pdfData)))
			.build();

var response = this.chatModel.call(new Prompt(List.of(userMessage)));

Cached Content

Google GenAI’s コンテキストキャッシング (英語) allows you to cache large amounts of content (such as long documents, code repositories, or media) and reuse it across multiple requests. This significantly reduces API costs and improves response latency for repeated queries on the same content.

メリット

  • Cost Reduction : Cached tokens are billed at a much lower rate than regular input tokens (typically 75-90% cheaper)

  • Improved Performance : Reusing cached content reduces processing time for large contexts

  • 一貫性 : Same cached context ensures consistent responses across multiple requests

Cache Requirements

  • Minimum cache size: 32,768 トークン (approximately 25,000 words)

  • Maximum cache duration: 1 hour by default (configurable via TTL)

  • Cached content must include either system instructions or conversation history

Using Cached Content Service

Spring AI provides GoogleGenAiCachedContentService for programmatic cache management. The service is automatically configured when using the Spring Boot auto-configuration.

Creating Cached Content

@Autowired
private GoogleGenAiCachedContentService cachedContentService;

// Create cached content with a large document
String largeDocument = "... your large context here (>32k tokens) ...";

CachedContentRequest request = CachedContentRequest.builder()
    .model("gemini-2.0-flash")
    .contents(List.of(
        Content.builder()
            .role("user")
            .parts(List.of(Part.fromText(largeDocument)))
            .build()
    ))
    .displayName("My Large Document Cache")
    .ttl(Duration.ofHours(1))
    .build();

GoogleGenAiCachedContent cachedContent = cachedContentService.create(request);
String cacheName = cachedContent.getName(); // Save this for reuse

Using Cached Content in Chat Requests

Once you’ve created cached content, reference it in your chat requests:

ChatResponse response = chatModel.call(
    new Prompt(
        "Summarize the key points from the document",
        GoogleGenAiChatOptions.builder()
            .useCachedContent(true)
            .cachedContentName(cacheName) // Use the cached content name
            .build()
    ));

または構成プロパティ経由で:

spring.ai.google.genai.chat.options.use-cached-content=true
spring.ai.google.genai.chat.options.cached-content-name=cachedContent/your-cache-name

Managing Cached Content

The GoogleGenAiCachedContentService provides comprehensive cache management:

// Retrieve cached content
GoogleGenAiCachedContent content = cachedContentService.get(cacheName);

// Update cache TTL
CachedContentUpdateRequest updateRequest = CachedContentUpdateRequest.builder()
    .ttl(Duration.ofHours(2))
    .build();
GoogleGenAiCachedContent updated = cachedContentService.update(cacheName, updateRequest);

// List all cached content
List<GoogleGenAiCachedContent> allCaches = cachedContentService.listAll();

// Delete cached content
boolean deleted = cachedContentService.delete(cacheName);

// Extend cache TTL
GoogleGenAiCachedContent extended = cachedContentService.extendTtl(cacheName, Duration.ofMinutes(30));

// Cleanup expired caches
int removedCount = cachedContentService.cleanupExpired();

Asynchronous Operations

All operations have asynchronous variants:

CompletableFuture<GoogleGenAiCachedContent> futureCache =
    cachedContentService.createAsync(request);

CompletableFuture<GoogleGenAiCachedContent> futureGet =
    cachedContentService.getAsync(cacheName);

CompletableFuture<Boolean> futureDelete =
    cachedContentService.deleteAsync(cacheName);

Auto-Caching

Spring AI can automatically cache large prompts when they exceed a specified token threshold:

# Automatically cache prompts larger than 100,000 tokens
spring.ai.google.genai.chat.options.auto-cache-threshold=100000
# Set auto-cache TTL to 1 hour
spring.ai.google.genai.chat.options.auto-cache-ttl=PT1H

Or programmatically:

ChatResponse response = chatModel.call(
    new Prompt(
        largePrompt,
        GoogleGenAiChatOptions.builder()
            .autoCacheThreshold(100000)
            .autoCacheTtl(Duration.ofHours(1))
            .build()
    ));
Auto-caching is useful for one-time large contexts. For repeated use of the same context, manually creating and referencing cached content is more efficient.

Monitoring Cache Usage

Cached content includes usage metadata accessible via the service:

GoogleGenAiCachedContent content = cachedContentService.get(cacheName);

// Check if cache is expired
boolean expired = content.isExpired();

// Get remaining TTL
Duration remaining = content.getRemainingTtl();

// Get usage metadata
CachedContentUsageMetadata metadata = content.getUsageMetadata();
if (metadata != null) {
    System.out.println("Total tokens: " + metadata.totalTokenCount().orElse(0));
}

ベストプラクティス

  1. Cache Lifetime : Set appropriate TTL based on your use case. Shorter TTLs for frequently changing content, longer for static content.

  2. Cache Naming : Use descriptive display names to identify cached content easily.

  3. クリーンアップ : Periodically clean up expired caches to maintain organization.

  4. Token Threshold : Only cache content that exceeds the minimum threshold (32,768 tokens).

  5. Cost Optimization : Reuse cached content across multiple requests to maximize cost savings.

設定例

Complete configuration example:

# Enable cached content service (enabled by default)
spring.ai.google.genai.chat.enable-cached-content=true

# Use a specific cached content
spring.ai.google.genai.chat.options.use-cached-content=true
spring.ai.google.genai.chat.options.cached-content-name=cachedContent/my-cache-123

# Auto-caching configuration
spring.ai.google.genai.chat.options.auto-cache-threshold=50000
spring.ai.google.genai.chat.options.auto-cache-ttl=PT30M

サンプルコントローラー

新しい Spring Boot プロジェクトを作成し、spring-ai-starter-model-google-genai を pom (または gradle) の依存関係に追加します。

Add a application.properties file, under the src/main/resources directory, to enable and configure the Google GenAI chat model:

Gemini 開発者 API の使用 (API キー)

spring.ai.google.genai.api-key=YOUR_API_KEY
spring.ai.google.genai.chat.options.model=gemini-2.0-flash
spring.ai.google.genai.chat.options.temperature=0.5

Vertex AI の使用

spring.ai.google.genai.project-id=PROJECT_ID
spring.ai.google.genai.location=LOCATION
spring.ai.google.genai.chat.options.model=gemini-2.0-flash
spring.ai.google.genai.chat.options.temperature=0.5
project-id を Google クラウドプロジェクト ID に置き換え、location は us-central1europe-west1 などの Google クラウドリージョンに置き換えます。

各モデルには独自のサポート対象リージョンのセットがあり、サポート対象リージョンのリストはモデルページで確認できます。

これにより、クラスに挿入できる GoogleGenAiChatModel 実装が作成されます。以下は、テキスト生成にチャットモデルを使用する単純な @Controller クラスの例です。

@RestController
public class ChatController {

    private final GoogleGenAiChatModel chatModel;

    @Autowired
    public ChatController(GoogleGenAiChatModel chatModel) {
        this.chatModel = chatModel;
    }

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", this.chatModel.call(message));
    }

    @GetMapping("/ai/generateStream")
	public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return this.chatModel.stream(prompt);
    }
}

手動構成

The GoogleGenAiChatModel [GitHub] (英語) implements the ChatModel and uses the com.google.genai.Client to connect to the Google GenAI service.

spring-ai-google-genai 依存関係をプロジェクトの Maven pom.xml ファイルに追加します。

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-google-genai</artifactId>
</dependency>

または、Gradle build.gradle ビルドファイルに保存します。

dependencies {
    implementation 'org.springframework.ai:spring-ai-google-genai'
}
Spring AI BOM をビルドファイルに追加するには、"依存関係管理" セクションを参照してください。

次に、GoogleGenAiChatModel を作成し、テキスト生成に使用します。

API キーの使用

Client genAiClient = Client.builder()
    .apiKey(System.getenv("GOOGLE_API_KEY"))
    .build();

var chatModel = new GoogleGenAiChatModel(genAiClient,
    GoogleGenAiChatOptions.builder()
        .model(ChatModel.GEMINI_2_0_FLASH)
        .temperature(0.4)
    .build());

ChatResponse response = this.chatModel.call(
    new Prompt("Generate the names of 5 famous pirates."));

Vertex AI の使用

Client genAiClient = Client.builder()
    .project(System.getenv("GOOGLE_CLOUD_PROJECT"))
    .location(System.getenv("GOOGLE_CLOUD_LOCATION"))
    .vertexAI(true)
    .build();

var chatModel = new GoogleGenAiChatModel(genAiClient,
    GoogleGenAiChatOptions.builder()
        .model(ChatModel.GEMINI_2_0_FLASH)
        .temperature(0.4)
    .build());

ChatResponse response = this.chatModel.call(
    new Prompt("Generate the names of 5 famous pirates."));

GoogleGenAiChatOptions は、チャットリクエストの構成情報を提供します。GoogleGenAiChatOptions.Builder は流れるようなオプションビルダーです。

Migration from Vertex AI Gemini

If you’re currently using the Vertex AI Gemini implementation (spring-ai-vertex-ai-gemini), you can migrate to Google GenAI with minimal changes:

主な違い

  1. SDK : Google GenAI uses the new com.google.genai.Client instead of com.google.cloud.vertexai.VertexAI

  2. 認証 : API キーと Google クラウド認証情報の両方をサポート

  3. パッケージ名 : クラスは org.springframework.ai.vertexai.gemini ではなく org.springframework.ai.google.genai にあります

  4. プロパティ接頭辞 spring.ai.vertex.ai.gemini の代わりに spring.ai.google.genai を使用する

Google GenAI と Vertex AI Gemini の使い分け

Google GenAI を使用する場合 : - API キーを使用して迅速にプロトタイピングしたい - 開発者 API の最新の Gemini 機能が必要 - API キーと Vertex AI モードを柔軟に切り替えたい

Vertex AI Gemini を使用する場合 : - 既存の Vertex AI インフラストラクチャがある - 特定の Vertex AI エンタープライズ機能が必要 - 組織で Google クラウドのみのデプロイが必要

低レベル Java クライアント

Google GenAI 実装は、Gemini モデルにアクセスするための最新の合理化された API を提供する新しい Google GenAI Java SDK 上に構築されています。