สร้างคำตอบจากเอกสารประกอบ

Developer Knowledge API ช่วยให้คุณถามคำถามเกี่ยวกับผลิตภัณฑ์สำหรับนักพัฒนาซอฟต์แวร์ของ Google และรับคำตอบโดยตรงในภาษาธรรมชาติได้ สำหรับการค้นหาแต่ละครั้ง API จะเขียนคำตอบที่ดึงมาจากคลังความรู้สำหรับนักพัฒนาซอฟต์แวร์ (เรียกว่าการสร้างที่อิงตามข้อมูลในเอกสารอ้างอิง API) และรวมการอ้างอิงไปยังหน้าเอกสารประกอบที่เกี่ยวข้อง

ก่อนเริ่มต้น

ตั้งค่าสภาพแวดล้อมสำหรับเครื่องมือที่ต้องการ ดังนี้

gcloud

ติดตั้งและกำหนดค่า gcloud CLI และเปิดใช้ Developer Knowledge API

REST

เปิดใช้ API และสร้างคีย์ Developer Knowledge API จากนั้นบันทึกคีย์ลงในตัวแปรสภาพแวดล้อม ดังนี้

export DEVELOPERKNOWLEDGE_API_KEY="YOUR_API_KEY"

แทนที่ YOUR_API_KEY ด้วยคีย์ Developer Knowledge API

ตอบคำถาม

ใช้คำสั่ง gcloud developer-knowledge answer-query หรือเมธอด REST answerQuery เพื่อถามคำถาม

ตัวอย่างต่อไปนี้จะส่งคำถามเกี่ยวกับวิธีสร้างชุดข้อมูล BigQuery

gcloud

gcloud developer-knowledge answer-query \
  --query="How do I create a BigQuery dataset?"

REST

curl -X POST "https://developerknowledge.googleapis.com/v1:answerQuery?key=$DEVELOPERKNOWLEDGE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"query": "How do I create a BigQuery dataset?"}'

การตอบกลับจะมีคำตอบแบบข้อความในช่อง answer.answerText พร้อมด้วย citations และ references ในออบเจ็กต์ answer ดังนี้

{
  "answer": {
    "answerText": "To create a BigQuery dataset, you can use the Google Cloud console, the bq command-line tool, or the BigQuery client libraries.",
    "citations": [
      {
        "startIndex": 0,
        "endIndex": 123,
        "sources": [
          {
            "referenceIndex": 0
          }
        ]
      }
    ],
    "references": [
      {
        "documentReference": {
          "documentChunk": {
            "parent": "documents/docs.cloud.google.com/bigquery/docs/datasets",
            "content": "This page explains how to create BigQuery datasets...",
            "document": {
              "name": "documents/docs.cloud.google.com/bigquery/docs/datasets",
              "title": "Introduction to datasets",
              "uri": "https://docs.cloud.google.com/bigquery/docs/datasets"
            }
          }
        }
      }
    ]
  }
}

กรองแหล่งที่มาของเอกสารประกอบ

หากต้องการจำกัดแหล่งที่มาของเอกสารที่ใช้สร้างคำตอบ ให้ส่งนิพจน์ตัวกรองโดยใช้แฟล็ก --query-filter ใน gcloud CLI หรือช่อง filter ในเนื้อหาคำขอ REST ดูรายละเอียดเกี่ยวกับช่องตัวกรองและโอเปอเรเตอร์ที่รองรับได้ที่หัวข้อกรองผลการค้นหา

ตัวอย่างต่อไปนี้จำกัดแหล่งที่มาของเอกสารไว้ที่ docs.cloud.google.com

gcloud

gcloud developer-knowledge answer-query \
  --query="How do I create a BigQuery dataset?" \
  --query-filter='data_source = "docs.cloud.google.com"'

REST

curl -X POST "https://developerknowledge.googleapis.com/v1:answerQuery?key=$DEVELOPERKNOWLEDGE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "query": "How do I create a BigQuery dataset?",
    "filter": "data_source = \"docs.cloud.google.com\""
  }'

