VertexAI Gemini チャット
Vertex AI Gemini API を使用すると、開発者は Gemini モデルを使用して生成 AI アプリケーションを構築できます。Vertex AI Gemini API は、入力および出力のテキストまたはコードとしてマルチモーダルプロンプトをサポートします。マルチモーダルモデルは、イメージ、ビデオ、テキストなどの複数のモダリティからの情報を処理できるモデルです。例: モデルにクッキーの皿の写真を送り、そのクッキーのレシピを教えてもらうことができます。
Gemini は、Google DeepMind によって開発された生成 AI モデルのファミリーであり、マルチモーダルなユースケース向けに設計されています。Gemini API を使用すると、Gemini 1.0 Pro Vision および Gemini 1.0 Pro モデルにアクセスできます。Vertex AI Gemini API モデルの仕様については、モデル情報 を参照してください。
前提条件
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 は、VertexAI Gemini チャットクライアント用の Spring Boot 自動構成を提供します。これを有効にするには、プロジェクトの Maven pom.xml
または Gradle build.gradle
ビルドファイルに次の依存関係を追加します。
Maven
Gradle
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-vertex-ai-gemini-spring-boot-starter</artifactId>
</dependency>
dependencies {
implementation 'org.springframework.ai:spring-ai-vertex-ai-gemini-spring-boot-starter'
}
Spring AI BOM をビルドファイルに追加するには、"依存関係管理" セクションを参照してください。 |
チャットのプロパティ
接頭辞 spring.ai.vertex.ai.gemini
は、VertexAI への接続を可能にするプロパティ接頭辞として使用されます。
プロパティ | 説明 | デフォルト |
---|---|---|
spring.ai.vertex.ai.gemini.projectId | Google クラウドプラットフォームプロジェクト ID | - |
spring.ai.vertex.ai.gemini.location | 領域 | - |
spring.ai.vertex.ai.gemini.credentialsUri | Vertex AI Gemini 資格情報への URI。指定すると、 | - |
spring.ai.vertex.ai.gemini.apiEndpoint | Vertex AI Gemini API エンドポイント。 | - |
spring.ai.vertex.ai.gemini.scopes | - | |
spring.ai.vertex.ai.gemini.transport | API トランスポート。GRPC または REST。 | GRPC |
プレフィックス spring.ai.vertex.ai.gemini.chat
は、VertexAI Gemini チャットのチャットモデル実装を構成できるプロパティプレフィックスです。
プロパティ | 説明 | デフォルト |
---|---|---|
spring.ai.vertex.ai.gemini.chat.options.model | 使用できるサポートされている Vertex AI Gemini チャットモデル には、(1.0) | ジェミニ -1.5- プロ -001 |
spring.ai.vertex.ai.gemini.chat.options.responseMimeType | 生成された候補テキストのレスポンス MIME 型を出力します。 |
|
spring.ai.vertex.ai.gemini.chat.options.googleSearchRetrieval | Google 検索接地機能を使用する |
|
spring.ai.vertex.ai.gemini.chat.options.temperature | 出力のランダム性を制御します。値の範囲は [0.0,1.0] 以上です。1.0 に近い値は、より多様なレスポンスを生成しますが、0.0 に近い値は、通常、生成からの驚くべきレスポンスが少なくなります。この値は、ジェネレーティブの呼び出し中にバックエンドによって使用されるデフォルトを指定します。 | 0.8 |
spring.ai.vertex.ai.gemini.chat.options.topK | サンプリング時に考慮するトークンの最大数。生成では、Top-k サンプリングと核サンプリングを組み合わせて使用します。Top-k サンプリングでは、最も可能性の高い上位 K 個のトークンのセットが考慮されます。 | - |
spring.ai.vertex.ai.gemini.chat.options.topP | サンプリング時に考慮するトークンの最大累積確率。生成では、Top-k サンプリングと核サンプリングを組み合わせて使用します。nucleus サンプリングでは、確率の合計が少なくとも topP であるトークンの最小セットが考慮されます。 | - |
spring.ai.vertex.ai.gemini.chat.options.candidateCount | 返される生成されたレスポンスメッセージの数。この値は [1 ~ 8] の範囲内である必要があります。デフォルトは 1 です。 | - |
spring.ai.vertex.ai.gemini.chat.options.candidateCount | 返される生成されたレスポンスメッセージの数。この値は [1 ~ 8] の範囲内である必要があります。デフォルトは 1 です。 | - |
spring.ai.vertex.ai.gemini.chat.options.maxOutputTokens | 生成するトークンの最大数。 | - |
spring.ai.vertex.ai.gemini.chat.options.frequencyPenalty | - | |
spring.ai.vertex.ai.gemini.chat.options.presencePenalty | - | |
spring.ai.vertex.ai.gemini.chat.options.toolNames | 単一のプロンプトリクエストで関数呼び出しを有効にする、名前で識別されるツールのリスト。これらの名前のツールは、ToolCallback レジストリに存在する必要があります。 | - |
( | 単一のプロンプトリクエストで関数呼び出しを有効にするために、名前で識別される関数のリスト。これらの名前を持つ関数は、functionCallbacks レジストリに存在する必要があります。 | - |
spring.ai.vertex.ai.gemini.chat.options.proxy-tool-calls | true の場合、Spring AI は関数呼び出しを内部で処理せず、クライアントにプロキシします。関数呼び出しを処理し、適切な関数にディスパッチして、結果を返すのはクライアントの責任です。false (デフォルト) の場合、Spring AI は関数呼び出しを内部で処理します。関数呼び出しをサポートするチャットモデルにのみ適用されます。 | false |
spring.ai.vertex.ai.gemini.chat.options.safetySettings | Vertex AI 安全フィルター で定義されている、安全フィルターを制御するための安全設定のリスト。各安全設定には、方法、しきい値、カテゴリを設定できます。 | - |
spring.ai.vertex.ai.gemini.chat.options というプレフィックスが付いたすべてのプロパティは、リクエスト固有のランタイムオプションを Prompt 呼び出しに追加することで実行時にオーバーライドできます。 |
ランタイムオプション
VertexAiGeminiChatOptions.java [GitHub] (英語) は、温度、topK などのモデル構成を提供します。
起動時に、VertexAiGeminiChatModel(api, options)
コンストラクターまたは spring.ai.vertex.ai.chat.options.*
プロパティを使用してデフォルトのオプションを構成できます。
実行時に、新しいリクエスト固有のオプションを Prompt
呼び出しに追加することで、デフォルトのオプションをオーバーライドできます。たとえば、特定のリクエストのデフォルトの温度をオーバーライドするには、次のようにします。
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
VertexAiGeminiChatOptions.builder()
.temperature(0.4)
.build()
));
モデル固有の VertexAiGeminiChatOptions に加えて、ChatOptionsBuilder#builder() [GitHub] (英語) で作成されたポータブル ChatOptions [GitHub] (英語) インスタンスを使用できます。 |
ツール呼び出し
Vertex AI Gemini モデルはツール呼び出し機能をサポートしており、モデルは会話中にツールを使用できます。@Tool
ベースのツールを定義して使用する方法の例を次に示します。
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();
java.util.function Bean をツールとしても使用できます。
@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?")
.tools("weatherFunction")
.inputType(Request.class)
.call()
.content();
詳細については、ツールのドキュメントを参照してください。
マルチモーダル
マルチモダリティとは、text
、pdf
、images
、audio
やその他のデータ形式を含むさまざまなソースからの情報を同時に理解して処理するモデルの機能を指します。
イメージ、音声、ビデオ
Google の Gemini AI モデルは、テキスト、コード、オーディオ、イメージ、ビデオを理解して統合することにより、この機能をサポートします。詳細については、ブログ投稿 Gemini の導入 (英語) を参照してください。
Spring AI の Message
インターフェースは、メディア型を導入することでマルチモーダル AI モデルをサポートします。この型には、生のメディアデータに Spring の org.springframework.util.MimeType
および java.lang.Object
を使用して、メッセージ内のメディア添付ファイルに関するデータと情報が含まれます。
以下は、VertexAiGeminiChatModelIT.java [GitHub] (英語) から抽出された簡単なコード例で、ユーザーテキストとイメージの組み合わせを示しています。
byte[] data = new ClassPathResource("/vertex-test.png").getContentAsByteArray();
var userMessage = new UserMessage("Explain what do you see on this picture?",
List.of(new Media(MimeTypeUtils.IMAGE_PNG, this.data)));
ChatResponse response = chatModel.call(new Prompt(List.of(this.userMessage)));
最新の Vertex Gemini は PDF 入力型をサポートしています。メッセージに PDF ファイルを添付するには、application/pdf
メディア型を使用します。
var pdfData = new ClassPathResource("/spring-ai-reference-overview.pdf");
var userMessage = new UserMessage(
"You are a very professional document summarization specialist. Please summarize the given document.",
List.of(new Media(new MimeType("application", "pdf"), pdfData)));
var response = this.chatModel.call(new Prompt(List.of(userMessage)));
サンプルコントローラー
新しい Spring Boot プロジェクトを作成し、spring-ai-vertex-ai-gemini-spring-boot-starter
を pom (または gradle) の依存関係に追加します。
src/main/resources
ディレクトリに application.properties
ファイルを追加して、VertexAi チャットモデルを有効にして構成します。
spring.ai.vertex.ai.gemini.project-id=PROJECT_ID
spring.ai.vertex.ai.gemini.location=LOCATION
spring.ai.vertex.ai.gemini.chat.options.model=vertex-pro-vision
spring.ai.vertex.ai.gemini.chat.options.temperature=0.5
project-id を Google クラウドプロジェクト ID に置き換え、location を Gemini の場所 に置き換えます。 |
これにより、クラスに挿入できる VertexAiGeminiChatModel
実装が作成されます。以下は、テキスト生成にチャットモデルを使用する単純な @Controller
クラスの例です。
@RestController
public class ChatController {
private final VertexAiGeminiChatModel chatModel;
@Autowired
public ChatController(VertexAiGeminiChatModel 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);
}
}
手動構成
VertexAiGeminiChatModel [GitHub] (英語) は ChatModel
を実装し、VertexAI
を使用して Vertex AI Gemini サービスに接続します。
spring-ai-vertex-ai-gemini
依存関係をプロジェクトの Maven pom.xml
ファイルに追加します。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-vertex-ai-gemini</artifactId>
</dependency>
または、Gradle build.gradle
ビルドファイルに保存します。
dependencies {
implementation 'org.springframework.ai:spring-ai-vertex-ai-gemini'
}
Spring AI BOM をビルドファイルに追加するには、"依存関係管理" セクションを参照してください。 |
次に、VertexAiGeminiChatModel
を作成し、テキスト生成に使用します。
VertexAI vertexApi = new VertexAI(projectId, location);
var chatModel = new VertexAiGeminiChatModel(this.vertexApi,
VertexAiGeminiChatOptions.builder()
.model(ChatModel.GEMINI_PRO_1_5_PRO)
.temperature(0.4)
.build());
ChatResponse response = this.chatModel.call(
new Prompt("Generate the names of 5 famous pirates."));
VertexAiGeminiChatOptions
は、チャットリクエストの構成情報を提供します。VertexAiGeminiChatOptions.Builder
は流れるようなオプションビルダーです。