借助 Developer Knowledge API,您可以提出有关 Google 开发者产品的问题,并直接收到自然语言回答。对于每个查询,该 API 都会从 开发者知识语料库(在 API 参考文档中称为“基于事实的生成”)中生成 回答,并引用相关 文档页面。
准备工作
为首选工具设置环境:
gcloud
REST
启用该 API 并生成 Developer Knowledge API 密钥。 然后,将密钥保存到环境变量:
export DEVELOPERKNOWLEDGE_API_KEY="YOUR_API_KEY"
将 YOUR_API_KEY 替换为您的 Developer Knowledge API 密钥。
回答查询
使用
gcloud developer-knowledge answer-query命令或
answerQueryREST 方法
提出问题。
以下示例发送了一个查询,询问如何创建 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"
}
}
}
}
]
}
}
过滤文档来源
如需限制用于生成回答的文档来源,请传递过滤
表达式,在 gcloud CLI 中使用 --query-filter 标志或在 REST 请求正文中使用
filter 字段。如需详细了解支持的过滤字段
和运算符,请参阅
过滤搜索结果。
以下示例将文档来源限制为 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 或 Developer Knowledge MCP 服务器时,请选择最符合您需求的方法:
AnswerQuery(或answer_queryMCP 工具) :最适合一般 HowTo 问题、产品比较和分步指南,您可以在其中获得完整的摘要以及指向源文档的链接。SearchDocumentChunks(或search_documentsMCP 工具) :最适合查找确切的 CLI 标志、代码语法、形参名称或 IAM 权限(例如service.resource.verb)。请使用两到五个特定关键字,而不是完整的问题。
后续步骤
- 搜索和检索文档,以提取 匹配的文本片段或完整的 Markdown 页面。
- 连接到 Developer Knowledge MCP 服务器 并安装
retrieving-developer-knowledge代理技能。 - 浏览语料库参考文档,查看 所有受支持的文档来源。