URL context

เครื่องมือบริบท URL ช่วยให้คุณระบุบริบทเพิ่มเติมให้กับโมเดลในรูปแบบของ URL ได้ การใส่ URL ในคำขอจะช่วยให้โมเดลเข้าถึงเนื้อหาจากหน้าเหล่านั้นได้ (ตราบใดที่ URL ไม่ใช่ประเภทที่ระบุไว้ในส่วนข้อจำกัด) เพื่อให้ข้อมูลและปรับปรุงคำตอบ

เครื่องมือบริบท URL มีประโยชน์สำหรับงานต่างๆ เช่น

  • ดึงข้อมูล: ดึงข้อมูลที่เฉพาะเจาะจง เช่น ราคา ชื่อ หรือข้อค้นพบที่สำคัญ จากหลาย URL
  • เปรียบเทียบเอกสาร: วิเคราะห์รายงาน บทความ หรือ PDF หลายรายการเพื่อ ระบุความแตกต่างและติดตามแนวโน้ม
  • สังเคราะห์และสร้างเนื้อหา: รวมข้อมูลจาก URL แหล่งที่มาหลายรายการเพื่อสร้างข้อมูลสรุป บล็อกโพสต์ หรือรายงานที่ถูกต้อง
  • วิเคราะห์โค้ดและเอกสาร: ชี้ไปยังที่เก็บ GitHub หรือเอกสารทางเทคนิค เพื่ออธิบายโค้ด สร้างวิธีการตั้งค่า หรือตอบคำถาม

ตัวอย่างต่อไปนี้แสดงวิธีเปรียบเทียบสูตรอาหาร 2 สูตรจากเว็บไซต์ต่างๆ

Python

from google import genai
from google.genai.types import Tool, GenerateContentConfig

client = genai.Client()
model_id = "gemini-2.5-flash"

tools = [
  {"url_context": {}},
]

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

response = client.models.generate_content(
    model=model_id,
    contents=f"Compare the ingredients and cooking times from the recipes at {url1} and {url2}",
    config=GenerateContentConfig(
        tools=tools,
    )
)

for each in response.candidates[0].content.parts:
    print(each.text)

# For verification, you can inspect the metadata to see which URLs the model retrieved
print(response.candidates[0].url_context_metadata)

JavaScript

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

const ai = new GoogleGenAI({});

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.5-flash",
    contents: [
        "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/",
    ],
    config: {
      tools: [{urlContext: {}}],
    },
  });
  console.log(response.text);

  // For verification, you can inspect the metadata to see which URLs the model retrieved
  console.log(response.candidates[0].urlContextMetadata)
}

await main();

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
      "contents": [
          {
              "parts": [
                  {"text": "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": [
          {
              "url_context": {}
          }
      ]
  }' > result.json

cat result.json

วิธีการทำงาน

เครื่องมือบริบท URL ใช้กระบวนการดึงข้อมูล 2 ขั้นตอนเพื่อ รักษาสมดุลระหว่างความเร็ว ต้นทุน และการเข้าถึงข้อมูลล่าสุด เมื่อคุณระบุ URL เครื่องมือจะ พยายามดึงข้อมูลเนื้อหาจากแคชดัชนีภายในก่อน ซึ่งทำหน้าที่เป็น แคชที่มีการเพิ่มประสิทธิภาพสูง หาก URL ไม่พร้อมใช้งานในดัชนี (เช่น หากเป็นหน้าเว็บใหม่มาก) เครื่องมือจะกลับไปใช้การดึงข้อมูลเวอร์ชันที่ใช้จริงโดยอัตโนมัติ ซึ่งจะเข้าถึง URL โดยตรงเพื่อดึงเนื้อหาแบบเรียลไทม์

คุณสามารถใช้เครื่องมือบริบท URL ร่วมกับเครื่องมืออื่นๆ เพื่อสร้างเวิร์กโฟลว์ที่มีประสิทธิภาพมากขึ้นได้

เมื่อเปิดใช้ทั้งบริบท URL และการอ้างอิงจาก Google Search โมเดลจะใช้ความสามารถในการค้นหาเพื่อค้นหาข้อมูลที่เกี่ยวข้องทางออนไลน์ จากนั้นใช้เครื่องมือบริบท URL เพื่อทำความเข้าใจหน้าเว็บที่พบในเชิงลึกมากขึ้น แนวทางนี้มีประสิทธิภาพสำหรับ พรอมต์ที่ต้องใช้ทั้งการค้นหาแบบกว้างและการวิเคราะห์หน้าเว็บที่เฉพาะเจาะจงอย่างละเอียด

Python

from google import genai
from google.genai.types import Tool, GenerateContentConfig, GoogleSearch, UrlContext

client = genai.Client()
model_id = "gemini-2.5-flash"

tools = [
      {"url_context": {}},
      {"google_search": {}}
  ]

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,
    )
)

for each in response.candidates[0].content.parts:
    print(each.text)
# get URLs retrieved for context
print(response.candidates[0].url_context_metadata)

JavaScript

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

const ai = new GoogleGenAI({});

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.5-flash",
    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: {
      tools: [
        {urlContext: {}},
        {googleSearch: {}}
        ],
    },
  });
  console.log(response.text);
  // To get URLs retrieved for context
  console.log(response.candidates[0].urlContextMetadata)
}

await main();