เลือกระหว่าง AnswerQuery ถึง SearchDocumentChunks

เมื่อใช้ API หรือเซิร์ฟเวอร์ MCP ของ Developer Knowledge ให้เลือกวิธีที่เหมาะกับสิ่งที่คุณต้องการมากที่สุด ดังนี้

  • AnswerQuery (หรือเครื่องมือ answer_query MCP): เหมาะที่สุดสำหรับคำถามทั่วไปเกี่ยวกับ "วิธีการ" การเปรียบเทียบผลิตภัณฑ์ และคำแนะนำทีละขั้นตอนที่คุณต้องการสรุปที่สมบูรณ์พร้อมลิงก์ไปยังเอกสารแหล่งที่มา
  • SearchDocumentChunks (หรือเครื่องมือ MCP search_documents): เหมาะที่สุดสำหรับการค้นหาแฟล็ก CLI, ไวยากรณ์โค้ด, ชื่อพารามิเตอร์ หรือสิทธิ์ IAM ที่แน่นอน (เช่น service.resource.verb) โดยใช้คีย์เวิร์ดที่เจาะจง 2-5 คำแทนคำถามแบบเต็ม

แยกวิเคราะห์การอ้างอิงและแหล่งข้อมูลอ้างอิง

เมื่อคุณเรียกใช้ answerQuery ออบเจ็กต์ answer จะให้การระบุแหล่งที่มาแบบละเอียดซึ่งลิงก์ส่วนที่เฉพาะเจาะจงของ answerText ที่สร้างขึ้นกับข้อความต้นฉบับใน references

เอกสารต้นฉบับรายการเดียวใน references สามารถอ้างอิงได้หลายครั้งในส่วนข้อความต่างๆ และส่วนเดียวสามารถอ้างอิงแหล่งข้อมูลได้หลายรายการ

ตัวอย่างคำตอบพร้อมการอ้างอิง

ข้อมูลโค้ด JSON ต่อไปนี้แสดงออบเจ็กต์ answer ที่มีการอ้างอิงหลายรายการซึ่งชี้ไปยังข้อมูลอ้างอิงเอกสารที่แตกต่างกัน

{
  "answer": {
    "answerText": "You can create a BigQuery dataset using the Google Cloud console, the bq command-line tool, or the client libraries. Each dataset is assigned a region or multi-region location when created.",
    "citations": [
      {
        "startIndex": 0,
        "endIndex": 116,
        "sources": [
          {
            "referenceIndex": 0
          }
        ]
      },
      {
        "startIndex": 117,
        "endIndex": 189,
        "sources": [
          {
            "referenceIndex": 1
          }
        ]
      }
    ],
    "references": [
      {
        "documentReference": {
          "documentChunk": {
            "parent": "documents/docs.cloud.google.com/bigquery/docs/datasets",
            "content": "You can create a dataset in BigQuery using the Google Cloud console, the bq command-line tool, or client libraries...",
            "document": {
              "name": "documents/docs.cloud.google.com/bigquery/docs/datasets",
              "title": "Creating and managing datasets",
              "uri": "https://docs.cloud.google.com/bigquery/docs/datasets"
            }
          }
        }
      },
      {
        "documentReference": {
          "documentChunk": {
            "parent": "documents/docs.cloud.google.com/bigquery/docs/locations",
            "content": "When you create a dataset, you specify the location where your BigQuery data is stored...",
            "document": {
              "name": "documents/docs.cloud.google.com/bigquery/docs/locations",
              "title": "Dataset locations",
              "uri": "https://docs.cloud.google.com/bigquery/docs/locations"
            }
          }
        }
      }
    ]
  }
}

การแมปและการอ้างอิงฟิลด์

