การอ้างอิงด้วย Google Search จะเชื่อมต่อโมเดล Gemini กับเนื้อหาเว็บแบบเรียลไทม์ที่พร้อมให้บริการแก่สาธารณะ ซึ่งจะช่วยให้โมเดลให้คำตอบที่แม่นยำและเป็นปัจจุบันมากขึ้น รวมถึงอ้างอิงแหล่งที่มาที่ตรวจสอบได้นอกเหนือจากความรู้ที่มี
การอ้างอิงจาก Google Search มีประโยชน์ดังนี้
- เพิ่มความถูกต้องตามข้อเท็จจริง: ลดการหลอนของโมเดลโดยอิง คำตอบตามข้อมูลในโลกแห่งความเป็นจริง
- เข้าถึงข้อมูลแบบเรียลไทม์: ตอบคำถามเกี่ยวกับเหตุการณ์และหัวข้อล่าสุด
- ระบุการอ้างอิง: สร้างความไว้วางใจจากผู้ใช้หรืออนุญาตให้ผู้ใช้เรียกดู เว็บไซต์ที่เกี่ยวข้องโดยแสดงแหล่งที่มาของคำกล่าวอ้างของโมเดล
- ทำงานที่ซับซ้อนมากขึ้น: ดึงข้อมูลอาร์ติแฟกต์และรูปภาพ วิดีโอ หรือสื่ออื่นๆ ที่เกี่ยวข้องเพื่อช่วยในงานการให้เหตุผล
- ปรับปรุงคำตอบที่เฉพาะเจาะจงภูมิภาคหรือภาษา: ค้นหาข้อมูลที่เฉพาะเจาะจงภูมิภาค หรือช่วยแปลเนื้อหาให้ถูกต้อง
โปรดทราบว่าการรองรับการอ้างอิงสำหรับ Google Search พร้อมใช้งานสำหรับ iOS+, Android และเว็บ โดยจะพร้อมใช้งานสำหรับ Flutter และ Unity ในรุ่นที่จะเปิดตัวเร็วๆ นี้
โมเดลที่รองรับ
gemini-2.5-pro
gemini-2.5-flash
gemini-2.5-flash-lite-preview-06-17
gemini-2.0-flash-001
(และนามแฝงที่อัปเดตอัตโนมัติgemini-2.0-flash
)gemini-2.0-flash-live-preview-04-09
ภาษาที่สนับสนุน
ดูภาษาที่รองรับสำหรับGemini โมเดล
อ้างอิงโมเดลด้วย Google Search
คลิกผู้ให้บริการ Gemini API เพื่อดูเนื้อหาและโค้ดของผู้ให้บริการนั้นๆ ในหน้านี้ |
เมื่อสร้างอินสแตนซ์ GenerativeModel
ให้ระบุ GoogleSearch
เป็นtool
ที่โมเดลใช้สร้างคำตอบได้
Swift
import FirebaseAI
// 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",
// Provide Google Search as a tool that the model can use to generate its response
tools: [Tool.googleSearch()]
)
let response = try await model.generateContent("Who won the euro 2024?")
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",
// Provide Google Search as a tool that the model can use to generate its response
tools = listOf(Tool.GoogleSearch())
)
val response = model.generateContent("Who won the euro 2024?")
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,
// Provide Google Search as a tool that the model can use to generate its response
List.of(Tool.GoogleSearch()));
// Use the GenerativeModelFutures Java compatibility layer which offers
// support for ListenableFuture and Publisher APIs
GenerativeModelFutures model = GenerativeModelFutures.from(ai);
ListenableFuture response = model.generateContent("Who won the euro 2024?");
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",
// Provide Google Search as a tool that the model can use to generate its response
tools: [{ googleSearch: {} }]
}
);
const result = await model.generateContent("Who won the euro 2024?");
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
โดยจะรองรับ Flutter ในรุ่นถัดไป
Unity
โดยจะรองรับ Unity ในรุ่นถัดไป
ดูวิธีเลือกโมเดล ที่เหมาะสมกับกรณีการใช้งานและแอปของคุณ
ใช้ค่าอุณหภูมิ 1.0
(ซึ่งเป็นค่าเริ่มต้นสำหรับโมเดล 2.5 ทั้งหมด) เพื่อให้ได้ผลลัพธ์ที่ดีที่สุด ดูวิธีตั้งอุณหภูมิในการกำหนดค่าของโมเดล
วิธีการทำงานของการอ้างอิงข้อมูลด้วย Google Search
เมื่อคุณใช้เครื่องมือ GoogleSearch
โมเดลจะจัดการเวิร์กโฟลว์ทั้งหมดของ
การค้นหา การประมวลผล และการอ้างอิงข้อมูลโดยอัตโนมัติ
ขั้นตอนการทำงานของโมเดลมีดังนี้
- รับพรอมต์: แอปของคุณจะส่งพรอมต์ไปยังโมเดล Gemini
โดยเปิดใช้เครื่องมือ
GoogleSearch
- วิเคราะห์พรอมต์: โมเดลจะวิเคราะห์พรอมต์และพิจารณาว่า Google Search สามารถปรับปรุงคำตอบได้หรือไม่
- ส่งคำค้นหาไปยัง Google Search: หากจำเป็น โมเดลจะสร้างคำค้นหาอย่างน้อย 1 รายการและดำเนินการโดยอัตโนมัติ
- ประมวลผลผลการค้นหา: โมเดลจะประมวลผลผลการค้นหาของ Google Search และสร้างคำตอบสำหรับพรอมต์ต้นฉบับ
- แสดง "ผลลัพธ์ที่อิงตามข้อมูลพื้นฐาน": โมเดลจะแสดงคำตอบสุดท้ายที่ใช้งานง่าย
ซึ่งอิงตามผลการค้นหาของ Google Search คำตอบนี้
ประกอบด้วยคำตอบที่เป็นข้อความของโมเดลและ
groundingMetadata
พร้อมกับคำค้นหา ผลการค้นหาเว็บ และการอ้างอิง
โปรดทราบว่าการระบุ Google Search เป็นเครื่องมือสำหรับโมเดลไม่ได้หมายความว่าโมเดลจะต้องใช้เครื่องมือ Google Search เสมอเพื่อสร้างคำตอบ ในกรณีเหล่านี้ คำตอบจะไม่มีออบเจ็กต์ groundingMetadata
และจึงไม่ใช่ "ผลลัพธ์ที่อิงตามข้อมูล"
ทำความเข้าใจผลลัพธ์ที่อิงตามข้อมูล
หากโมเดลอ้างอิงคำตอบจากผลการค้นหาของ Google Search คำตอบนั้นจะมีออบเจ็กต์ groundingMetadata
ซึ่งมี Structured Data ที่จำเป็นต่อการยืนยันการกล่าวอ้างและสร้างประสบการณ์การอ้างอิงที่สมบูรณ์ในแอปพลิเคชันของคุณ
ออบเจ็กต์ groundingMetadata
ใน "ผลลัพธ์ที่อิงตามข้อมูล" มีข้อมูลต่อไปนี้
webSearchQueries
: อาร์เรย์ของคำค้นหาที่ส่งไปยัง Google Search ข้อมูลนี้มีประโยชน์ในการแก้ไขข้อบกพร่องและทำความเข้าใจกระบวนการให้เหตุผลของโมเดลsearchEntryPoint
: มี HTML และ CSS เพื่อแสดงผล "คำแนะนำของ Google Search" ที่จำเป็น คุณต้องปฏิบัติตามข้อกำหนดในการใช้งาน "การอ้างอิงจาก Google Search" สำหรับผู้ให้บริการ API ที่คุณเลือก ดังนี้ Gemini Developer API หรือ Vertex AI Gemini API (ดูส่วน ข้อกำหนดในการให้บริการ ภายในข้อกำหนดเฉพาะของบริการ) ดูวิธีใช้และแสดงผลการค้นหาที่อิงตามข้อมูลพื้นฐาน ในภายหลังในหน้านี้groundingChunks
: อาร์เรย์ของออบเจ็กต์ที่มีแหล่งที่มาบนเว็บ (uri
และtitle
)groundingSupports
: อาร์เรย์ของก้อนข้อมูลเพื่อเชื่อมต่อคำตอบของโมเดลtext
กับแหล่งที่มาในgroundingChunks
แต่ละก้อนจะลิงก์ข้อความsegment
(กำหนดโดยstartIndex
และendIndex
) กับgroundingChunkIndices
อย่างน้อย 1 รายการ ช่องนี้ช่วยให้คุณสร้างการอ้างอิงในบรรทัดได้ ดูวิธีใช้และแสดงผลการค้นหาที่อิงตามข้อมูล ในภายหลังของหน้านี้
นี่คือตัวอย่างการตอบกลับที่มีออบเจ็กต์ 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]
}
]
}
}
]
}
ใช้และแสดงผลลัพธ์ที่อิงตามข้อมูล
หากโมเดลใช้เครื่องมือ Google Search เพื่อสร้างคำตอบ โมเดลจะระบุออบเจ็กต์ groundingMetadata
ในคำตอบ
ต้องแสดงคำแนะนำของ Google Search และแนะนำให้แสดงการอ้างอิง
นอกเหนือจากการปฏิบัติตามข้อกำหนดในการใช้เครื่องมือ Google Search แล้ว การแสดงข้อมูลนี้ยังช่วยให้คุณและผู้ใช้ตรวจสอบคำตอบได้ และเพิ่มช่องทางสำหรับการเรียนรู้เพิ่มเติม
(ต้องระบุ) แสดงคำแนะนำของ Google Search
หากคำตอบมี "คำแนะนำของ Google Search" คุณจะต้อง ปฏิบัติตามข้อกำหนดในการใช้งาน "การอ้างอิงจาก Google Search" ซึ่ง รวมถึงวิธีแสดงคำแนะนำของ Google Search
ออบเจ็กต์ groundingMetadata
มี "คำแนะนำของ Google Search"
โดยเฉพาะฟิลด์ searchEntryPoint
ซึ่งมีฟิลด์ renderedContent
ที่ให้รูปแบบ HTML และ CSS ที่เป็นไปตามข้อกำหนด ซึ่งคุณต้องใช้เพื่อ
แสดงคำแนะนำของ Search ในแอป
โปรดอ่านข้อมูลโดยละเอียดเกี่ยวกับ ข้อกำหนดในการแสดงและลักษณะการทำงานของคำแนะนำของ Google Search ในเอกสารประกอบของ Google Cloud โปรดทราบว่าแม้ว่าคำแนะนำโดยละเอียดนี้จะอยู่ในเอกสารประกอบของ Vertex AI Gemini API แต่คำแนะนำนี้ก็ใช้ได้กับผู้ให้บริการ Gemini Developer API ด้วย
ดูตัวอย่างโค้ดได้ในส่วนนี้
(แนะนำ) แสดงการอ้างอิง
ออบเจ็กต์ groundingMetadata
มีข้อมูลการอ้างอิงที่มีโครงสร้าง โดยเฉพาะฟิลด์ groundingSupports
และ groundingChunks
ใช้ข้อมูลนี้เพื่อ
ลิงก์ข้อความของโมเดลกับแหล่งที่มาโดยตรงภายใน UI (ในบรรทัด
และแบบรวม)
ดูตัวอย่างโค้ดได้ในส่วนนี้
ตัวอย่างโค้ด
ตัวอย่างโค้ดเหล่านี้แสดงรูปแบบทั่วไปสำหรับการใช้และแสดงผล ผลลัพธ์ที่อิงตามข้อมูลพื้นฐาน อย่างไรก็ตาม คุณมีหน้าที่ตรวจสอบว่าการติดตั้งใช้งานเฉพาะของคุณเป็นไปตามข้อกำหนดด้านการปฏิบัติตามข้อกำหนด
Swift
// ...
// Get the model's response
let text = response.text
// Get the grounding metadata
if let candidate = response.candidates.first,
let groundingMetadata = candidate.groundingMetadata {
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
if let renderedContent = groundingMetadata.searchEntryPoint?.renderedContent {
// TODO(developer): Display Google Search suggestions using a WebView
}
// RECOMMENDED - display citations
let groundingChunks = groundingMetadata.groundingChunks
for chunk in groundingMetadata.groundingChunks {
if let web = chunk.web {
let title = web.title // for example, "uefa.com"
let uri = web.uri // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show citation in the UI
}
}
}
Kotlin
// ...
// Get the model's response
val text = response.text
// Get the grounding metadata
val groundingMetadata = response.candidates.firstOrNull()?.groundingMetadata
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
val renderedContent = groundingMetadata?.searchEntryPoint?.renderedContent
if (renderedContent != null) {
// TODO(developer): Display Google Search suggestions using a WebView
}
// RECOMMENDED - display citations
val groundingChunks = groundingMetadata?.groundingChunks
groundingChunks?.let { chunks ->
for (chunk in chunks) {
val title = chunk.web?.title // for example, "uefa.com"
val uri = chunk.web?.uri // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show citation in the UI
}
}
Java
// ...
Futures.addCallback(response, new FutureCallback() {
@Override
public void onSuccess(GenerateContentResponse result) {
// Get the model's response
String text = result.getText();
// Get the grounding metadata
GroundingMetadata groundingMetadata =
result.getCandidates()[0].getGroundingMetadata();
if (groundingMetadata != null) {
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
String renderedContent =
groundingMetadata.getSearchEntryPoint().getRenderedContent();
if (renderedContent != null) {
// TODO(developer): Display Google Search suggestions using a WebView
}
// RECOMMENDED - display citations
List chunks = groundingMetadata.getGroundingChunks();
if (chunks != null) {
for(GroundingChunk chunk : chunks) {
WebGroundingChunk web = chunk.getWeb();
if (web != null) {
String title = web.getTitle(); // for example, "uefa.com"
String uri = web.getUri(); // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show citation in the UI
}
}
}
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
}, executor);
Web
// ...
// Get the model's text response
const text = result.response.text();
// Get the grounding metadata
const groundingMetadata = result.response.candidates?.[0]?.groundingMetadata;
// REQUIRED - display Google Search suggestions
// (renderedContent contains HTML and CSS for the search widget)
const renderedContent = groundingMetadata?.searchEntryPoint?.renderedContent;
if (renderedContent) {
// TODO(developer): render this HTML and CSS in the UI
}
// RECOMMENDED - display citations
const groundingChunks = groundingMetadata?.groundingChunks;
if (groundingChunks) {
for (const chunk of groundingChunks) {
const title = chunk.web?.title; // for example, "uefa.com"
const uri = chunk.web?.uri; // for example, "https://vertexaisearch.cloud.google.com..."
// TODO(developer): show citation in the UI
}
}
Dart
โดยจะรองรับ Flutter ในรุ่นถัดไป
Unity
โดยจะรองรับ Unity ในรุ่นถัดไป
ผลการค้นหาที่อิงตามข้อมูลและ AI Monitoring ในคอนโซล Firebase
หากเปิดใช้ การตรวจสอบ AI ในFirebaseคอนโซล ระบบจะจัดเก็บคำตอบใน Cloud Logging โดยค่าเริ่มต้น ข้อมูลนี้มีระยะเวลาเก็บรักษา 30 วัน
คุณมีหน้าที่รับผิดชอบในการตรวจสอบว่าระยะเวลาการเก็บรักษานี้หรือระยะเวลาที่กำหนดเองใดๆ ที่คุณตั้งค่าไว้สอดคล้องกับกรณีการใช้งานเฉพาะของคุณและข้อกำหนดด้านการปฏิบัติตามข้อกำหนดเพิ่มเติมสำหรับGemini APIผู้ให้บริการที่คุณเลือกอย่างครบถ้วน Gemini Developer API หรือ Vertex AI Gemini API (ดูส่วนข้อกำหนดในการให้บริการ ภายในข้อกำหนดเฉพาะของบริการ) คุณอาจต้องปรับระยะเวลาเก็บรักษาใน Cloud Logging เพื่อให้เป็นไปตามข้อกำหนดเหล่านี้
ราคาและขีดจำกัด
โปรดตรวจสอบราคา ความพร้อมใช้งานของโมเดล และขีดจำกัดสำหรับการกราวด์ด้วย Google Search ในเอกสารประกอบของผู้ให้บริการ Gemini API ที่คุณเลือก Gemini Developer API | Vertex AI Gemini API