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

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

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

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

Python

from google import genai

client = genai.Client()

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

print(interaction.output_text)

JavaScript

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

const client = new GoogleGenAI({});

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

console.log(interaction.output_text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Api-Revision: 2026-05-20" \
  -d '{
    "model": "gemini-3.5-flash",
    "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 เดียวกัน) ระบบจะนับเป็นการใช้เครื่องมือที่เรียกเก็บเงินได้ 2 ครั้ง สำหรับคำขอนั้น ระบบจะไม่นับคำค้นหาเว็บที่ว่างเปล่าเมื่อนับคำค้นหาที่ไม่ซ้ำกันเพื่อวัตถุประสงค์ในการเรียกเก็บเงิน โมเดลการเรียกเก็บเงินนี้ใช้ได้กับโมเดล Gemini 3 เท่านั้น เมื่อคุณใช้การเชื่อมต่อแหล่งข้อมูลกับ Search ร่วมกับโมเดล Gemini 2.5 หรือโมเดลเก่ากว่า ระบบจะเรียกเก็บเงินจากโปรเจ็กต์ของคุณต่อพรอมต์

ดูข้อมูลการกำหนดราคารายละเอียดได้ที่หน้าการกำหนดราคา Gemini API

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

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

โมเดล การเชื่อมต่อแหล่งข้อมูลกับ Google Search
Gemini 3.5 Flash ✔️
Gemini 3.1 Flash Image Preview ✔️
Gemini 3.1 Pro Preview ✔️
Gemini 3 Pro Image Preview ✔️
Gemini 3 Flash Preview ✔️
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) กับเครื่องมือที่กำหนดเอง (การเรียกใช้ฟังก์ชัน) ดูข้อมูลเพิ่มเติมได้ในหน้า ชุดค่าผสมของเครื่องมือ

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

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