การอ้างอิงข้อมูลด้วย Google Search จะเชื่อมต่อโมเดล Gemini กับเนื้อหาบนเว็บแบบเรียลไทม์ และทำงานได้กับทุกภาษาที่พร้อมให้บริการ ซึ่งจะช่วยให้ Gemini สามารถให้คำตอบที่แม่นยำยิ่งขึ้นและอ้างอิงแหล่งข้อมูลที่ตรวจสอบได้นอกเหนือจากวันที่ตัดความรู้
การอ้างอิงช่วยให้คุณสร้างแอปพลิเคชันที่ทำสิ่งต่อไปนี้ได้
- เพิ่มความถูกต้องตามข้อเท็จจริง: ลดอาการหลอนของโมเดลโดยอิงคำตอบตามข้อมูลในโลกแห่งความเป็นจริง
- เข้าถึงข้อมูลแบบเรียลไทม์: ตอบคำถามเกี่ยวกับเหตุการณ์และหัวข้อล่าสุด
ระบุแหล่งที่มา: สร้างความไว้วางใจของผู้ใช้โดยแสดงแหล่งที่มาของข้อกล่าวอ้างของโมเดล
Python
from google import genai
from google.genai import types
client = genai.Client()
grounding_tool = types.Tool(
google_search=types.GoogleSearch()
)
config = types.GenerateContentConfig(
tools=[grounding_tool]
)
response = client.models.generate_content(
model="gemini-3-flash-preview",
contents="Who won the euro 2024?",
config=config,
)
print(response.text)
JavaScript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const groundingTool = {
googleSearch: {},
};
const config = {
tools: [groundingTool],
};
const response = await ai.models.generateContent({
model: "gemini-3-flash-preview",
contents: "Who won the euro 2024?",
config,
});
console.log(response.text);
REST
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-X POST \
-d '{
"contents": [
{
"parts": [
{"text": "Who won the euro 2024?"}
]
}
],
"tools": [
{
"google_search": {}
}
]
}'
ดูข้อมูลเพิ่มเติมได้โดยลองใช้สมุดบันทึกเครื่องมือค้นหา
วิธีการทำงานของการเชื่อมต่อแหล่งข้อมูลกับ Google Search
เมื่อเปิดใช้google_searchเครื่องมือ โมเดลจะจัดการเวิร์กโฟลว์ทั้งหมด
ของการค้นหา ประมวลผล และอ้างอิงข้อมูลโดยอัตโนมัติ

