网址上下文

借助网址上下文工具,您可以网址的形式向模型提供更多上下文。通过在请求中添加网址,模型将访问这些网页中的内容(只要不是限制部分中列出的网址类型),从而为回答提供信息并提高回答质量。

网址上下文工具适用于以下任务:

  • 提取数据:从多个网址中提取价格、名称或关键发现等特定信息。
  • 比较文档:分析多份报告、文章或 PDF,以找出差异并跟踪趋势。
  • 综合和创建内容:整合来自多个来源网址的信息,生成准确的摘要、博文或报告。
  • 分析代码和文档:指向 GitHub 代码库或技术文档,以解释代码、生成设置说明或回答问题。

以下示例展示了如何比较来自不同网站的两份食谱。

Python

from google import genai

client = genai.Client()

url1 = "https://www.foodnetwork.com/recipes/ina-garten/perfect-roast-chicken-recipe-1940592"
url2 = "https://www.allrecipes.com/recipe/21151/simple-whole-roast-chicken/"

interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input=f"Compare the ingredients and cooking times from the recipes at {url1} and {url2}",
    tools=[{"type": "url_context"}]
)

# Print the model's text response and its source annotations
for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)
                if content_block.annotations:
                    print("\nSources:")
                    for annotation in content_block.annotations:
                        if annotation.type == "url_citation":
                            print(f"  - {annotation.title}: {annotation.url}")

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

async function main() {
  const interaction = await client.interactions.create({
    model: "gemini-3-flash-preview",
    input: "Compare the ingredients and cooking times from the recipes at https://www.foodnetwork.com/recipes/ina-garten/perfect-roast-chicken-recipe-1940592 and https://www.allrecipes.com/recipe/21151/simple-whole-roast-chicken/",
    tools: [{ type: "url_context" }]
  });

  // Print the model's text response and its source annotations
  for (const step of interaction.steps) {
    if (step.type === 'model_output') {
      for (const contentBlock of step.content) {
        if (contentBlock.type === 'text') {
          console.log(contentBlock.text);
          if (contentBlock.annotations) {
            console.log("\nSources:");
            for (const annotation of contentBlock.annotations) {
              if (annotation.type === 'url_citation') {
                console.log(`  - ${annotation.title}: ${annotation.url}`);
              }
            }
          }
        }
      }
    }
  }
}

await main();

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
      "model": "gemini-3-flash-preview",
      "input": "Compare the ingredients and cooking times from the recipes at https://www.foodnetwork.com/recipes/ina-garten/perfect-roast-chicken-recipe-1940592 and https://www.allrecipes.com/recipe/21151/simple-whole-roast-chicken/",
      "tools": [{"type": "url_context"}]
  }'

运作方式

网址上下文工具使用两步检索流程来平衡速度、费用和对最新数据的访问。当您提供网址时,该工具会先尝试从内部索引缓存中提取内容。它充当高度优化的缓存。如果某个网址未编入索引(例如,如果该网址指向的网页是新近发布的),该工具会自动回退到执行实时提取。此工具会直接访问网址,以实时检索其内容。

您可以将网址上下文工具与其他工具结合使用,以创建更强大的工作流。

Gemini 3 模型支持将内置工具(例如网址上下文)与自定义工具(函数调用)相结合。如需了解详情,请参阅工具组合页面。

同时启用网址上下文和依托 Google 搜索进行接地后,模型可以使用其搜索功能在网上查找相关信息,然后使用网址上下文工具更深入地了解找到的网页。对于需要广泛搜索和深入分析特定网页的提示,这种方法非常有效。

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input="Give me three day events schedule based on YOUR_URL. Also let me know what needs to taken care of considering weather and commute.",
    tools=[
        {"type": "url_context"},
        {"type": "google_search"}
    ]
)

for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)

JavaScript

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({});

