การเชื่อมต่อแหล่งข้อมูลกับ Google Search

การเชื่อมต่อแหล่งข้อมูลกับ Google Search จะเชื่อมต่อโมเดล Gemini กับเนื้อหาเว็บแบบเรียลไทม์ และใช้งานได้กับทุกภาษาที่มี ซึ่งจะช่วยให้ Gemini สามารถให้คำตอบที่แม่นยำยิ่งขึ้นและอ้างอิงแหล่งข้อมูลที่ตรวจสอบได้นอกเหนือจากการตัดข้อมูล

การเชื่อมต่อแหล่งข้อมูลช่วยให้คุณสร้างแอปพลิเคชันที่ทำสิ่งต่อไปนี้ได้

  • เพิ่มความแม่นยำของข้อเท็จจริง: ลดอาการหลอนของโมเดลโดยอิงคำตอบตามข้อมูลในโลกแห่งความเป็นจริง
  • เข้าถึงข้อมูลแบบเรียลไทม์: ตอบคำถามเกี่ยวกับเหตุการณ์และหัวข้อล่าสุด
  • อ้างอิงแหล่งที่มา: สร้างความไว้วางใจของผู้ใช้โดยแสดงแหล่งที่มาของคำกล่าวอ้างของโมเดล

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    model="gemini-3-flash-preview",
    input="Who won the euro 2024?",
    tools=[{"type": "google_search"}]
)

# Print the model's text response
for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)

JavaScript

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

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    model: "gemini-3-flash-preview",
    input: "Who won the euro 2024?",
    tools: [{ type: "google_search" }]
});

const modelStep = interaction.steps.find(s => s.type === 'model_output');
if (modelStep) {
  for (const contentBlock of modelStep.content) {
    if (contentBlock.type === 'text') console.log(contentBlock.text);
  }
}

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3-flash-preview",
    "input": "Who won the euro 2024?",
    "tools": [{"type": "google_search"}]
  }'

วิธีการทำงานของการเชื่อมต่อแหล่งข้อมูลกับ Google Search

เมื่อเปิดใช้google_searchเครื่องมือ โมเดลจะจัดการเวิร์กโฟลว์ทั้งหมด ของการค้นหา ประมวลผล และอ้างอิงข้อมูลโดยอัตโนมัติ

grounding-overview

  1. พรอมต์ของผู้ใช้: แอปพลิเคชันของคุณจะส่งพรอมต์ของผู้ใช้ไปยัง Gemini API โดยเปิดใช้เครื่องมือ google_search
  2. การวิเคราะห์พรอมต์: โมเดลจะวิเคราะห์พรอมต์และพิจารณาว่า Google Search สามารถปรับปรุงคำตอบได้หรือไม่
  3. Google Search: หากจำเป็น โมเดลจะสร้างคำค้นหาอย่างน้อย 1 รายการโดยอัตโนมัติและดำเนินการค้นหา
  4. การประมวลผลผลการค้นหา: โมเดลจะประมวลผลผลการค้นหา สังเคราะห์ข้อมูล และสร้างคำตอบ
  5. คำตอบที่อิงตามข้อมูล: API จะแสดงคำตอบสุดท้ายที่ใช้งานง่ายซึ่ง อิงตามผลการค้นหา การตอบกลับนี้ประกอบด้วยคำตอบที่เป็นข้อความของโมเดล พร้อมด้วย annotations แบบอินไลน์ที่มีการอ้างอิง รวมถึงขั้นตอน google_search_call และ google_search_result พร้อมด้วยคำค้นหาและคำแนะนำในการค้นหา

ทำความเข้าใจคำตอบที่อิงตามข้อมูล

เมื่ออ้างอิงคำตอบได้สำเร็จ ข้อความเอาต์พุตของโมเดลจะมีannotationsในบรรทัดโดยตรงในบล็อกเนื้อหาข้อความ คำอธิบายประกอบเหล่านี้ ให้ข้อมูลการอ้างอิงที่ลิงก์ส่วนต่างๆ ของคำตอบไปยังแหล่งที่มา

