Trả lời các câu hỏi bằng tính năng tạo thông tin có căn cứ

Sử dụng phương thức AnswerQuery để nhận câu trả lời cho các truy vấn dựa trên kho dữ liệu Kiến thức dành cho nhà phát triển.

Trước khi bắt đầu

Đảm bảo bạn đã bật API và tạo khoá API Kiến thức dành cho nhà phát triển, đồng thời lưu khoá vào một biến môi trường:

export DEVELOPERKNOWLEDGE_API_KEY="YOUR_API_KEY"

Ví dụ về cách sử dụng

Ví dụ sau đây hỏi "Làm cách nào để tạo một tập dữ liệu 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?"}'

Phản hồi chứa câu trả lời bằng văn bản trong trường answer.answer_text, cùng với citationsreferences trong đối tượng 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"
            }
          }
        }
      }
    ]
  }
}