URL context

בעזרת הכלי להוספת הקשר של כתובות URL, תוכלו לספק ל-Gemini כתובות URL כרקע נוסף להנחיה. לאחר מכן, המודל יכול לאחזר תוכן מכתובות ה-URL ולהשתמש בתוכן הזה כדי לקבל מידע ולעצב את התשובה שלו.

הכלי הזה שימושי למשימות כמו:

  • חילוץ נקודות נתונים או נקודות מרכזיות לדיון ממאמרים
  • השוואת מידע בין כמה קישורים
  • סינתזה של נתונים מכמה מקורות
  • מענה על שאלות על סמך התוכן של דף או דפים ספציפיים
  • ניתוח תוכן למטרות ספציפיות (למשל כתיבת תיאור משרה או יצירת שאלות למבחן)

במדריך הזה נסביר איך משתמשים בכלי להקשר של כתובת URL ב-Gemini API.

שימוש בהקשר של כתובת ה-URL

יש שתי דרכים עיקריות להשתמש בכלי ההקשר של כתובת ה-URL: לבד או בשילוב עם הצגת מידע על הנושא באמצעות חיפוש Google.

הקשר של כתובת ה-URL בלבד

אתם מספקים כתובות URL ספציפיות שאתם רוצים שהמודל ינתח ישירות בהנחיה.

הנחיות לדוגמה:

Summarize this document: YOUR_URLs

Extract the key features from the product description on this page: YOUR_URLs

התאמה לנתונים באמצעות חיפוש Google + הקשר של כתובת ה-URL

אפשר גם להפעיל את ההגדרות 'הקשר של כתובת ה-URL' ו'התאמה ל-Google Search' בו-זמנית. אפשר להזין הנחיה עם כתובות URL או בלי כתובות URL. יכול להיות שהמודל יחפש קודם מידע רלוונטי, ואז ישתמש בכלי להקשר של כתובות URL כדי לקרוא את התוכן של תוצאות החיפוש כדי לקבל הבנה מעמיקה יותר.

הנחיות לדוגמה:

Give me three day events 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.

דוגמאות לקוד עם הקשר של כתובת URL בלבד

Python

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

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

url_context_tool = Tool(
    url_context = types.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

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

const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.5-flash-preview-05-20",
    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 "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-05-20:generateContent?key=$GOOGLE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
      "contents": [
          {
              "parts": [
                  {"text": "Compare recipes from YOUR_URL1 and YOUR_URL2"}
              ]
          }
      ],
      "tools": [
          {
              "url_context": {}
          }
      ]
  }' > result.json

cat result.json

Python

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

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

tools = []
tools.append(Tool(url_context=types.UrlContext))
tools.append(Tool(google_search=types.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

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

const ai = new GoogleGenAI({ apiKey: "GEMINI_API_KEY" });

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.5-flash-preview-05-20",
    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-preview-05-20:generateContent?key=$GOOGLE_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

פרטים נוספים על 'הכנה לקראת השיחה' בחיפוש Google זמינים בדף הסקירה הכללית.

תגובה לפי הקשר

התשובה של המודל תהיה מבוססת על התוכן שהוא אחזר מכתובות ה-URL. אם המודל אחזר תוכן מכתובות ה-URL, התשובה תכלול את הערך url_context_metadata. התגובה עשויה להיראות כך (חלקים מהתגובה הושמטו כדי לקצר את הטקסט):

{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "text": "... \n"
          }
        ],
        "role": "model"
      },
      ...
      "url_context_metadata":
      {
          "url_metadata":
          [
            {
              "retrieved_url": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/1234567890abcdef",
              "url_retrieval_status": <UrlRetrievalStatus.URL_RETRIEVAL_STATUS_SUCCESS: "URL_RETRIEVAL_STATUS_SUCCESS">
            },
            {
              "retrieved_url": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/abcdef1234567890",
              "url_retrieval_status": <UrlRetrievalStatus.URL_RETRIEVAL_STATUS_SUCCESS: "URL_RETRIEVAL_STATUS_SUCCESS">
            },
            {
              "retrieved_url": "YOUR_URL",
              "url_retrieval_status": <UrlRetrievalStatus.URL_RETRIEVAL_STATUS_SUCCESS: "URL_RETRIEVAL_STATUS_SUCCESS">
            },
            {
              "retrieved_url": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/fedcba0987654321",
              "url_retrieval_status": <UrlRetrievalStatus.URL_RETRIEVAL_STATUS_SUCCESS: "URL_RETRIEVAL_STATUS_SUCCESS">
            }
          ]
        }
    }
}

מודלים נתמכים

מגבלות

  • בכל בקשה לניתוח, הכלי ישתמש בעד 20 כתובות URL.
  • כדי לקבל את התוצאות הטובות ביותר במהלך השלב הניסיוני, מומלץ להשתמש בכלי בדפי אינטרנט רגילים ולא בתוכן מולטימדיה כמו סרטונים ב-YouTube.
  • במהלך השלב הניסיוני, השימוש בכלי הוא בחינם. החיוב יתבצע מאוחר יותר.
  • לגרסה הניסיונית יש את המכסות הבאות:

    • 1,500 שאילתות ביום לכל פרויקט לבקשות שנשלחות דרך Gemini API
    • 100 שאילתות ביום לכל משתמש ב-Google AI Studio