{
  "steps": [
    {
      "type": "thought",
      "summary": [
        {
          "type": "text",
          "text": "The user is asking for the winner of Euro 2024. I need to search for the result of the Euro 2024 final."
        }
      ],
      "signature": "CoMDAXLI2nynRYojJIy6B1Jh9os2crpWLfB0..."
    },
    {
      "type": "google_search_call",
      "arguments": {
        "queries": ["UEFA Euro 2024 winner"]
      }
    },
    {
      "type": "google_search_result",
      "call_id": "search_001",
      "result": [
        {
          "search_suggestions": "<!-- HTML and CSS for the search widget -->"
        }
      ]
    },
    {
      "type": "model_output",
      "content": [
        {
          "type": "text",
          "text": "Spain won Euro 2024, defeating England 2-1 in the final. This victory marks Spain's record fourth European Championship title.",
          "annotations": [
            {
              "type": "url_citation",
              "url": "https://www.aljazeera.com/sports/euro-2024-final",
              "title": "aljazeera.com",
              "start_index": 0,
              "end_index": 56
            },
            {
              "type": "url_citation",
              "url": "https://www.uefa.com/euro2024/news/spain-wins-euro-2024",
              "title": "uefa.com",
              "start_index": 57,
              "end_index": 124
            }
          ]
        }
      ]
    }
  ]
}

ฟิลด์คีย์ในการตอบกลับ

  • google_search_call : มีการค้นหา queries ที่โมเดลดำเนินการ
  • google_search_result : มี search_suggestions ซึ่งเป็นข้อมูลโค้ด HTML สำหรับการแสดงผลคำแนะนำในการค้นหาใน UI ข้อกำหนดในการใช้งานฉบับเต็มมีรายละเอียดอยู่ในข้อกำหนดในการให้บริการ
  • text พร้อม annotations : คำตอบที่โมเดลสังเคราะห์พร้อมการอ้างอิงในบรรทัด url_citationแต่ละรายการจะลิงก์กลุ่มข้อความ (กำหนดโดย start_index และ end_index) ไปยัง URL แหล่งที่มา ซึ่งเป็นกุญแจสำคัญในการ สร้างการอ้างอิงในบรรทัด

นอกจากนี้ คุณยังใช้การเชื่อมต่อแหล่งข้อมูลกับ Google Search ร่วมกับเครื่องมือบริบท URL เพื่ออ้างอิงคำตอบในทั้งข้อมูลเว็บสาธารณะและ URL ที่เฉพาะเจาะจงที่คุณระบุได้ด้วย

การระบุแหล่งที่มาด้วยการอ้างอิงในบรรทัด

API จะแสดงคำอธิบายประกอบ url_citation ในบรรทัดในบล็อกเนื้อหาข้อความ เพื่อให้คุณควบคุมวิธีแสดงแหล่งที่มาในอินเทอร์เฟซผู้ใช้ได้อย่างสมบูรณ์ โดยคำอธิบายประกอบแต่ละรายการจะมี start_index และ end_index เพื่อระบุส่วนของข้อความที่อ้างอิง วิธีแยกและแสดงข้อมูลมีดังนี้

Python

for step in interaction.steps:
    if step.type == "model_output":
        for content_block in step.content:
            if content_block.type == "text":
                print(content_block.text)
                if content_block.annotations:
                    print("\nCitations:")
                    for annotation in content_block.annotations:
                        if annotation.type == "url_citation":
                            cited_text = content_block.text[annotation.start_index:annotation.end_index]
                            print(f"  [{annotation.title}]({annotation.url})")
                            print(f"    Cited text: \"{cited_text}\"")

JavaScript

for (const step of interaction.steps) {
  if (step.type === 'model_output') {
    for (const contentBlock of step.content) {
      if (contentBlock.type === 'text') {
        console.log(contentBlock.text);
        if (contentBlock.annotations) {
          console.log("\nCitations:");
          for (const annotation of contentBlock.annotations) {
            if (annotation.type === 'url_citation') {
              const citedText = contentBlock.text.slice(annotation.startIndex, annotation.endIndex);
              console.log(`  [${annotation.title}](${annotation.url})`);
              console.log(`    Cited text: "${citedText}"`);
            }
          }
        }
      }
    }
  }
}

