URL コンテキスト ツールを使用すると、プロンプトの追加コンテキストとして URL を Gemini に提供できます。モデルは、URL からコンテンツを取得し、そのコンテンツを使用してレスポンスを形成します。
このツールは、次のようなタスクに役立ちます。
- 記事からの重要なデータポイントや話のポイントの抽出
- マルチリンク間での情報の比較
- 複数のソースからのデータの統合
- 特定のページの内容に基づいて質問に回答する
- 特定の目的(求人情報の作成やテスト問題の作成など)のためのコンテンツの分析
このガイドでは、Vertex AI の Gemini API で URL コンテキスト ツールを使用する方法について説明します。
サポートされているモデル
次のモデルは URL コンテキストをサポートしています。
- Gemini 2.5 Flash (プレビュー)
- Gemini 2.5 Flash-Lite (プレビュー)
- Gemini 2.5 Flash-Lite
- Gemini 2.5 Pro
- Gemini 2.5 Flash
- Gemini 2.0 Flash
URL コンテキストを使用する
URL コンテキスト ツールは、単独で使用する方法と、Google 検索でのグラウンディングと組み合わせて使用する方法の 2 つの主な方法で使用できます。
URL コンテキストのみ
モデルで分析する特定の URL をプロンプトで直接指定できます。
Summarize this document: YOUR_URLs
Extract the key features from the product description on this page: YOUR_URLs
Python
from google import genai
from google.genai.types import Tool, GenerateContentConfig, HttpOptions, UrlContext
client = genai.Client(http_options=HttpOptions(api_version="v1"))
model_id = "gemini-2.5-flash"
url_context_tool = Tool(
url_context = UrlContext
)
response = client.models.generate_content(
model=model_id,
contents="Compare recipes from YOUR_URL1 and YOUR_URL2",
config=GenerateContentConfig(
tools=[url_context_tool],
response_modalities=["TEXT"],
)
)
for each in response.candidates[0].content.parts:
print(each.text)
# get URLs retrieved for context
print(response.candidates[0].url_context_metadata)
JavaScript
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
# with appropriate values for your project.
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_VERTEXAI=True
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({
vertexai: true,
project: process.env.GOOGLE_CLOUD_PROJECT,
location: process.env.GOOGLE_CLOUD_LOCATION,
apiVersion: 'v1',
});
async function main() {
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: [
"Compare recipes from YOUR_URL1 and YOUR_URL2",
],
config: {
tools: [{urlContext: {}}],
},
});
console.log(response.text);
// To get URLs retrieved for context
console.log(response.candidates[0].urlContextMetadata)
}
await main();
REST
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://aiplatform.googleapis.com/v1beta1/projects/GOOGLE_CLOUD_PROJECT/locations/global/publishers/google/models/gemini-2.5-flash:generateContent \
-d '{
"contents": [
{
"role": "user",
"parts": [
{"text": "Compare recipes from YOUR_URL1 and YOUR_URL2"}
]
}
],
"tools": [
{
"url_context": {}
}
]
}' > result.json
cat result.json
URL コンテキストを使用した Google 検索によるグラウンディング
URL コンテキストと Google 検索でのグラウンディングの両方を有効にして、URL を含むプロンプトまたは URL を含まないプロンプトを使用することもできます。モデルは、まず関連情報を検索し、次に URL コンテキスト ツールを使用して検索結果のコンテンツを読み取り、より詳細な情報を取得します。
この機能は試験運用版であり、API バージョン v1beta1
で使用できます。
プロンプトの例
Give me a three day event schedule based on YOUR_URL. Also let me know what needs to taken care of considering weather and commute.
Recommend 3 books for beginners to read to learn more about the latest YOUR_SUBJECT.
Python
from google import genai
from google.genai.types import Tool, GenerateContentConfig, HttpOptions, UrlContext, GoogleSearch
client = genai.Client(http_options=HttpOptions(api_version="v1beta1"))
model_id = "gemini-2.5-flash"
tools = []
tools.append(Tool(url_context=UrlContext))
tools.append(Tool(google_search=GoogleSearch))
response = client.models.generate_content(
model=model_id,
contents="Give me three day events schedule based on YOUR_URL. Also let me know what needs to taken care of considering weather and commute.",
config=GenerateContentConfig(
tools=tools,
response_modalities=["TEXT"],
)
)
for each in response.candidates[0].content.parts:
print(each.text)
# get URLs retrieved for context
print(response.candidates[0].url_context_metadata)
JavaScript
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values
# with appropriate values for your project.
export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_LOCATION=global
export GOOGLE_GENAI_USE_VERTEXAI=True
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({
vertexai: true,
project: process.env.GOOGLE_CLOUD_PROJECT,
location: process.env.GOOGLE_CLOUD_LOCATION,
apiVersion: 'v1beta1',
});
async function main() {
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: [
"Give me a three day event schedule based on YOUR_URL. Also let me know what needs to taken care of considering weather and commute.",
],
config: {
tools: [{urlContext: {}}, {googleSearch: {}}],
},
});
console.log(response.text);
// To get URLs retrieved for context
console.log(response.candidates[0].urlContextMetadata)
}
await main();
REST
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://aiplatform.googleapis.com/v1beta1/projects/GOOGLE_CLOUD_PROJECT/locations/global/publishers/google/models/gemini-2.5-flash:generateContent \
-d '{
"contents": [
{
"role": "user",
"parts": [
{"text": "Give me a three day event schedule based on YOUR_URL. Also let me know what needs to taken care of considering weather and commute."}
]
}
],
"tools": [
{
"url_context": {}
},
{
"google_search": {}
}
]
}' > result.json
cat result.json
Google 検索によるグラウンディングの詳細については、概要ページをご覧ください。
URL コンテキストを使用したエンタープライズ向けウェブ グラウンディング
特定のコンプライアンス要件がある場合や、医療、金融、公共部門などの規制対象業界に属している場合は、Enterprise で URL コンテキストとウェブ グラウンディングの両方を有効にできます。Web Grounding for Enterprise で使用されるウェブ インデックスは、Google 検索で利用可能なサブセットを使用するため、Google 検索による標準のグラウンディング インデックスよりも制限が厳しくなっています。
Web Grounding for Enterprise の詳細については、Web Grounding for Enterprise ページをご覧ください。
コンテキストに応じた回答
モデルのレスポンスは、URL から取得したコンテンツに基づきます。モデルが URL からコンテンツを取得した場合、レスポンスには url_context_metadata
が含まれます。このようなレスポンスは次のようになります(簡潔にするため、レスポンスの一部は省略しています)。
{
"candidates": [
{
"content": {
"parts": [
{
"text": "... \n"
}
],
"role": "model"
},
...
"url_context_metadata":
{
"url_metadata":
[
{
"retrieved_url": "https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/code-execution",
"url_retrieval_status": <UrlRetrievalStatus.URL_RETRIEVAL_STATUS_SUCCESS: "URL_RETRIEVAL_STATUS_SUCCESS">
},
{
"retrieved_url": "https://cloud.google.com/vertex-ai/generative-ai/docs/grounding/grounding-with-google-search",
"url_retrieval_status": <UrlRetrievalStatus.URL_RETRIEVAL_STATUS_SUCCESS: "URL_RETRIEVAL_STATUS_SUCCESS">
},
]
}
}
]
}
制限事項
- このツールでは、リクエストごとに最大 20 個の URL を分析のために消費します。
- このツールはウェブページのライブ バージョンを取得しないため、鮮度に問題があるか、情報が古くなっている可能性があります。
- 試験運用期間中は、YouTube 動画などのマルチメディア コンテンツではなく、標準的なウェブページでこのツールを使用することをおすすめします。