REST

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
      "contents": [
          {
              "parts": [
                  {"text": "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": [
          {
              "url_context": {}
          },
          {
              "google_search": {}
          }
      ]
  }' > result.json

cat result.json

การทำความเข้าใจคำตอบ

เมื่อโมเดลใช้เครื่องมือบริบท URL การตอบกลับจะมีออบเจ็กต์ url_context_metadata ออบเจ็กต์นี้แสดง URL ที่โมเดลดึงข้อมูลเนื้อหาจาก และสถานะของความพยายามในการดึงข้อมูลแต่ละครั้ง ซึ่งมีประโยชน์สำหรับการยืนยันและการแก้ไขข้อบกพร่อง

ตัวอย่างส่วนนั้นของการตอบกลับมีดังนี้ (เราได้ละเว้นบางส่วนของการตอบกลับเพื่อความกระชับ)

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "... \n"
          }
        ],
        "role": "model"
      },
      ...
      "url_context_metadata": {
        "url_metadata": [
          {
            "retrieved_url": "https://www.foodnetwork.com/recipes/ina-garten/perfect-roast-chicken-recipe-1940592",
            "url_retrieval_status": "URL_RETRIEVAL_STATUS_SUCCESS"
          },
          {
            "retrieved_url": "https://www.allrecipes.com/recipe/21151/simple-whole-roast-chicken/",
            "url_retrieval_status": "URL_RETRIEVAL_STATUS_SUCCESS"
          }
        ]
      }
    }
}

ดูรายละเอียดทั้งหมดเกี่ยวกับออบเจ็กต์นี้ได้ในUrlContextMetadataเอกสารอ้างอิง API

การตรวจสอบความปลอดภัย

ระบบจะตรวจสอบการกลั่นกรองเนื้อหาใน URL เพื่อยืนยันว่า URL เป็นไปตามมาตรฐานความปลอดภัย หาก URL ที่คุณระบุไม่ผ่านการตรวจสอบนี้ คุณจะได้รับurl_retrieval_statusของURL_RETRIEVAL_STATUS_UNSAFE

จำนวนโทเค็น

ระบบจะนับเนื้อหาที่ดึงมาจาก URL ที่คุณระบุในพรอมต์เป็นส่วนหนึ่งของโทเค็นอินพุต คุณดูจำนวนโทเค็นสำหรับพรอมต์และการใช้เครื่องมือได้ในออบเจ็กต์ usage_metadata ของเอาต์พุตโมเดล ตัวอย่างเอาต์พุตมีดังนี้

'usage_metadata': {
  'candidates_token_count': 45,
  'prompt_token_count': 27,
  'prompt_tokens_details': [{'modality': <MediaModality.TEXT: 'TEXT'>,
    'token_count': 27}],
  'thoughts_token_count': 31,
  'tool_use_prompt_token_count': 10309,
  'tool_use_prompt_tokens_details': [{'modality': <MediaModality.TEXT: 'TEXT'>,
    'token_count': 10309}],
  'total_token_count': 10412
  }

ราคาต่อโทเค็นจะขึ้นอยู่กับโมเดลที่ใช้ โปรดดูรายละเอียดในหน้าการกำหนดราคา

โมเดลที่รองรับ

แนวทางปฏิบัติแนะนำ

  • ระบุ URL ที่เฉพาะเจาะจง: เพื่อให้ได้ผลลัพธ์ที่ดีที่สุด ให้ระบุ URL โดยตรงไปยังเนื้อหาที่ต้องการให้โมเดลวิเคราะห์ โมเดลจะดึงข้อมูลเนื้อหาจาก URL ที่คุณระบุเท่านั้น ไม่ใช่เนื้อหาจากลิงก์ที่ซ้อนกัน
  • ตรวจสอบการช่วยเหลือพิเศษ: ตรวจสอบว่า URL ที่คุณระบุไม่ได้นำไปยังหน้าเว็บที่ต้องมีการเข้าสู่ระบบหรืออยู่หลังเพย์วอลล์
  • ใช้ URL แบบเต็ม: ระบุ URL แบบเต็ม รวมถึงโปรโตคอล (เช่น https://www.google.com แทนที่จะเป็นแค่ google.com)

ข้อจำกัด

  • ราคา: เนื้อหาที่ดึงมาจาก URL จะนับเป็นโทเค็นอินพุต การจำกัดอัตรา และราคาจะขึ้นอยู่กับโมเดลที่ใช้ ดูรายละเอียดได้ที่หน้าขีดจำกัดของอัตราและราคา
  • ขีดจำกัดคำขอ: เครื่องมือนี้ประมวลผล URL ได้สูงสุด 20 รายการต่อคำขอ
  • ขนาดเนื้อหา URL: ขนาดสูงสุดของเนื้อหาที่ดึงมาจาก URL เดียวคือ 34 MB

ประเภทเนื้อหาที่รองรับและไม่รองรับ

เครื่องมือนี้สามารถดึงเนื้อหาจาก URL ที่มีเนื้อหาประเภทต่อไปนี้

  • ข้อความ (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 (ดูการทำความเข้าใจวิดีโอเพื่อดูวิธีประมวลผล URL ของ YouTube)
  • ไฟล์ Google Workspace เช่น Google เอกสารหรือสเปรดชีต
  • ไฟล์วิดีโอและเสียง

ขั้นตอนถัดไป