ตอบคำค้นหาด้วยการสร้างที่อิงตามข้อมูล

ใช้เมธอด AnswerQuery เพื่อรับคำตอบสำหรับคำค้นหาที่อิงตาม คลังข้อมูลความรู้สำหรับนักพัฒนาแอป

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

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

export DEVELOPERKNOWLEDGE_API_KEY="YOUR_API_KEY"

ตัวอย่างการใช้

ตัวอย่างต่อไปนี้จะถามว่า "ฉันจะสร้างชุดข้อมูล BigQuery ได้อย่างไร"

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

คำตอบจะมีคำตอบเป็นข้อความในช่อง answer.answer_text พร้อมด้วย 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/cloud.google.com/bigquery/docs/datasets",
            "content": "This page explains how to create BigQuery datasets...",
            "document": {
              "name": "documents/cloud.google.com/bigquery/docs/datasets",
              "title": "Introduction to datasets",
              "uri": "https://cloud.google.com/bigquery/docs/datasets"
            }
          }
        }
      }
    ]
  }
}