Answer queries with grounded generation

Use the AnswerQuery method to get answers to queries that are grounded in the Developer Knowledge corpus.

Before you begin

Make sure you have enabled the API and generated a Developer Knowledge API key, and save your key to an environment variable:

export DEVELOPERKNOWLEDGE_API_KEY="YOUR_API_KEY"

Example usage

The following example asks "How do I create a BigQuery dataset?":

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?"}'

The response contains the text answer in the answer.answer_text field, along with citations and references in the answer object:

{
  "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"
            }
          }
        }
      }
    ]
  }
}