Google 検索によるグラウンディングは、Gemini モデルをリアルタイムで一般公開されているウェブ コンテンツに接続します。これにより、モデルは知識のカットオフを超えて、より正確で最新の回答を提供し、検証可能なソースを引用できるようになります。
Google 検索によるグラウンディングには、次のようなメリットがあります。
- 事実の正確性を高める: 実世界の情報を基に回答することで、モデルのハルシネーションを減らします。
- リアルタイムの情報にアクセスする: 最近の出来事やトピックに関する質問に答えます。
- 引用を提供する: モデルの主張の出典を示すことで、ユーザーの信頼を高めたり、関連サイトを閲覧できるようにしたりします。
- より複雑なタスクを完了する: 推論タスクを支援するために、アーティファクトと関連する画像、動画、その他のメディアを取得します。
- 地域や言語に固有の回答を改善する: 地域固有の情報を検索したり、コンテンツの正確な翻訳をサポートしたりします。
Google 検索の Grounding は、iOS+、Android、ウェブでご利用いただけます。Flutter と Unity では、今後のリリースで利用できるようになります。
サポートされているモデル
gemini-2.5-pro
gemini-2.5-flash
gemini-2.5-flash-lite-preview-06-17
gemini-2.0-flash-001
(および自動更新エイリアスgemini-2.0-flash
)gemini-2.0-flash-live-preview-04-09
サポートされている言語
Gemini モデルについては、サポートされている言語をご覧ください。
Google 検索でモデルのグラウンディングを行う
Gemini API プロバイダをクリックして、このページでプロバイダ固有のコンテンツとコードを表示します。 |
GenerativeModel
インスタンスを作成するときに、モデルがレスポンスの生成に使用できる tool
として GoogleSearch
を指定します。
Swift
import FirebaseAI
// Initialize the Gemini Developer API backend service
let ai = FirebaseAI.firebaseAI(backend: .googleAI())
// Create a `GenerativeModel` instance with a model that supports your use case
let model = ai.generativeModel(
modelName: "GEMINI_MODEL_NAME",
// Provide Google Search as a tool that the model can use to generate its response
tools: [Tool.googleSearch()]
)
let response = try await model.generateContent("Who won the euro 2024?")
print(response.text ?? "No text in response.")
// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result
Kotlin
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
modelName = "GEMINI_MODEL_NAME",
// Provide Google Search as a tool that the model can use to generate its response
tools = listOf(Tool.GoogleSearch())
)
val response = model.generateContent("Who won the euro 2024?")
print(response.text)
// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result
Java
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance with a model that supports your use case
GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel("GEMINI_MODEL_NAME",
null,
null,
// Provide Google Search as a tool that the model can use to generate its response
List.of(Tool.GoogleSearch()));
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
ListenableFuture response = model.generateContent("Who won the euro 2024?");
Futures.addCallback(response, new FutureCallback() {
@Override
public void onSuccess(GenerateContentResponse result) {
String resultText = result.getText();
System.out.println(resultText);
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result
Web
import { initializeApp } from "firebase/app";
import { getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";
// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
// ...
};
// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);
// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Create a `GenerativeModel` instance with a model that supports your use case
const model = getGenerativeModel(
ai,
{
model: "GEMINI_MODEL_NAME",
// Provide Google Search as a tool that the model can use to generate its response
tools: [{ googleSearch: {} }]
}
);
const result = await model.generateContent("Who won the euro 2024?");
console.log(result.response.text());
// Make sure to comply with the "Grounding with Google Search" usage requirements,
// which includes how you use and display the grounded result
Dart
Flutter のサポートは、次のリリースで提供される予定です。
Unity
Unity のサポートは、次のリリースで提供される予定です。
ユースケースとアプリに適したモデル を選択する方法について説明します。
最適な結果を得るには、1.0
の温度を使用します(これはすべての 2.5 モデルのデフォルトです)。モデルの構成で温度を設定する方法を確認する。
Google 検索によるグラウンディングの仕組み
GoogleSearch
ツールを使用すると、モデルは情報の検索、処理、引用のワークフロー全体を自動的に処理します。
モデルのワークフローは次のとおりです。
- プロンプトを受信する: アプリが
GoogleSearch
ツールを有効にして Gemini モデルにプロンプトを送信します。 - プロンプトを分析する: モデルがプロンプトを分析し、Google 検索で回答を改善できるかどうかを判断します。
- Google 検索にクエリを送信する: 必要に応じて、モデルは 1 つ以上の検索クエリを自動的に生成して実行します。
- 検索結果を処理する: モデルは Google 検索の結果を処理し、元のプロンプトに対する回答を生成します。
- 「グラウンディングされた結果」を返す: モデルは、Google 検索の検索結果に基づいてグラウンディングされた、最終的なユーザー フレンドリーなレスポンスを返します。このレスポンスには、モデルのテキスト回答と、検索クエリ、ウェブ検索結果、引用を含む
groundingMetadata
が含まれます。
Google 検索をモデルのツールとして提供しても、モデルが常に Google 検索ツールを使用してレスポンスを生成する必要はありません。このような場合、レスポンスに groundingMetadata
オブジェクトは含まれないため、「グラウンディングされた結果」にはなりません。
グラウンディングされた結果を理解する
モデルが Google 検索の検索結果に基づいてレスポンスをグラウンディングする場合、レスポンスには、クレームの検証とアプリケーションでのリッチな引用エクスペリエンスの構築に不可欠な構造化データを含む groundingMetadata
オブジェクトが含まれます。
「グラウンディングされた結果」の groundingMetadata
オブジェクトには、次の情報が含まれます。
webSearchQueries
: Google 検索に送信された検索クエリの配列。この情報は、モデルの推論プロセスのデバッグと理解に役立ちます。searchEntryPoint
: 必要な「Google 検索候補」をレンダリングするための HTML と CSS が含まれています。選択した API プロバイダの「Google 検索によるグラウンディング」の使用要件(Gemini Developer API または Vertex AI Gemini API)を遵守する必要があります(サービス固有の規約のサービス規約セクションを参照)。このページの後半で、グラウンディングされた結果を使用および表示する方法について説明します。groundingChunks
: ウェブソース(uri
とtitle
)を含むオブジェクトの配列。groundingSupports
: モデル レスポンスtext
をgroundingChunks
のソースに接続するチャンクの配列。各チャンクは、テキストsegment
(startIndex
とendIndex
で定義)を 1 つ以上のgroundingChunkIndices
にリンクします。このフィールドは、インライン引用を作成するのに役立ちます。このページの後半のグラウンディングされた結果を使用および表示するをご覧ください。
groundingMetadata
オブジェクトを含むレスポンスの例を次に示します。
{
"candidates": [
{
"content": {
"parts": [
{
"text": "Spain won Euro 2024, defeating England 2-1 in the final. This victory marks Spain's record fourth European Championship title."
}
],
"role": "model"
},
"groundingMetadata": {
"webSearchQueries": [
"UEFA Euro 2024 winner",
"who won euro 2024"
],
"searchEntryPoint": {
"renderedContent": "<!-- HTML and CSS for the search widget -->"
},
"groundingChunks": [
{"web": {"uri": "https://vertexaisearch.cloud.google.com.....", "title": "aljazeera.com"}},
{"web": {"uri": "https://vertexaisearch.cloud.google.com.....", "title": "uefa.com"}}
],
"groundingSupports": [
{
"segment": {"startIndex": 0, "endIndex": 85, "text": "Spain won Euro 2024, defeatin..."},
"groundingChunkIndices": [0]
},
{
"segment": {"startIndex": 86, "endIndex": 210, "text": "This victory marks Spain's..."},
"groundingChunkIndices": [0, 1]
}
]
}
}
]
}
グラウンディングされた結果を使用および表示する
モデルが Google 検索ツールを使用してレスポンスを生成する場合、レスポンスに groundingMetadata
オブジェクトが提供されます。
Google 検索の検索候補を表示する場合は必須、引用を表示する場合は推奨されます。
この情報を表示することで、Google 検索ツールの使用要件を満たすだけでなく、ユーザーとエンドユーザーが回答を検証し、さらに学習するための手段を追加できます。
(必須) Google 検索の候補を表示する
レスポンスに「Google 検索候補」が含まれている場合は、「Google 検索によるグラウンディング」の使用要件(Google 検索候補の表示方法など)を遵守する必要があります。
groundingMetadata
オブジェクトには「Google 検索候補」が含まれています。具体的には、searchEntryPoint
フィールドが含まれています。このフィールドには、準拠する HTML と CSS のスタイルを提供する renderedContent
フィールドがあります。このフィールドを実装すると、アプリで検索候補を表示できます。
Google Cloud ドキュメントで、Google 検索の候補の表示と動作に関する要件の詳細を確認してください。この詳細なガイダンスは Vertex AI Gemini API ドキュメントに記載されていますが、Gemini Developer API プロバイダにも適用されます。
このセクションの後半にあるコードサンプルの例を参照してください。
(推奨)引用を表示する
groundingMetadata
オブジェクトには、構造化された引用データ(具体的には groundingSupports
フィールドと groundingChunks
フィールド)が含まれます。この情報を使用して、モデルのステートメントを UI 内のソースに直接リンクします(インラインおよび集計)。
このセクションの後半にあるコードサンプルの例を参照してください。
コードサンプルの例
これらのコードサンプルは、グラウンディングされた結果の使用と表示に関する一般化されたパターンを提供します。ただし、お客様の責任において、お客様固有の実装がコンプライアンス要件に準拠していることを確認してください。
Swift
// ...
// Get the model's response
let text = response.text
// Get the grounding metadata
if let candidate = response.candidates.first,
let groundingMetadata = candidate.groundingMetadata {
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
if let renderedContent = groundingMetadata.searchEntryPoint?.renderedContent {
// TODO(developer): Display Google Search suggestions using a WebView
}
// RECOMMENDED - display citations
let groundingChunks = groundingMetadata.groundingChunks
for chunk in groundingMetadata.groundingChunks {
if let web = chunk.web {
let title = web.title // for example, "uefa.com"
let uri = web.uri // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show citation in the UI
}
}
}
Kotlin
// ...
// Get the model's response
val text = response.text
// Get the grounding metadata
val groundingMetadata = response.candidates.firstOrNull()?.groundingMetadata
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
val renderedContent = groundingMetadata?.searchEntryPoint?.renderedContent
if (renderedContent != null) {
// TODO(developer): Display Google Search suggestions using a WebView
}
// RECOMMENDED - display citations
val groundingChunks = groundingMetadata?.groundingChunks
groundingChunks?.let { chunks ->
for (chunk in chunks) {
val title = chunk.web?.title // for example, "uefa.com"
val uri = chunk.web?.uri // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show citation in the UI
}
}
Java
// ...
Futures.addCallback(response, new FutureCallback() {
@Override
public void onSuccess(GenerateContentResponse result) {
// Get the model's response
String text = result.getText();
// Get the grounding metadata
GroundingMetadata groundingMetadata =
result.getCandidates()[0].getGroundingMetadata();
if (groundingMetadata != null) {
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
String renderedContent =
groundingMetadata.getSearchEntryPoint().getRenderedContent();
if (renderedContent != null) {
// TODO(developer): Display Google Search suggestions using a WebView
}
// RECOMMENDED - display citations
List chunks = groundingMetadata.getGroundingChunks();
if (chunks != null) {
for(GroundingChunk chunk : chunks) {
WebGroundingChunk web = chunk.getWeb();
if (web != null) {
String title = web.getTitle(); // for example, "uefa.com"
String uri = web.getUri(); // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show citation in the UI
}
}
}
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
Web
// ...
// Get the model's text response
const text = result.response.text();
// Get the grounding metadata
const groundingMetadata = result.response.candidates?.[0]?.groundingMetadata;
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
const renderedContent = groundingMetadata?.searchEntryPoint?.renderedContent;
if (renderedContent) {
// TODO(developer): render this HTML and CSS in the UI
}
// RECOMMENDED - display citations
const groundingChunks = groundingMetadata?.groundingChunks;
if (groundingChunks) {
for (const chunk of groundingChunks) {
const title = chunk.web?.title; // for example, "uefa.com"
const uri = chunk.web?.uri; // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show citation in the UI
}
}
Dart
Flutter のサポートは、次のリリースで提供される予定です。
Unity
Unity のサポートは、次のリリースで提供される予定です。
Firebase コンソールでのグラウンディングされた結果と AI モニタリング
Firebase コンソールで AI モニタリングを有効にしている場合、レスポンスは Cloud Logging に保存されます。デフォルトでは、このデータの保持期間は 30 日間です。
この保持期間または設定したカスタム期間が、特定のユースケースと選択した Gemini API プロバイダ(Gemini Developer API または Vertex AI Gemini API)の追加のコンプライアンス要件に完全に準拠していることを確認するのは、お客様の責任です(サービス固有の規約のサービス規約のセクションを参照)。これらの要件を満たすには、Cloud Logging で保持期間を調整する必要があります。
料金と上限
選択した Gemini API プロバイダのドキュメント(Gemini Developer API | Vertex AI Gemini API)で、Google 検索によるグラウンディングの料金、モデルの可用性、上限を確認してください。