เครื่องมือบริบท URL ช่วยให้คุณระบุบริบทเพิ่มเติมแก่โมเดลในรูปแบบของ URL ได้ โมเดลสามารถเข้าถึงเนื้อหาจาก URL เหล่านั้นเพื่อแจ้งและปรับปรุงคำตอบของตน
บริบท URL มีประโยชน์ดังนี้
ดึงข้อมูล: ระบุข้อมูลที่เฉพาะเจาะจง เช่น ราคา ชื่อ หรือผลการค้นหาที่สำคัญจากบทความหรือ URL หลายรายการ
เปรียบเทียบข้อมูล: วิเคราะห์รายงาน บทความ หรือ PDF หลายรายการเพื่อระบุความแตกต่างและติดตามแนวโน้ม
สังเคราะห์และสร้างเนื้อหา: รวมข้อมูลจาก URL แหล่งที่มาหลายรายการเพื่อสร้างสรุป บล็อกโพสต์ รายงาน หรือคำถามทดสอบที่ถูกต้อง
วิเคราะห์โค้ดและเนื้อหาทางเทคนิค: ระบุ URL ไปยังที่เก็บ GitHub หรือเอกสารทางเทคนิคเพื่ออธิบายโค้ด สร้างวิธีการตั้งค่า หรือตอบคำถาม
โปรดอ่านแนวทางปฏิบัติแนะนำและข้อจำกัดเมื่อใช้เครื่องมือบริบท URL
โมเดลที่รองรับ
gemini-4-argongemini-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-3.1-pro-preview และโมเดล Gemini 2.5 สำหรับการใช้งานทั่วไป (ซึ่งเลิกใช้งานแล้ว) ก็รองรับความสามารถนี้เช่นกัน
ภาษาที่สนับสนุน
ดูภาษาที่รองรับสำหรับโมเดล Gemini
ใช้เครื่องมือบริบท URL
คุณใช้เครื่องมือบริบท URL ได้ 2 วิธีหลักๆ ดังนี้
เครื่องมือ Context Tools สำหรับ 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: [.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.");
ดูวิธีเลือกโมเดล ที่เหมาะกับ Use Case และแอปของคุณ
บริบท 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: [
.urlContext(),
.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
ดูวิธีเลือกโมเดล ที่เหมาะกับ Use Case และแอปของคุณ
วิธีการทำงานของเครื่องมือ Context Tools สำหรับ 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_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)) URL Cloud Storage
Gemini Developer API ไม่รองรับ 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)