Google Vertex AI Gemini
Vertex AIはGoogle Cloudの完全管理型AI開発プラ ットフォームであり、旧世代(PaLM2)と新世代(Gemini)を含むGoogleの大規模生成モデルへのアクセスを提供します。
Vertex AIを利用するには、まずGoogle Cloud Platformアカウントを作成する必要があります。
はじめに
Google Cloudアカウントの作成
Google Cloudを初めて利用する場合は、以下のページの「Get set up on Google Cloud」ドロップダウンメニューにある「[create an account]」ボタンをクリックして新しいアカウントを作成できます:
Google Cloud Platformアカウント内でプロジェクトを作成する
Google Cloudアカウント内で新しいプロジェクトを作成し、以下の手順に従ってVertex AI APIを有効にします:
将来のAPI呼び出しに必要となるため、PROJECT_ID
をメモしておいてください。
Google Cloud認証戦略の選択
Google CloudサービスとAPIに対してアプリケーションが認証を行う方法はいくつかあります。例えば、サービスアカウントを作成し、環境変数GOOGLE_APPLICATION_CREDENTIALS
を認証情報を含むJSONファイルのパスに設定することができます。
すべての認証戦略はこちらで確認できます。ただし、ローカルテストを簡単にするために、ここではgcloud
ユーティリティを使用した認証を使用します。
Google Cloud CLIのインストール(オプション)
クラウドプロジェクトにローカルからアクセスするには、インストール手順に従ってgcloud
ツールをインストールできます。GNU/Linuxオペレーティングシステムの場合、インストール手順は次のとおりです:
- SDKをダウンロードする:
curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-467.0.0-linux-x86_64.tar.gz
- アーカイブを展開する:
tar -xf google-cloud-cli-467.0.0-linux-x86_64.tar.gz
- インストールスクリプトを実行する:
cd google-cloud-sdk/
./install.sh
- 次のコマンドを実行して、デフォルトのプロジェクトと認証情報を設定します:
gcloud auth application-default login
この認証方法は、vertex-ai
(埋め込みモデル、PaLM2)とvertex-ai-gemini
(Gemini)の両方のパッケージと互換性があります。
依存関係の追加
始めるには、プロジェクトのpom.xml
に次の依存関係を追加します:
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-vertex-ai-gemini</artifactId>
<version>1.0.0-beta4</version>
</dependency>
またはプロジェクトのbuild.gradle
に:
implementation 'dev.langchain4j:langchain4j-vertex-ai-gemini:1.0.0-beta4'
サンプルコードを試す:
PROJECT_ID
フィールドは、新しいGoogle Cloudプロジェクトを作成するときに設定した変数を表します。
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.model.vertexai.VertexAiGeminiChatModel;
public class GeminiProVisionWithImageInput {
private static final String PROJECT_ID = "YOUR-PROJECT-ID";
private static final String LOCATION = "us-central1";
private static final String MODEL_NAME = "gemini-1.5-flash";
private static final String CAT_IMAGE_URL = "https://upload.wikimedia.org/" +
"wikipedia/commons/e/e9/" +
"Felis_silvestris_silvestris_small_gradual_decrease_of_quality.png";
public static void main(String[] args) {
ChatModel visionModel = VertexAiGeminiChatModel.builder()
.project(PROJECT_ID)
.location(LOCATION)
.modelName(MODEL_NAME)
.build();
ChatResponse response = visionModel.chat(
UserMessage.from(
ImageContent.from(CAT_IMAGE_URL),
TextContent.from("What do you see?")
)
);
System.out.println(response.aiMessage().text());
}
}
VertexAiGeminiStreamingChatModel
クラスのおかげでストリーミングもサポートされています:
var model = VertexAiGeminiStreamingChatModel.builder()
.project(PROJECT_ID)
.location(LOCATION)
.modelName(GEMINI_1_5_PRO)
.build();
model.chat("Why is the sky blue?", new StreamingChatResponseHandler() {
@Override
public void onPartialResponse(String partialResponse) {
System.print(partialResponse);
}
@Override
public void onCompleteResponse(ChatResponse completeResponse){
System.print(completeResponse);
}
@Override
public void onError(Throwable error) {
error.printStackTrace();
}
});
LambdaStreamingResponseHandler
からonPartialResponse()
とonPartialResponseAndError()
ユーティリティ関数のショートカットを使用できます:
model.chat("Why is the sky blue?", onPartialResponse(System.out::print));
model.chat("Why is the sky blue?", onPartialResponseAndError(System.out::print, Throwable::printStackTrace));
利用可能なモデル
モデル名 | 説明 | 入力 | プロパティ |
---|---|---|---|
gemini-1.5-flash | 高ボリューム、高品質、コスト効率の良いアプリケーション向けの速度と効率性を提供します。 | テキスト、コード、画像、音声、動画、音声付き動画、PDF | 最大入力トークン: 1,048,576, 最大出力トークン: 8,192 |
gemini-1.5-pro | テキストまたはコードの応答のためのテキストまたはチャットプロンプトをサポートします。最大入力トークン制限までの長文脈理解をサポートします。 | テキスト、コード、画像、音声、動画、音声付き動画、PDF | 最大入力トークン: 2,097,152, 最大出力トークン: 8,192 |
gemini-1.0-pro | テキストのみのタスクに最適なパフォーマンスを発揮するモデルです。 | テキスト | 最大入力トークン: 32,760, 最大出力トークン: 8,192 |
gemini-1.0-pro-vision | 幅広いアプリケーションに対応する最高のパフォーマンスを持つ画像および動画理解モデルです。 | テキスト、画像、音声、動画、音声付き動画、PDF | 最大入力トークン: 16,384, 最大出力トークン: 2,048 |
gemini-1.0-ultra | 指示、コード、推論を含む複雑なタスク向けに最適化された最も高性能なテキストモデルです。 | テキスト | 最大入力トークン: 8,192, 最大出力トークン: 2,048 |
gemini-1.0-ultra-vision | 最も高性能なマルチモーダルビジョンモデルです。テキスト、画像、動画の入力を共同でサポートするように最適化されています。 | テキスト、コード、画像、音声、動画、音声付き動画、PDF | 最大入力トークン: 8,192, 最大出力トークン: 2,048 |
モデルについての詳細はGeminiモデルのドキュメントページで確認できます。
2024年3月の時点では、Ultraバージョンは許可リストによるプライベートアクセスとなっています。そのため、次のような例外が発生する可能性があります:
Caused by: io.grpc.StatusRuntimeException:
FAILED_PRECONDITION: Project `1234567890` is not allowed to use Publisher Model
`projects/{YOUR_PROJECT_ID}/locations/us-central1/publishers/google/models/gemini-ultra`
設定
ChatModel model = VertexAiGeminiChatModel.builder()
.project(PROJECT_ID) // Google CloudプロジェクトのプロジェクトID
.location(LOCATION) // AI推論が行われる地域
.modelName(MODEL_NAME) // 使用するモデル
.logRequests(true) // 入力リクエストをログに記録
.logResponses(true) // 出力レスポンスをログに記録
.maxOutputTokens(8192) // 生成する最大トークン数(最大8192)
.temperature(0.7) // 温度(0から2の間)
.topP(0.95) // topP(0から1の間)- 最も確率の高いトークンの累積確率
.topK(3) // topK(正の整数)- 最も確率の高いトークンの中から選択
.seed(1234) // 乱数生成器のシード
.maxRetries(2) // 最大再試行回数
.responseMimeType("application/json") // JSON構造化出力を取得するため
.responseSchema(/*...*/) // 提供されたスキーマに従った構造化出力
.safetySettings(/*...*/) // 不適切なコンテンツをフィルタリングするための安全設定
.useGoogleSearch(true) // Google検索結果で回答を根拠付ける
.vertexSearchDatastore(name)// カスタムVertex AI Search データストアからのデータ
// バックドキュメントで回答を根拠付ける
.toolCallingMode(/*...*/) // AUTO(自動)、ANY(関数リストから)、NONE
.allowedFunctionNames(/*...*/) // ANYツール呼び出しモードを使用する場合、
// 呼び出し可能な関数名を指定
.listeners(/*...*/) // モデルイベントを受信するリスナーのリスト
.build();
同じパラメータはストリーミングチャットモデルでも利用可能です。
その他の例
Geminiはマルチモーダル
モデルであり、テキストだけでなく、画像、音声、動画ファイル、およびPDFも入力として受け付けます。
画像の内容を説明する
ChatModel model = VertexAiGeminiChatModel.builder()
.project(PROJECT_ID)
.location(LOCATION)
.modelName(GEMINI_1_5_PRO)
.build();
UserMessage userMessage = UserMessage.from(
ImageContent.from(CAT_IMAGE_URL),
TextContent.from("What do you see? Reply in one word.")
);
ChatResponse response = model.chat(userMessage);
URLはウェブURLでも、gs://my-bucket/my-image.png
のようなGoogle Cloud Storageバケットに保存されたファイルを指すこともできます。
また、Base64エンコードされた文字列として画像の内容を渡すこともできます:
String base64Data = Base64.getEncoder().encodeToString(readBytes(CAT_IMAGE_URL));
UserMessage userMessage = UserMessage.from(
ImageContent.from(base64Data, "image/png"),
TextContent.from("What do you see? Reply in one word.")
);
PDFドキュメントについて質問する
var model = VertexAiGeminiChatModel.builder()
.project(PROJECT_ID)
.location(LOCATION)
.modelName(GEMINI_1_5_PRO)
.logRequests(true)
.logResponses(true)
.build();
UserMessage message = UserMessage.from(
PdfFileContent.from(Paths.get("src/test/resources/gemini-doc-snapshot.pdf").toUri()),
TextContent.from("Provide a summary of the document")
);
ChatResponse response = model.chat(message);
ツール呼び出し
ChatModel model = VertexAiGeminiChatModel.builder()
.project(PROJECT_ID)
.location(LOCATION)
.modelName(GEMINI_1_5_PRO)
.build();
ToolSpecification weatherToolSpec = ToolSpecification.builder()
.name("getWeatherForecast")
.description("Get the weather forecast for a location")
.parameters(JsonObjectSchema.builder()
.addStringProperty("location", "the location to get the weather forecast for")
.required("location")
.build())
.build();
ChatRequest request = ChatRequest.builder()
.messages(UserMessage.from("What is the weather in Paris?"))
.toolSpecifications(weatherToolSpec)
.build();
ChatResponse response = model.chat(request);
モデルはテキストメッセージではなく、ツール実行リクエストで応答します。
あなたの責任は、ToolExecutionResultMessage
をモデルに送り返すことで、その実行リクエストの結果をモデルに提供することです。
その後、モデルはテキスト応答で返信できるようになります。
並列関数呼び出しもサポートされており、モデルが単一の応答で複数のツール実行リクエストを行うよう要求することができます。
AiServicesに よるツールサポート
AiServices
を使用して、ツールを活用した独自のアシスタントを作成できます。
次の例では、数学計算を行うCalculator
ツール、アシスタントの契約を指定するAssistant
インターフェース、
そしてGemini、チャットメモリ、およびCalculatorツールを使用するようにAiServices
を構成しています。
static class Calculator {
@Tool("Adds two given numbers")
double add(double a, double b) {
return a + b;
}
@Tool("Multiplies two given numbers")
String multiply(double a, double b) {
return String.valueOf(a * b);
}
}
interface Assistant {
String chat(String userMessage);
}
Calculator calculator = new Calculator();
Assistant assistant = AiServices.builder(Assistant.class)
.chatModel(model)
.chatMemory(MessageWindowChatMemory.withMaxMessages(10))
.tools(calculator)
.build();
String answer = assistant.chat("How much is 74589613588 + 4786521789?");