async function main() {
  const interaction = await client.interactions.create({
    model: "gemini-3-flash-preview",
    input: "Give me three day events schedule based on YOUR_URL. Also let me know what needs to taken care of considering weather and commute.",
    tools: [
      { type: "url_context" },
      { type: "google_search" }
    ]
  });

  for (const step of interaction.steps) {
    if (step.type === 'model_output') {
      for (const contentBlock of step.content) {
        if (contentBlock.type === 'text') console.log(contentBlock.text);
      }
    }
  }
}

await main();

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
      "model": "gemini-3-flash-preview",
      "input": "Give me three day events schedule based on YOUR_URL. Also let me know what needs to taken care of considering weather and commute.",
      "tools": [
          {"type": "url_context"},
          {"type": "google_search"}
      ]
  }'

了解回答

当模型使用网址上下文工具时,其文本回答会在文本内容块中添加内嵌的 url_citation 注释。每条注释都会将回答文本的某个部分(通过 start_indexend_index)关联到其来源网址。这是在应用中显示引用信息的主要方式 - 请参阅上文中的主要示例,了解如何提取引用信息。

响应中还包含一个 url_context_result 步骤,其中包含有关每次网址检索尝试(状态、检索到的网址)的元数据。这主要用于调试。

安全检查

系统会对网址进行内容审核检查,以确认其符合安全标准。如果某个网址未通过此检查,相应的 url_context_result 步骤将显示 "unsafe"status

Token 计数

从您在提示中指定的网址检索到的内容会作为输入 token 的一部分进行统计。您可以在互动的 usage 对象中查看令牌数量。下面给出了一个示例:

'usage': {
  'output_tokens': 45,
  'input_tokens': 27,
  'input_tokens_details': [{'modality': 'TEXT', 'token_count': 27}],
  'thoughts_tokens': 31,
  'tool_use_input_tokens': 10309,
  'tool_use_input_tokens_details': [{'modality': 'TEXT', 'token_count': 10309}],
  'total_tokens': 10412
}

每个令牌的价格取决于所用模型,详情请参阅价格页面。

支持的模型

模型 网址上下文
Gemini 3.1 Pro 预览版 ✔️
Gemini 3.1 Flash-Lite ✔️
Gemini 3.1 Flash-Lite 预览版 ✔️
Gemini 3 Flash 预览版 ✔️
Gemini 2.5 Pro ✔️
Gemini 2.5 Flash ✔️
Gemini 2.5 Flash-Lite ✔️

最佳做法

  • 提供具体网址:为获得最佳结果,请提供您希望模型分析的内容的直接网址。该模型只会从您提供的网址中检索内容,而不会从任何嵌套链接中检索内容。
  • 检查可访问性:验证您提供的网址是否不会指向需要登录或位于付费墙后面的网页。
  • 使用完整网址:提供完整网址,包括协议(例如,https://www.google.com 而不是仅提供 google.com)。

限制

  • 函数调用:目前不支持将工具使用(网址上下文、依托 Google 搜索进行接地等)与函数调用搭配使用。
  • 请求限制:该工具每次请求最多可处理 20 个网址。
  • 网址内容大小:从单个网址检索的内容大小上限为 34MB。
  • 公开可访问性:网址必须可在网络上公开访问。 不支持本地主机地址(例如,localhost、127.0.0.1)、专用网络和隧道服务(例如,ngrok、pinggy)。
  • 仅限 Gemini API:网址上下文仅在 Gemini API 中提供,无法通过 Gemini Enterprise Agent Platform 使用。

支持和不支持的内容类型

该工具可以从具有以下内容类型的网址中提取内容:

  • 文本(text/html、application/json、text/plain、text/xml、text/css、text/javascript、text/csv、text/rtf)
  • 图片(image/png、image/jpeg、image/bmp、image/webp)
  • PDF (application/pdf)

以下内容类型受支持:

  • 付费内容
  • YouTube 视频(请参阅视频理解,了解如何处理 YouTube 网址)
  • Google Workspace 文件,例如 Google 文档或电子表格
  • 视频和音频文件