เครื่องมือบริบท URL ช่วยให้คุณระบุบริบทเพิ่มเติมให้กับโมเดลในรูปแบบของ URL ได้ โมเดลจะเข้าถึงเนื้อหาจาก URL เหล่านั้นเพื่อแจ้งและปรับปรุงคำตอบของโมเดล
บริบท URL มีประโยชน์ดังนี้
ดึงข้อมูล: ระบุข้อมูลที่เฉพาะเจาะจง เช่น ราคา ชื่อ หรือ ข้อค้นพบที่สำคัญจากบทความหรือ URL หลายรายการ
เปรียบเทียบข้อมูล: วิเคราะห์รายงาน บทความ หรือ PDF หลายรายการ เพื่อระบุความแตกต่างและติดตามแนวโน้ม
สังเคราะห์และสร้างเนื้อหา: รวมข้อมูลจาก URL แหล่งที่มาหลายรายการเพื่อสร้างสรุป บล็อกโพสต์ รายงาน หรือคำถามทดสอบที่ถูกต้อง
วิเคราะห์โค้ดและเนื้อหาทางเทคนิค: ระบุ URL ไปยังที่เก็บ GitHub หรือ เอกสารทางเทคนิคเพื่ออธิบายโค้ด สร้างวิธีการตั้งค่า หรือ ตอบคำถาม
โปรดอ่านแนวทางปฏิบัติแนะนำและข้อจำกัดเมื่อใช้เครื่องมือบริบท URL
รุ่นที่รองรับ
gemini-3.1-pro-previewgemini-3.8-flash(และรุ่นเก่ากว่าอย่างgemini-3.7-flash,gemini-3.6-flashและgemini-3.5-flash)gemini-3.5-flash-lite(และgemini-3.1-flash-liteรุ่นเก่า)
โมเดล Gemini 2.5 แบบใช้งานทั่วไปรองรับความสามารถนี้ แต่ทั้งหมดเลิกใช้งานแล้ว
ภาษาที่สนับสนุน
ดูภาษาที่รองรับสำหรับGemini โมเดล
ใช้เครื่องมือบริบท URL
คุณใช้เครื่องมือบริบท URL ได้ 2 วิธีหลักๆ ดังนี้
เครื่องมือบริบท URL เท่านั้น
|
คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดเฉพาะของผู้ให้บริการ ในหน้านี้ |
เมื่อสร้างอินสแตนซ์ GenerativeModel ให้ระบุ UrlContext เป็นเครื่องมือ
จากนั้นให้ระบุ URL ที่เฉพาะเจาะจงซึ่งคุณต้องการให้โมเดลเข้าถึงและวิเคราะห์ในพรอมต์โดยตรง
ตัวอย่างต่อไปนี้แสดงวิธีเปรียบเทียบสูตรอาหาร 2 สูตรจากเว็บไซต์ต่างๆ
Swift
import FirebaseAILogic
// 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",
// Enable the URL context tool.
tools: [Tool.urlContext()]
)
// Specify one or more URLs for the tool to access.
let url1 = "FIRST_RECIPE_URL"
let url2 = "SECOND_RECIPE_URL"
// Provide the URLs in the prompt sent in the request.
let prompt = "Compare the ingredients and cooking times from the recipes at \(url1) and \(url2)"
// Get and handle the model's response.
let response = try await model.generateContent(prompt)
print(response.text ?? "No text in response.")
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",
// Enable the URL context tool.
tools = listOf(Tool.urlContext())
)
// Specify one or more URLs for the tool to access.
val url1 = "FIRST_RECIPE_URL"
val url2 = "SECOND_RECIPE_URL"
// Provide the URLs in the prompt sent in the request.
val prompt = "Compare the ingredients and cooking times from the recipes at $url1 and $url2"
// Get and handle the model's response.
val response = model.generateContent(prompt)
print(response.text)
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,
// Enable the URL context tool.
List.of(Tool.urlContext(new UrlContext())));
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
// Specify one or more URLs for the tool to access.
String url1 = "FIRST_RECIPE_URL";
String url2 = "SECOND_RECIPE_URL";
// Provide the URLs in the prompt sent in the request.
String prompt = "Compare the ingredients and cooking times from the recipes at " + url1 + " and " + url2 + "";
ListenableFuture response = model.generateContent(prompt);
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);
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",
// Enable the URL context tool.
tools: [{ urlContext: {} }]
}
);
// Specify one or more URLs for the tool to access.
const url1 = "FIRST_RECIPE_URL"
const url2 = "SECOND_RECIPE_URL"
// Provide the URLs in the prompt sent in the request.
const prompt = `Compare the ingredients and cooking times from the recipes at ${url1} and ${url2}`
// Get and handle the model's response.
const result = await model.generateContent(prompt);
console.log(result.response.text());
Dart
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_ai/firebase_ai.dart';
import 'firebase_options.dart';
// Initialize FirebaseApp
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports your use case.
final model = FirebaseAI.googleAI().generativeModel(
model: 'GEMINI_MODEL_NAME',
// Enable the URL context tool.
tools: [
Tool.urlContext(),
],
);
// Specify one or more URLs for the tool to access.
final url1 = "FIRST_RECIPE_URL";
final url2 = "SECOND_RECIPE_URL";
// Provide the URLs in the prompt sent in the request.
final prompt = "Compare the ingredients and cooking times from the recipes at $url1 and $url2";
// Get and handle the model's response.
final response = await model.generateContent([Content.text(prompt)]);
print(response.text);
Unity
using Firebase;
using Firebase.AI;
// Initialize the Gemini Developer API backend service.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create a `GenerativeModel` instance with a model that supports your use case.
var model = ai.GetGenerativeModel(
modelName: "GEMINI_MODEL_NAME",
// Enable the URL context tool.
tools: new[] { new Tool(new UrlContext()) }
);
// Specify one or more URLs for the tool to access.
var url1 = "FIRST_RECIPE_URL";
var url2 = "SECOND_RECIPE_URL";
// Provide the URLs in the prompt sent in the request.
var prompt = $"Compare the ingredients and cooking times from the recipes at {url1} and {url2}";
// Get and handle the model's response.
var response = await model.GenerateContentAsync(prompt);
UnityEngine.Debug.Log(response.Text ?? "No text in response.");
ดูวิธีเลือกโมเดล (ไม่บังคับ) ที่เหมาะสมกับกรณีการใช้งานและแอปของคุณ
บริบท URL ร่วมกับการอ้างอิงจากข้อมูลพื้นฐานด้วย Google Search
|
คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดเฉพาะของผู้ให้บริการ ในหน้านี้ |
คุณเปิดใช้ทั้งบริบท URL และการอ้างอิงกับ
เมื่อเปิดใช้การอ้างอิงจาก
กรณีการใช้งานบางส่วนมีดังต่อไปนี้
คุณระบุ URL ในพรอมต์เพื่อช่วยในการตอบกลับที่สร้างขึ้นบางส่วน อย่างไรก็ตาม โมเดลยังคงต้องการข้อมูลเพิ่มเติมเกี่ยวกับหัวข้ออื่นๆ เพื่อสร้างคำตอบที่เหมาะสม จึงใช้เครื่องมือการเชื่อมโยงกับ
Google Search ตัวอย่างพรอมต์
Give me a three day event schedule based on YOUR_URL. Also what do I need to pack according to the weather?คุณไม่ได้ระบุ URL ในพรอมต์เลย ดังนั้น โมเดลจึงใช้การกราวด์ด้วย
Google Search เครื่องมือ เพื่อค้นหา URL ที่เกี่ยวข้อง แล้วใช้เครื่องมือบริบท URL เพื่อวิเคราะห์เนื้อหา ของ URL เหล่านั้นเพื่อสร้างคำตอบที่เหมาะสมตัวอย่างพรอมต์
Recommend 3 beginner-level books to learn about the latest YOUR_SUBJECT.
ตัวอย่างต่อไปนี้แสดงวิธีเปิดใช้และใช้เครื่องมือทั้ง 2 อย่าง ได้แก่ บริบท URL และ
การอ้างอิงด้วย
Swift
import FirebaseAILogic
// 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",
// Enable both the URL context tool and Google Search tool.
tools: [
Tool.urlContex(),
Tool.googleSearch()
]
)
// Specify one or more URLs for the tool to access.
let url = "YOUR_URL"
// Provide the URLs in the prompt sent in the request.
// If the model can't generate a response using its own knowledge or the content in the specified URL,
// then the model will use the grounding with Google Search tool.
let prompt = "Give me a three day event schedule based on \(url). Also what do I need to pack according to the weather?"
// Get and handle the model's response.
let response = try await model.generateContent(prompt)
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",
// Enable both the URL context tool and Google Search tool.
tools = listOf(Tool.urlContext(), Tool.googleSearch())
)
// Specify one or more URLs for the tool to access.
val url = "YOUR_URL"
// Provide the URLs in the prompt sent in the request.
// If the model can't generate a response using its own knowledge or the content in the specified URL,
// then the model will use the grounding with Google Search tool.
val prompt = "Give me a three day event schedule based on $url. Also what do I need to pack according to the weather?"
// Get and handle the model's response.
val response = model.generateContent(prompt)
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,
// Enable both the URL context tool and Google Search tool.
List.of(Tool.urlContext(new UrlContext()), Tool.googleSearch(new GoogleSearch())));
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
// Specify one or more URLs for the tool to access.
String url = "YOUR_URL";
// Provide the URLs in the prompt sent in the request.
// If the model can't generate a response using its own knowledge or the content in the specified URL,
// then the model will use the grounding with Google Search tool.
String prompt = "Give me a three day event schedule based on " + url + ". Also what do I need to pack according to the weather?";
ListenableFuture response = model.generateContent(prompt);
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",
// Enable both the URL context tool and Google Search tool.
tools: [{ urlContext: {} }, { googleSearch: {} }],
}
);
// Specify one or more URLs for the tool to access.
const url = "YOUR_URL"
// Provide the URLs in the prompt sent in the request.
// If the model can't generate a response using its own knowledge or the content in the specified URL,
// then the model will use the grounding with Google Search tool.
const prompt = `Give me a three day event schedule based on ${url}. Also what do I need to pack according to the weather?`
// Get and handle the model's response.
const result = await model.generateContent(prompt);
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
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_ai/firebase_ai.dart';
import 'firebase_options.dart';
// Initialize FirebaseApp.
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
// Initialize the Gemini Developer API backend service.
// Create a `GenerativeModel` instance with a model that supports your use case.
final model = FirebaseAI.googleAI().generativeModel(
model: 'GEMINI_MODEL_NAME',
// Enable both the URL context tool and Google Search tool.
tools: [
Tool.urlContext(),
Tool.googleSearch(),
],
);
// Specify one or more URLs for the tool to access.
final url = "YOUR_URL";
// Provide the URLs in the prompt sent in the request.
// If the model can't generate a response using its own knowledge or the content in the specified URL,
// then the model will use the grounding with Google Search tool.
final prompt = "Give me a three day event schedule based on $url. Also what do I need to pack according to the weather?";
final response = await model.generateContent([Content.text(prompt)]);
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
Unity
using Firebase;
using Firebase.AI;
// Initialize the Gemini Developer API backend service.
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
// Create a `GenerativeModel` instance with a model that supports your use case.
var model = ai.GetGenerativeModel(
modelName: "GEMINI_MODEL_NAME",
// Enable both the URL context tool and Google Search tool.
tools: new[] { new Tool(new GoogleSearch()), new Tool(new UrlContext()) }
);
// Specify one or more URLs for the tool to access.
var url = "YOUR_URL";
// Provide the URLs in the prompt sent in the request.
// If the model can't generate a response using its own knowledge or the content in the specified URL,
// then the model will use the grounding with Google Search tool.
var prompt = $"Give me a three day event schedule based on {url}. Also what do I need to pack according to the weather?";
// Get and handle the model's response.
var response = await model.GenerateContentAsync(prompt);
UnityEngine.Debug.Log(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
ดูวิธีเลือกโมเดล (ไม่บังคับ) ที่เหมาะสมกับกรณีการใช้งานและแอปของคุณ
วิธีการทำงานของเครื่องมือบริบท URL
เครื่องมือบริบท URL ใช้กระบวนการดึงข้อมูล 2 ขั้นตอนเพื่อรักษาสมดุลระหว่างความเร็ว ต้นทุน และการเข้าถึงข้อมูลล่าสุด
ขั้นตอนที่ 1: เมื่อคุณระบุ URL ที่เฉพาะเจาะจง เครื่องมือจะพยายามดึงข้อมูล เนื้อหาจากแคชดัชนีภายในก่อน ซึ่งทำหน้าที่เป็นแคชที่ได้รับการเพิ่มประสิทธิภาพอย่างสูง
ขั้นตอนที่ 2: หาก URL ไม่พร้อมใช้งานในดัชนี (เช่น หากเป็นหน้าเว็บใหม่มาก) เครื่องมือจะกลับไปใช้การดึงข้อมูลเวอร์ชันที่ใช้จริงโดยอัตโนมัติ ซึ่งจะเข้าถึง URL โดยตรงเพื่อดึงข้อมูลเนื้อหาแบบเรียลไทม์
แนวทางปฏิบัติแนะนำ
ระบุ URL ที่เฉพาะเจาะจง: เพื่อให้ได้ผลลัพธ์ที่ดีที่สุด ให้ระบุ URL โดยตรงไปยังเนื้อหาที่คุณต้องการให้โมเดลวิเคราะห์ โมเดลจะดึงข้อมูลเนื้อหาจาก URL ที่คุณระบุเท่านั้น ไม่ดึงเนื้อหาจากลิงก์ที่ซ้อนกัน
ตรวจสอบการช่วยเหลือพิเศษ: ตรวจสอบว่า URL ที่คุณระบุไม่ได้นำไปยังหน้าเว็บที่ต้องมีการเข้าสู่ระบบหรืออยู่หลังเพย์วอลล์
ใช้ URL ที่สมบูรณ์: ระบุ URL แบบเต็ม รวมถึงโปรโตคอล (เช่น
https://www.example.comแทนที่จะเป็นexample.comเท่านั้น)
ทำความเข้าใจคำตอบ
คำตอบของโมเดลจะอิงตามเนื้อหาที่ดึงมาจาก URL
หากโมเดลดึงเนื้อหาจาก URL การตอบกลับจะมี
url_context_metadata คำตอบดังกล่าวอาจมีลักษณะดังนี้
(เราได้ละเว้นบางส่วนของคำตอบเพื่อความกระชับ)
{
"candidates": [
{
"content": {
"parts": [
{
"text": "... \n"
}
],
"role": "model"
},
...
"url_context_metadata":
{
"url_metadata":
[
{
"retrieved_url": "https://www.example.com",
"url_retrieval_status": "URL_RETRIEVAL_STATUS_SUCCESS"
},
{
"retrieved_url": "https://www.example.org",
"url_retrieval_status": "URL_RETRIEVAL_STATUS_SUCCESS"
},
]
}
}
]
}
การตรวจสอบความปลอดภัย
ระบบจะตรวจสอบการกลั่นกรองเนื้อหาใน URL เพื่อยืนยันว่า URL เป็นไปตาม
มาตรฐานด้านความปลอดภัย หาก URL ที่คุณระบุไม่ผ่านการตรวจสอบนี้ คุณจะได้รับ
url_retrieval_statusของ URL_RETRIEVAL_STATUS_UNSAFE
ข้อจำกัด
ข้อจำกัดบางประการของเครื่องมือบริบท URL มีดังนี้
การใช้ร่วมกับการเรียกใช้ฟังก์ชัน: ไม่สามารถใช้เครื่องมือบริบท URL ในคำขอที่ใช้การเรียกใช้ฟังก์ชันด้วยได้
ขีดจำกัด URL ต่อคำขอ: จำนวน URL สูงสุดต่อคำขอคือ 20 URL
ขีดจำกัดขนาดเนื้อหา URL: ขนาดสูงสุดสำหรับเนื้อหาที่ดึงมาจาก URL เดียวคือ 34 MB
ความใหม่: เครื่องมือไม่ได้ดึงข้อมูลหน้าเว็บเวอร์ชันที่ใช้งานจริง ดังนั้นอาจมีปัญหาเกี่ยวกับความใหม่หรือข้อมูลที่อาจล้าสมัย
การเข้าถึง URL แบบสาธารณะ: URL ที่ระบุต้องเข้าถึงได้แบบสาธารณะบน เว็บ ไม่รองรับเนื้อหาเพย์วอลล์ เนื้อหาที่ต้องให้ผู้ใช้ลงชื่อเข้าใช้ เครือข่ายส่วนตัว ที่อยู่ localhost (เช่น
localhostหรือ127.0.0.1) และบริการการสร้างอุโมงค์เสมือน (เช่น ngrok หรือ pinggy)
ประเภทเนื้อหาที่รองรับและไม่รองรับ
รองรับ: เครื่องมือสามารถดึงเนื้อหา จาก 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 (ให้ดูวิเคราะห์วิดีโอแทน)
ไฟล์วิดีโอและเสียง (ดูวิเคราะห์วิดีโอหรือวิเคราะห์เสียงแทน)
ไฟล์ Google Workspace เช่น Google เอกสารหรือสเปรดชีต
(หากใช้ Agent Platform Gemini API (formerly Vertex AI)) Cloud Storage URL
Gemini Developer API ไม่รองรับ URL ประเภทนี้ ไม่ว่าคุณจะเข้าถึง URL นี้ด้วยวิธีใดก็ตามเนื้อหาที่เข้าถึงแบบสาธารณะไม่ได้ ไม่รองรับรายการต่อไปนี้ เนื้อหาเพย์วอลล์ เนื้อหาที่กำหนดให้ผู้ใช้ต้องลงชื่อเข้าใช้ เครือข่ายส่วนตัว ที่อยู่ localhost (เช่น
localhostหรือ127.0.0.1) และบริการการสร้างอุโมงค์เสมือน (เช่น ngrok หรือ pinggy)
โทเค็นเครื่องมือการกำหนดราคาและการนับ
เนื้อหาที่ดึงมาจาก 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 ในเอกสารประกอบของผู้ให้บริการ Gemini API ที่คุณเลือกได้ที่ Gemini Developer API | Agent Platform Gemini API (formerly Vertex AI)