หากต้องการแมปการอ้างอิงกับแหล่งที่มาของเอกสารประกอบ ให้ใช้ช่องต่อไปนี้

  • answerText: สตริงคำตอบภาษาธรรมชาติที่สร้างขึ้นอย่างสมบูรณ์
  • citations: อาร์เรย์ของการระบุแหล่งที่มาของกลุ่มที่อ้างอิง
    • startIndex: ออฟเซ็ตเริ่มต้นแบบรวมของส่วนที่อ้างอิง โดยวัดเป็นไบต์ UTF-8
    • endIndex: ออฟเซ็ตสิ้นสุด (ไม่รวม) ของส่วนที่อ้างอิง โดยวัดเป็นไบต์ UTF-8
    • sources: อาร์เรย์ของแหล่งข้อมูลอ้างอิง แต่ละรายการจะมี referenceIndex แบบอิงตาม 0 ซึ่งสอดคล้องกับรายการในอาร์เรย์ references (เช่น referenceIndex: 0 จะแมปกับ references[0])
  • references: อาร์เรย์ของข้อความในเอกสารแหล่งที่มาที่ใช้สร้างคำตอบ แต่ละรายการจะมี documentReference.documentChunk พร้อมข้อมูลเมตาต่อไปนี้
    • document.title: ชื่อของหน้าเอกสารประกอบ
    • document.uri: URL สาธารณะของหน้าเอกสารประกอบ
    • document.name: ชื่อทรัพยากร Canonical ของเอกสาร
    • content: ตัวอย่างข้อความที่ดึงมาจากส่วนของเอกสารที่ใช้ในการสังเคราะห์คำตอบ
    • parent: ชื่อทรัพยากรของเอกสารหลัก (เช่น documents/docs.cloud.google.com/bigquery/docs/datasets) ซึ่งคุณสามารถส่งไปยัง documents.get หรือ documents.batchGet เพื่อดึงข้อมูลเอกสารฉบับเต็ม

ใช้การอ้างอิงในแอปพลิเคชัน

วิธีแสดงผลคำตอบที่มีการอ้างอิงในแอปพลิเคชันไคลเอ็นต์หรือไปป์ไลน์ AI Agent

  1. แยกส่วนที่อ้างอิง: หากต้องการแยกส่วนที่เกี่ยวข้องจาก answerText ให้ใช้ออฟเซ็ตไบต์ startIndex และ endIndex
  2. แก้ไขรายละเอียดแหล่งที่มา: หากต้องการดึงข้อมูลหน้า document.title และปลายทาง document.uri ให้ค้นหารายการที่เกี่ยวข้องใน references โดยใช้ sources[].referenceIndex
  3. แสดงลิงก์ในบรรทัดหรือเชิงอรรถ: หากต้องการลิงก์ไปยังหน้าแหล่งที่มาโดยตรง ให้ใส่หมายเลขเชิงอรรถ (เช่น [1] หรือ [2]) การ์ดตัวอย่าง หรือไฮเปอร์ลิงก์ที่คลิกได้ซึ่งชี้ไปยัง document.uri
  4. ดึงข้อมูลบริบททั้งหมดหากจำเป็น: หากต้องการดึงข้อมูลหน้าเอกสารประกอบที่สมบูรณ์เมื่อข้อความที่ตัดตอนมาใน content ไม่เพียงพอ ให้ส่ง parent ไปยัง documents.get
  • ภาษา UTF-16 (JavaScript หรือ TypeScript): แปลงสตริงเป็นไบต์ UTF-8 ก่อนแบ่งส่วน เช่น ใช้ Buffer.from(answerText, 'utf-8').subarray(start, end) ใน Node.js หรือ TextEncoder และ TextDecoder ในสภาพแวดล้อมของเบราว์เซอร์
  • Python: เข้ารหัสสตริงเป็นไบต์ UTF-8 ก่อนแบ่งส่วน แล้วถอดรหัส: answer_text.encode('utf-8')[start:end].decode('utf-8')
  • ภาษาอื่นๆ: สำหรับภาษาที่จัดทำดัชนีสตริงตามไบต์ UTF-8 โดยค่าเริ่มต้น (เช่น Go หรือ Rust) ให้แบ่ง answerText โดยตรงโดยใช้ startIndex และ endIndex ไม่เช่นนั้น ให้แปลงสตริงเป็นไบต์ UTF-8 ก่อนแบ่งส่วน

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