เอาต์พุตจะแสดงข้อความตามด้วยการอ้างอิง ดังนี้

Spain won Euro 2024, defeating England 2-1 in the final. This victory marks Spain's record fourth European Championship title.

Citations:
  [aljazeera.com](https://www.aljazeera.com/sports/euro-2024-final)
    Cited text: "Spain won Euro 2024, defeating England 2-1 in the final."
  [uefa.com](https://www.uefa.com/euro2024/news/spain-wins-euro-2024)
    Cited text: "This victory marks Spain's record fourth European Championship title."

ราคา

เมื่อใช้การเชื่อมต่อแหล่งข้อมูลกับ Google Search ด้วย Gemini 3 ระบบจะเรียกเก็บเงินจากโปรเจ็กต์ของคุณ สำหรับคำค้นหาแต่ละคำที่โมเดลตัดสินใจดำเนินการ หากโมเดลตัดสินใจที่จะเรียกใช้คำค้นหาหลายรายการเพื่อตอบพรอมต์เดียว (เช่น ค้นหา "UEFA Euro 2024 winner" และ "Spain vs England Euro 2024 final score" ภายใน API Call เดียวกัน) ระบบจะนับเป็นการใช้เครื่องมือที่เรียกเก็บเงินได้ 2 ครั้งสำหรับคำขอนั้น เพื่อวัตถุประสงค์ในการเรียกเก็บเงิน เราจะไม่สนใจคำค้นหาบนเว็บที่ว่างเปล่า เมื่อนับคำค้นหาที่ไม่ซ้ำกัน โมเดลการเรียกเก็บเงินนี้ใช้กับโมเดล Gemini 3 เท่านั้น เมื่อคุณใช้การเชื่อมต่อแหล่งข้อมูลการค้นหากับโมเดล Gemini 2.5 หรือรุ่นเก่ากว่า ระบบจะเรียกเก็บเงินโปรเจ็กต์ต่อพรอมต์

ดูข้อมูลราคาโดยละเอียดได้ที่หน้าเว็บราคาของ Gemini API

โมเดลที่รองรับ

คุณดูความสามารถทั้งหมดได้ในหน้าภาพรวมโมเดล

รุ่น การเชื่อมต่อแหล่งข้อมูลกับ Google Search
เวอร์ชันตัวอย่างของรูปภาพ Gemini 3.1 Flash ✔️
Gemini 3.1 Pro (เวอร์ชันตัวอย่าง) ✔️
ตัวอย่างรูปภาพ Gemini 3 Pro ✔️
Gemini 3 Flash (เวอร์ชันตัวอย่าง) ✔️
Gemini 2.5 Pro ✔️
Gemini 2.5 Flash ✔️
Gemini 2.5 Flash-Lite ✔️
Gemini 2.0 Flash ✔️

ชุดเครื่องมือที่รองรับ

คุณสามารถใช้การเชื่อมต่อแหล่งข้อมูลกับ Google Search ร่วมกับเครื่องมืออื่นๆ เช่น การเรียกใช้โค้ดและ บริบท URL เพื่อขับเคลื่อน Use Case ที่ซับซ้อนยิ่งขึ้น

โมเดล Gemini 3 รองรับการรวมเครื่องมือในตัว (เช่น การอ้างอิงข้อมูลด้วย Google Search) กับเครื่องมือที่กำหนดเอง (การเรียกใช้ฟังก์ชัน) ดูข้อมูลเพิ่มเติมได้ที่หน้าชุดเครื่องมือ

ขั้นตอนถัดไป

  • ดูข้อมูลเกี่ยวกับเครื่องมืออื่นๆ ที่พร้อมใช้งาน เช่น Function Calling
  • ดูวิธีเพิ่มพรอมต์ด้วย URL ที่เฉพาะเจาะจงโดยใช้เครื่องมือบริบท URL