使用 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"
}
}
}
}
]
}
}