- พรอมต์ของผู้ใช้: แอปพลิเคชันของคุณจะส่งพรอมต์ของผู้ใช้ไปยัง Gemini API
โดยเปิดใช้เครื่องมือ
google_search - การวิเคราะห์พรอมต์: โมเดลจะวิเคราะห์พรอมต์และพิจารณาว่า Google Search สามารถปรับปรุงคำตอบได้หรือไม่
- Google Search: หากจำเป็น โมเดลจะสร้างคำค้นหาอย่างน้อย 1 รายการโดยอัตโนมัติและดำเนินการค้นหา
- การประมวลผลผลการค้นหา: โมเดลจะประมวลผลผลการค้นหา สังเคราะห์ข้อมูล และสร้างคำตอบ
- คำตอบที่อิงตามข้อมูล: API จะแสดงคำตอบสุดท้ายที่ใช้งานง่ายซึ่ง
อิงตามผลการค้นหา คำตอบนี้ประกอบด้วยข้อความคำตอบของโมเดลและ
groundingMetadataพร้อมคำค้นหา ผลการค้นหาเว็บ และ การอ้างอิง
ทำความเข้าใจคำตอบที่อิงตามข้อมูล
เมื่อมีการอ้างอิงคำตอบสำเร็จ คำตอบจะมีฟิลด์ groundingMetadata
ข้อมูลที่มีโครงสร้างนี้มีความสําคัญอย่างยิ่งต่อการยืนยัน
การอ้างสิทธิ์และการสร้างประสบการณ์การอ้างอิงที่สมบูรณ์ในแอปพลิเคชันของคุณ
{
"candidates": [
{
"content": {
"parts": [
{
"text": "Spain won Euro 2024, defeating England 2-1 in the final. This victory marks Spain's record fourth European Championship title."
}
],
"role": "model"
},
"groundingMetadata": {
"webSearchQueries": [
"UEFA Euro 2024 winner",
"who won euro 2024"
],
"searchEntryPoint": {
"renderedContent": "<!-- HTML and CSS for the search widget -->"
},
"groundingChunks": [
{"web": {"uri": "https://vertexaisearch.cloud.google.com.....", "title": "aljazeera.com"}},
{"web": {"uri": "https://vertexaisearch.cloud.google.com.....", "title": "uefa.com"}}
],
"groundingSupports": [
{
"segment": {"startIndex": 0, "endIndex": 85, "text": "Spain won Euro 2024, defeatin..."},
"groundingChunkIndices": [0]
},
{
"segment": {"startIndex": 86, "endIndex": 210, "text": "This victory marks Spain's..."},
"groundingChunkIndices": [0, 1]
}
]
}
}
]
}
Gemini API จะแสดงข้อมูลต่อไปนี้พร้อมกับ groundingMetadata
webSearchQueries: อาร์เรย์ของคำค้นหาที่ใช้ ซึ่งมีประโยชน์สําหรับ การแก้ไขข้อบกพร่องและทําความเข้าใจกระบวนการให้เหตุผลของโมเดลsearchEntryPoint: มี HTML และ CSS เพื่อแสดงผลคำแนะนำในการค้นหาที่จำเป็น ดูข้อกำหนดในการใช้งานทั้งหมดได้ในข้อกำหนดในการให้บริการgroundingChunks: อาร์เรย์ของออบเจ็กต์ที่มีแหล่งที่มาของเว็บ (uriและtitle)groundingSupports: อาร์เรย์ของก้อนข้อมูลเพื่อเชื่อมต่อคำตอบของโมเดลtextกับ แหล่งที่มาในgroundingChunksแต่ละก้อนจะลิงก์ข้อความsegment(กำหนดโดยstartIndexและendIndex) กับgroundingChunkIndicesอย่างน้อย 1 รายการ ซึ่งเป็นกุญแจสำคัญในการสร้างการอ้างอิงในบรรทัด
นอกจากนี้ คุณยังใช้การอ้างอิงด้วย Google Search ร่วมกับเครื่องมือบริบท URL เพื่ออ้างอิงคำตอบจากทั้งข้อมูลเว็บสาธารณะและ URL ที่เฉพาะเจาะจงที่คุณระบุได้ด้วย
การระบุแหล่งที่มาด้วยการอ้างอิงในบรรทัด
API จะแสดงข้อมูลการอ้างอิงที่มีโครงสร้าง ซึ่งช่วยให้คุณควบคุมวิธี
แสดงแหล่งที่มาในอินเทอร์เฟซผู้ใช้ได้อย่างสมบูรณ์ คุณสามารถใช้ฟิลด์ groundingSupports
และ groundingChunks เพื่อลิงก์คำกล่าวของโมเดลกับแหล่งที่มาโดยตรง ต่อไปนี้คือรูปแบบทั่วไปสำหรับการประมวลผลข้อมูลเมตาเพื่อสร้าง
การตอบกลับพร้อมการอ้างอิงแบบอินไลน์ที่คลิกได้
Python
def add_citations(response):
text = response.text
supports = response.candidates[0].grounding_metadata.grounding_supports
chunks = response.candidates[0].grounding_metadata.grounding_chunks
# Sort supports by end_index in descending order to avoid shifting issues when inserting.
sorted_supports = sorted(supports, key=lambda s: s.segment.end_index, reverse=True)
for support in sorted_supports:
end_index = support.segment.end_index
if support.grounding_chunk_indices:
# Create citation string like [1](link1)[2](link2)
citation_links = []
for i in support.grounding_chunk_indices:
if i < len(chunks):
uri = chunks[i].web.uri
citation_links.append(f"[{i + 1}]({uri})")
citation_string = ", ".join(citation_links)
text = text[:end_index] + citation_string + text[end_index:]
return text
# Assuming response with grounding metadata
text_with_citations = add_citations(response)
print(text_with_citations)
JavaScript
function addCitations(response) {
let text = response.text;
const supports = response.candidates[0]?.groundingMetadata?.groundingSupports;
const chunks = response.candidates[0]?.groundingMetadata?.groundingChunks;
// Sort supports by end_index in descending order to avoid shifting issues when inserting.
const sortedSupports = [...supports].sort(
(a, b) => (b.segment?.endIndex ?? 0) - (a.segment?.endIndex ?? 0),
);
for (const support of sortedSupports) {
const endIndex = support.segment?.endIndex;
if (endIndex === undefined || !support.groundingChunkIndices?.length) {
continue;
}
const citationLinks = support.groundingChunkIndices
.map(i => {
const uri = chunks[i]?.web?.uri;
if (uri) {
return `[${i + 1}](${uri})`;
}
return null;
})
.filter(Boolean);
if (citationLinks.length > 0) {
const citationString = citationLinks.join(", ");
text = text.slice(0, endIndex) + citationString + text.slice(endIndex);
}
}
return text;
}
const textWithCitations = addCitations(response);
console.log(textWithCitations);
คำตอบใหม่ที่มีการอ้างอิงในบรรทัดจะมีลักษณะดังนี้
Spain won Euro 2024, defeating England 2-1 in the final.[1](https:/...), [2](https:/...), [4](https:/...), [5](https:/...) This victory marks Spain's record-breaking fourth European Championship title.[5]((https:/...), [2](https:/...), [3](https:/...), [4](https:/...)
ราคา
เมื่อใช้การอ้างอิงกับ Google Search ด้วย Gemini 3 ระบบจะเรียกเก็บเงินจากโปรเจ็กต์ของคุณ
สำหรับคำค้นหาแต่ละรายการที่โมเดลตัดสินใจดำเนินการ หากโมเดลตัดสินใจที่จะ
เรียกใช้คำค้นหาหลายรายการเพื่อตอบพรอมต์เดียว (เช่น
ค้นหา "UEFA Euro 2024 winner" และ "Spain vs England Euro 2024 final
score" ภายใน API Call เดียวกัน) ระบบจะนับเป็นการใช้เครื่องมือที่เรียกเก็บเงินได้ 2 ครั้ง
สำหรับคำขอนั้น เพื่อวัตถุประสงค์ในการเรียกเก็บเงิน เราจะไม่สนใจคำค้นหาบนเว็บที่ว่างเปล่าเมื่อนับคำค้นหาที่ไม่ซ้ำกัน โมเดลการเรียกเก็บเงินนี้ใช้กับโมเดล Gemini 3 เท่านั้น เมื่อคุณใช้การอ้างอิงจาก Search กับโมเดล Gemini 2.5 หรือรุ่นเก่ากว่า ระบบจะเรียกเก็บเงินจากโปรเจ็กต์ต่อพรอมต์
ดูข้อมูลการกำหนดราคาโดยละเอียดได้ที่หน้าการกำหนดราคาของ Gemini API
รุ่นที่รองรับ
ไม่รวมโมเดลเวอร์ชันทดลองและเวอร์ชันตัวอย่าง คุณดูความสามารถของโมเดลได้ในหน้าภาพรวม โมเดล
| รุ่น | การเชื่อมต่อแหล่งข้อมูลกับ Google Search |
|---|---|
| Gemini 2.5 Pro | ✔️ |
| Gemini 2.5 Flash | ✔️ |
| Gemini 2.5 Flash-Lite | ✔️ |
| Gemini 2.0 Flash | ✔️ |
ชุดค่าผสมของเครื่องมือที่รองรับ
คุณสามารถใช้การอ้างอิงจาก Google Search กับเครื่องมืออื่นๆ เช่น การเรียกใช้โค้ดและบริบท URL เพื่อขับเคลื่อน Use Case ที่ซับซ้อนยิ่งขึ้น
ขั้นตอนถัดไป
- ลองใช้การอ้างอิงจาก Google Search ในสูตรการแก้ปัญหาของ Gemini API
- ดูข้อมูลเกี่ยวกับเครื่องมืออื่นๆ ที่พร้อมใช้งาน เช่น การเรียกใช้ฟังก์ชัน
- ดูวิธีเพิ่มพรอมต์ด้วย URL ที่เฉพาะเจาะจงโดยใช้เครื่องมือบริบท URL