はじめに
TAAFアニメファン賞、ミリアニにお願いします! nikkieです。
OpenAIのGPTではおなじみのJSON形式出力をGeminiでどうやるのか調べています。
前回はGoogle AI篇で、今回はVertex AI篇です1。
目次
ドキュメントより
google-cloud-aiplatform
JSONスキーマを表す辞書を手で書いて指定します。
同じGeminiのはずなのですが、google-generativeaiのようにTypedDictで2渡せないのが謎です(ライブラリにプルリクチャンスなのかな?)
ドキュメントには、Gemini 1.5 Flash・Gemini 1.5 Proのモデルの記載がありました。
cookbookより
google-genai
1つのライブラリで、Google AIのGemini APIもVertex AIのGemini APIも叩ける新星。
ドキュメントを参考にPydantic.BaseModelを継承したクラスを渡してみましょう。
https://googleapis.github.io/python-genai/#json-response-schema
動作確認スクリプト
Vertex AIが有効なGoogle Cloudのプロジェクトをgcloudコマンドのデフォルトprojectに設定しました。
その状態でgcloud auth application-default loginしたので、vertexai.init()にprojectやlocationの指定無しで済んでいます。
一方、google-genaiのClientは、ADCがあってもprojectやlocationの指定がいるようでした(省略したら 503 Service Unavailable になってハマりました)3
- uv 0.5.17 (Homebrew 2025-01-10)
- スクリプトにinline script metadataを書き、
uv runで実行しています
- スクリプトにinline script metadataを書き、
- Python 3.12.5
- google-cloud-aiplatform 1.77.0
- google-genai 0.5.0
google-cloud-aiplatformでプロンプト・Gemini 1.5
Vertex AIのドキュメントには記載がないですが、試しました。
Google AI同様にJSONのコードブロックで返ってきており、欲しい形(JSONそのもの)ではありません。
※実際はバッククォートのみです(バックスラッシュはエスケープの意図です)
\`\`\`json
[
{
"recipe_name": "Chocolate Chip Cookies",
"ingredients": [
"1 cup (2 sticks) unsalted butter, softened",
"¾ cup granulated sugar",
"¾ cup packed brown sugar",
"1 teaspoon pure vanilla extract",
"2 large eggs",
"2 ¼ cups all-purpose flour",
"1 teaspoon baking soda",
"1 teaspoon salt",
"2 cups chocolate chips"
]
},
# 省略
]
\`\`\`
google-cloud-aiplatformで手書きJSONスキーマ・Gemini 1.5
JSONで得られました!
[ { "recipe_name": "Chocolate Chip Cookies", "ingredients": [ "1 cup (2 sticks) unsalted butter, softened", "1 cup granulated sugar", "1 cup packed brown sugar", "2 teaspoons pure vanilla extract", "2 large eggs", "3 cups all-purpose flour", "1 teaspoon baking soda", "1 teaspoon salt", "2 cups chocolate chips" ] }, // 省略 { "recipe_name": "Snickerdoodles", "ingredients": [ "1 cup (2 sticks) unsalted butter, softened", "1 1/2 cups granulated sugar", "2 large eggs", "2 3/4 cups all-purpose flour", "2 teaspoons cream of tartar", "1 teaspoon baking soda", "1/4 teaspoon salt", "2 tablespoons granulated sugar", "2 teaspoons ground cinnamon" ] } ]
google-genaiでPydanticのBaseModel・Gemini 2.0
JSONで得られました!
google-genaiでlistが未サポートのようだったので、レシピの例ではなくgoogle-genaiのドキュメントの例です。
{ "capital": "Washington D.C.", "continent": "North America", "gdp": 25000000000000, "name": "United States", "official_language": "English", "population": 331000000, "total_area_sq_mi": 3797000 }
google-genaiでGoogle AIとVertex AIのコードが統合されたのはとてもありがたいです
終わりに
Vertex AIのGemini APIでJSON出力を得るには以下の方法があります。
- google-cloud-aiplatformで手書きJSONスキーマを渡す(Gemini 1.5)
- google-genaiでPydanticのBaseModelを継承したモデルを渡す(Gemini 2.0)
- Google AIと共通!
google-generativeaiはTypedDictでしたが、なんとVertex AIとは互換性がありません!(みんなちがって、みんないい...??)
google-genaiの登場でGoogle AI・Vertex AIを同じコードで叩けるようになったのは福音ですが、まだまだ発展途上のライブラリなので機能拡充が待たれます🙏(プルリクチャンス!)
- Gemini APIはGoogle AIとVertex AIの双方から利用できます ↩
- 前回記事のこの辺り Google AIのGemini APIで出力をJSON形式で構造化するには(google-generativeai、google-genai) - nikkie-ftnextの日記↩
-
↩なるほど。google-cloud-aiplatformはADC見てくれるようなのでvertexai.init()のprojectやlocationの指定を省略できるが
— nikkie(にっきー) / にっP (@ftnext) 2025年1月20日
google-genaiはADCを見ないっぽくClient初期化時にprojectやlocationの指定必須っぽいですね。この指定省略できるだろうと進めた結果503に直面していた