你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
从 QNA Maker 知识库获取答案
注意
QnA Maker 服务将于 2025 年 3 月 31 日停用。 问答功能的较新版本现已作为 Azure AI 语言的一部分提供。 有关语言服务中的问答功能,请参阅问答。 从 2022 年 10 月 1 日开始,你将无法创建新的 QnA Maker 资源。 有关将现有 QnA Maker 知识库迁移到问题解答的信息,请参阅迁移指南。
注意
本文档不适用于最新版本。 若要了解如何使用最新的问题解答 API,请参阅问题解答创作指南。
此基于 cURL 的快速入门详细介绍如何从知识库获取答案。
先决条件
- 在 Azure 门户中创建的 QnA Maker 资源。 请记住你在创建资源时选择的 Microsoft Entra ID、订阅、QnA 资源名称。
- 在前一篇快速入门中生成的已训练且已发布的知识库,包含问题和答案,并且已配置元数据和闲聊内容。
使用元数据筛选答案
使用上一篇快速入门中的知识库基于元数据查询答案。
在知识库的“设置”页中,选择“CURL”选项卡查看用于从知识库生成答案的示例 cURL 命令。
将该命令复制到可编辑环境(例如文本文件),以便可以编辑该命令。 按如下所示编辑问题,以便将
service:qna_maker
的元数据用作 QnA 对的筛选器。curl -X POST https://replace-with-your-resource-name.azurewebsites.net/qnamaker/knowledgebases/replace-with-your-knowledge-base-id/generateAnswer -H "Authorization: EndpointKey replace-with-your-endpoint-key" -H "Content-type: application/json" -d "{'top':30, 'question':'size','strictFilters': [{'name':'service','value':'qna_maker'}]}"
该问题只是一个单词
size
,可以返回两个 QnA 对中的一个。strictFilters
数组告知响应将缩减为仅包含qna_maker
回答。该响应仅包含符合筛选条件的回答。 为便于阅读,以下 cURL 响应已设置格式:
{ "answers": [ { "questions": [ "How large a knowledge base can I create?", "What is the max size of a knowledge base?", "How many GB of data can a knowledge base hold?" ], "answer": "The size of the knowledge base depends on the SKU of Azure search you choose when creating the QnA Maker service. Read [here](../concepts/azure-resources.md) for more details.", "score": 68.76, "id": 3, "source": "https://learn.microsoft.com/azure/ai-services/qnamaker/troubleshooting", "metadata": [ { "name": "link_in_answer", "value": "true" }, { "name": "service", "value": "qna_maker" } ], "context": { "isContextOnly": false, "prompts": [] } } ], "debugInfo": null }
如果某对问答不符合搜索词但符合筛选器,将不会返回该对问答。 而是返回常规回答
No good match found in KB.
。
使用调试查询属性
注意
建议不要将 Debug 属性用于任何依赖项。 添加此属性是为了帮助产品团队进行故障排除。
调试信息可帮助你了解返回的答案是如何确定的。 尽管此信息非常有用,但不是必需的。 若要生成附带调试信息的答案,请添加 debug
属性:
Debug: {Enable:true}
编辑 cURL 命令,以包含调试属性来查看更多信息。
curl -X POST https://replace-with-your-resource-name.azurewebsites.net/qnamaker/knowledgebases/replace-with-your-knowledge-base-id/generateAnswer -H "Authorization: EndpointKey replace-with-your-endpoint-key" -H "Content-type: application/json" -d "{'question':'size', 'Debug':{'Enable':true}}"
响应包括有关答案的相关信息。 在以下 JSON 输出中,为简洁起见,某些调试详细信息已替换为省略号。
{ "answers": [ { "questions": [ "How do I share a knowledge base with others?" ], "answer": "Sharing works at the level of a QnA Maker service, that is, all knowledge bases in the service will be shared.", "score": 56.07, "id": 5, "source": "https://learn.microsoft.com/azure/ai-services/qnamaker/troubleshooting", "metadata": [], "context": { "isContextOnly": false, "prompts": [] } } ], "debugInfo": { "userQuery": { "question": "How do I programmatically update my Knowledge Base?", "top": 1, "userId": null, "strictFilters": [], "isTest": false, "debug": { "enable": true, "recordL1SearchLatency": false, "mockQnaL1Content": null }, "rankerType": 0, "context": null, "qnaId": 0, "scoreThreshold": 0.0 }, "rankerInfo": { "specialFuzzyQuery": "how do i programmatically~6 update my knowledge base", "synonyms": "what s...", "rankerLanguage": "English", "rankerFileName": "https://qnamakerstore.blob.core.windows.net/qnamakerdata/rankers/ranker-English.ini", "rankersDirectory": "D:\\home\\site\\wwwroot\\Data\\QnAMaker\\rd0003ffa60fc45.24.0\\RankerData\\Rankers", "allQnAsfeatureValues": { "WordnetSimilarity": { "5": 0.54706300120043716,... }, ... }, "rankerVersion": "V2", "rankerModelType": "TreeEnsemble", "rankerType": 0, "indexResultsCount": 25, "reRankerResultsCount": 1 }, "runtimeVersion": "5.24.0", "indexDebugInfo": { "indexDefinition": { "name": "064a4112-bd65-42e8-b01d-141c4c9cd09e", "fields": [... ], "scoringProfiles": [], "defaultScoringProfile": null, "corsOptions": null, "suggesters": [], "analyzers": [], "tokenizers": [], "tokenFilters": [], "charFilters": [], "@odata.etag": "\"0x8D7A920EA5EE6FE\"" }, "qnaCount": 117, "parameters": {}, "azureSearchResult": { "continuationToken": null, "@odata.count": null, "@search.coverage": null, "@search.facets": null, "@search.nextPageParameters": null, "value": [...], "@odata.nextLink": null } }, "l1SearchLatencyInMs": 0, "qnaL1Results": {...} }, "activeLearningEnabled": true }
使用测试知识库
若要从测试知识库获取答案,请使用 isTest
正文属性。
此属性是一个布尔值。
isTest:true
cURL 命令如下所示:
curl -X POST https://replace-with-your-resource-name.azurewebsites.net/qnamaker/knowledgebases/replace-with-your-knowledge-base-id/generateAnswer -H "Authorization: EndpointKey replace-with-your-endpoint-key" -H "Content-type: application/json" -d "{'question':'size', 'IsTest':true}"
JSON 响应使用与已发布的知识库查询相同的架构。
注意
即使测试知识库和已发布的知识库看上去完全相同,也仍可能存在一些细微的差异,因为测试索引在资源中的所有知识库之间共享。
使用 cURL 查询聊天式解答
在支持 cURL 的终端中,使用用户提供的机器人聊天结束语句,例如
Thank you
。 无需设置任何其他属性。curl -X POST https://replace-with-your-resource-name.azurewebsites.net/qnamaker/knowledgebases/replace-with-your-knowledge-base-id/generateAnswer -H "Authorization: EndpointKey replace-with-your-endpoint-key" -H "Content-type: application/json" -d "{'question':'thank you'}"
运行 cURL 命令,然后接收包含评分和解答的 JSON 响应。
{ "answers": [ { "questions": [ "I thank you", "Oh, thank you", "My sincere thanks", "My humblest thanks to you", "Marvelous, thanks", "Marvelous, thank you kindly", "Marvelous, thank you", "Many thanks to you", "Many thanks", "Kthx", "I'm grateful, thanks", "Ahh, thanks", "I'm grateful for that, thank you", "Perfecto, thanks", "I appreciate you", "I appreciate that", "I appreciate it", "I am very thankful for that", "How kind, thank you", "Great, thanks", "Great, thank you", "Gracias", "Gotcha, thanks", "Gotcha, thank you", "Awesome thanks!", "I'm grateful for that, thank you kindly", "thank you pal", "Wonderful, thank you!", "Wonderful, thank you very much", "Why thank you", "Thx", "Thnx", "That's very kind", "That's great, thanks", "That is lovely, thanks", "That is awesome, thanks!", "Thanks bot", "Thanks a lot", "Okay, thanks!", "Thank you so much", "Perfect, thanks", "Thank you my friend", "Thank you kindly", "Thank you for that", "Thank you bot", "Thank you", "Right on, thanks very much", "Right on, thanks a lot", "Radical, thanks", "Rad, thanks", "Rad thank you", "Wonderful, thanks!", "Thanks" ], "answer": "You're welcome.", "score": 100.0, "id": 75, "source": "qna_chitchat_Professional.tsv", "metadata": [ { "name": "editorial", "value": "chitchat" } ], "context": { "isContextOnly": false, "prompts": [] } } ], "debugInfo": null, "activeLearningEnabled": true }
由于问题
Thank you
与聊天问题完全匹配,因此分数为 100,表明 QnA Maker 完全确信其答案。 QnA Maker 还返回了包含聊天元数据标记信息的所有相关问题和元数据属性。
使用阈值和默认答案
可以请求答案的最小阈值。 如果不满足阈值,将返回默认答案。
添加
threshold
属性,以请求提供阈值为 80% 或更佳的size
答案。 知识库应该不会查找该答案,因为问题的评分为 71%。 结果将返回在创建知识库时提供的默认答案。curl -X POST https://replace-with-your-resource-name.azurewebsites.net/qnamaker/knowledgebases/replace-with-your-knowledge-base-id/generateAnswer -H "Authorization: EndpointKey replace-with-your-endpoint-key" -H "Content-type: application/json" -d "{'question':'size', 'scoreThreshold':80.00}"
运行 cURL 命令并接收 JSON 响应。
{ "answers": [ { "questions": [], "answer": "No good match found in KB.", "score": 0.0, "id": -1, "source": null, "metadata": [] } ], "debugInfo": null, "activeLearningEnabled": true }
QnA Maker 返回的评分为
0
,表示没有置信度。 它同时返回了默认解答。将阈值更改为 60%,并再次请求查询:
curl -X POST https://replace-with-your-resource-name.azurewebsites.net/qnamaker/knowledgebases/replace-with-your-knowledge-base-id/generateAnswer -H "Authorization: EndpointKey replace-with-your-endpoint-key" -H "Content-type: application/json" -d "{'question':'size', 'scoreThreshold':60.00}"
返回的 JSON 找到了答案。
{ "answers": [ { "questions": [ "How large a knowledge base can I create?", "What is the max size of a knowledge base?", "How many GB of data can a knowledge base hold?" ], "answer": "The size of the knowledge base depends on the SKU of Azure search you choose when creating the QnA Maker service. Read [here](../concepts/azure-resources.md) for more details.", "score": 71.1, "id": 3, "source": "https://learn.microsoft.com/azure/ai-services/qnamaker/troubleshooting", "metadata": [ { "name": "link_in_answer", "value": "true" }, { "name": "server", "value": "qna_maker" } ], "context": { "isContextOnly": false, "prompts": [] } } ], "debugInfo": null, "activeLearningEnabled": true }
使用非结构化数据源
我们现支持添加不能用于提取 QnA 的非结构化文档。 提取对查询的响应时,用户可选择在 GenerateAnswer API 中包含或排除非结构化数据集。 正式发布版服务中不支持非结构化数据集,这仅包含在自定义问答中。
这篇基于 Visual Studio Code REST 客户端扩展的快速入门将指导你从知识库获取答案。
先决条件
- 必须具有:
- 最新的 Visual Studio Code REST 客户端扩展。
- 如果没有 Azure 订阅,请在开始之前创建一个免费帐户。
- 在 Azure 门户中创建的 QnA Maker 资源。 请记住你在创建资源时选择的 Microsoft Entra ID、订阅、QnA 资源名称。
- 在前一篇快速入门中生成的已训练且已发布的知识库,包含问题和答案,并且已配置元数据和闲聊内容。
为请求设置 Visual Studio Code REST 客户端扩展
本快速入门对 Visual Studio Code REST 客户端扩展 POST 请求使用相同的设置,并根据你尝试查询的内容,对发送到服务的 POST 正文 JSON 进行配置。
使用此过程配置 Visual Studio Code REST 客户端扩展,然后读取每个后续部分以配置 POST 正文 JSON。
在知识库的“设置”页中选择“Postman”选项卡,以查看用于从知识库生成答案的配置。 复制以下信息以在 Visual Studio Code REST 客户端扩展中使用。
名称 设置 用途和值 POST
/knowledgebases/replace-with-your-knowledge-base-id/generateAnswer
这是 URL 的 HTTP 方法和路由。 Host
https://YOUR-RESOURCE_NAME.azurewebsites.net/qnamaker
这是 URL 的宿主。 将 Host 和 Post 值串联在一起可以获取完整的 generateAnswer URL。 Authorization
EndpointKey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
用于授权向 Azure 发出请求的标头值。 Content-type
application/json
内容的标头值。 {"question":"<Your question>"}
POST 请求的正文,采用 JSON 对象形式。 在后续的每个部分,此值将会根据查询的目的发生更改。 重要
完成后,请记住将密钥从代码中删除,并且永远不要公开发布该密钥。 对于生产来说,请使用安全的方式存储和访问凭据,例如 Azure Key Vault。 有关详细信息,请参阅 Azure AI 服务安全性一文。
使用 Visual Studio Code REST 客户端扩展,创建带有已发布的知识库设置的新的基本 POST 请求。 在以下部分,请改动 POST 正文 JSON,以更改知识库查询。
使用元数据筛选答案
在前一篇快速入门中,已将元数据添加到两个 QnA 对,以区分两个不同的问题。 将元数据添加到查询,以将筛选器限制为仅返回相关的 QnA 对。
在 Visual Studio Code REST 客户端扩展中,通过添加带有
service:qna_maker
的名称/值对的strictFilters
属性来仅更改查询 JSON。 正文 JSON 应是:{ 'question':'size', 'strictFilters': [ { 'name':'service','value':'qna_maker' } ] }
问题只有一个单词
size
,这可能会返回两对问答中的任意一对。strictFilters
数组告知响应将缩减为仅包含qna_maker
回答。该响应仅包含符合筛选条件的回答。
为便于阅读,以下响应带有格式:
{ "answers": [ { "questions": [ "How large a knowledge base can I create?", "What is the max size of a knowledge base?", "How many GB of data can a knowledge base hold?" ], "answer": "The size of the knowledge base depends on the SKU of Azure search you choose when creating the QnA Maker service. Read [here](../concepts/azure-resources.md) for more details.", "score": 68.76, "id": 3, "source": "https://learn.microsoft.com/azure/ai-services/qnamaker/troubleshooting", "metadata": [ { "name": "link_in_answer", "value": "true" }, { "name": "service", "value": "qna_maker" } ], "context": { "isContextOnly": false, "prompts": [] } } ], "debugInfo": null }
如果某对问答不符合搜索词但符合筛选器,将不会返回该对问答。 而是返回常规回答
No good match found in KB.
。
使用调试查询属性
注意
建议不要将 Debug 属性用于任何依赖项。 添加此属性是为了帮助产品团队进行故障排除。
调试信息可帮助你了解返回的答案是如何确定的。 尽管此信息非常有用,但不是必需的。 若要生成附带调试信息的答案,请添加 debug
属性:
在 Visual Studio Code REST 客户端扩展中,通过添加
debug
属性仅更改正文 JSON。 JSON 应是:{ 'question':'size', 'Debug': { 'Enable':true } }
响应包括有关答案的相关信息。 在以下 JSON 输出中,某些调试详细信息已替换为省略号。
{ "answers": [ { "questions": [ "How do I share a knowledge base with others?" ], "answer": "Sharing works at the level of a QnA Maker service, that is, all knowledge bases in the service will be shared.", "score": 56.07, "id": 5, "source": "https://learn.microsoft.com/azure/ai-services/qnamaker/troubleshooting", "metadata": [], "context": { "isContextOnly": false, "prompts": [] } } ], "debugInfo": { "userQuery": { "question": "How do I programmatically update my Knowledge Base?", "top": 1, "userId": null, "strictFilters": [], "isTest": false, "debug": { "enable": true, "recordL1SearchLatency": false, "mockQnaL1Content": null }, "rankerType": 0, "context": null, "qnaId": 0, "scoreThreshold": 0.0 }, "rankerInfo": { "specialFuzzyQuery": "how do i programmatically~6 update my knowledge base", "synonyms": "what s...", "rankerLanguage": "English", "rankerFileName": "https://qnamakerstore.blob.core.windows.net/qnamakerdata/rankers/ranker-English.ini", "rankersDirectory": "D:\\home\\site\\wwwroot\\Data\\QnAMaker\\rd0003ffa60fc45.24.0\\RankerData\\Rankers", "allQnAsfeatureValues": { "WordnetSimilarity": { "5": 0.54706300120043716,... }, ... }, "rankerVersion": "V2", "rankerModelType": "TreeEnsemble", "rankerType": 0, "indexResultsCount": 25, "reRankerResultsCount": 1 }, "runtimeVersion": "5.24.0", "indexDebugInfo": { "indexDefinition": { "name": "064a4112-bd65-42e8-b01d-141c4c9cd09e", "fields": [... ], "scoringProfiles": [], "defaultScoringProfile": null, "corsOptions": null, "suggesters": [], "analyzers": [], "tokenizers": [], "tokenFilters": [], "charFilters": [], "@odata.etag": "\"0x8D7A920EA5EE6FE\"" }, "qnaCount": 117, "parameters": {}, "azureSearchResult": { "continuationToken": null, "@odata.count": null, "@search.coverage": null, "@search.facets": null, "@search.nextPageParameters": null, "value": [...], "@odata.nextLink": null } }, "l1SearchLatencyInMs": 0, "qnaL1Results": {...} }, "activeLearningEnabled": true }
使用测试知识库
若要从测试知识库获取答案,请使用 isTest
正文属性。
在 Visual Studio Code REST 客户端扩展中,通过添加 isTest
属性仅更改正文 JSON。 JSON 应是:
{
'question':'size',
'isTest': true
}
JSON 响应使用与已发布的知识库查询相同的架构。
注意
即使测试知识库和已发布的知识库看上去完全相同,也仍可能存在一些细微的差异,因为测试索引在资源中的所有知识库之间共享。
查询闲聊答案
在 Visual Studio Code REST 客户端扩展中,仅将正文 JSON 更改为用户的会话结束语句。 JSON 应是:
{ 'question':'thank you' }
响应包含评分和答案。
{ "answers": [ { "questions": [ "I thank you", "Oh, thank you", "My sincere thanks", "My humblest thanks to you", "Marvelous, thanks", "Marvelous, thank you kindly", "Marvelous, thank you", "Many thanks to you", "Many thanks", "Kthx", "I'm grateful, thanks", "Ahh, thanks", "I'm grateful for that, thank you", "Perfecto, thanks", "I appreciate you", "I appreciate that", "I appreciate it", "I am very thankful for that", "How kind, thank you", "Great, thanks", "Great, thank you", "Gracias", "Gotcha, thanks", "Gotcha, thank you", "Awesome thanks!", "I'm grateful for that, thank you kindly", "thank you pal", "Wonderful, thank you!", "Wonderful, thank you very much", "Why thank you", "Thx", "Thnx", "That's very kind", "That's great, thanks", "That is lovely, thanks", "That is awesome, thanks!", "Thanks bot", "Thanks a lot", "Okay, thanks!", "Thank you so much", "Perfect, thanks", "Thank you my friend", "Thank you kindly", "Thank you for that", "Thank you bot", "Thank you", "Right on, thanks very much", "Right on, thanks a lot", "Radical, thanks", "Rad, thanks", "Rad thank you", "Wonderful, thanks!", "Thanks" ], "answer": "You're welcome.", "score": 100.0, "id": 75, "source": "qna_chitchat_Professional.tsv", "metadata": [ { "name": "editorial", "value": "chitchat" } ], "context": { "isContextOnly": false, "prompts": [] } } ], "debugInfo": null, "activeLearningEnabled": true }
由于问题
Thank you
与聊天问题完全匹配,因此分数为 100,表明 QnA Maker 完全确信其答案。 QnA Maker 还返回了包含聊天元数据标记信息的所有相关问题和元数据属性。
使用阈值和默认答案
可以请求答案的最小阈值。 如果不满足阈值,将返回默认答案。
在 Visual Studio Code REST 客户端扩展中,仅将正文 JSON 更改为用户的会话结束语句。 JSON 应是:
{ 'question':'size', 'scoreThreshold':80.00 }
知识库应该不会查找该答案(因为问题的评分为 71%),而是返回创建知识库时提供的默认答案。
返回的 JSON 响应(包括评分和答案)为:
{ "answers": [ { "questions": [], "answer": "No good match found in KB.", "score": 0.0, "id": -1, "source": null, "metadata": [] } ], "debugInfo": null, "activeLearningEnabled": true }
QnA Maker 返回的评分为
0
,表示没有置信度。 它同时返回了默认解答。将阈值更改为 60%,并再次请求查询:
{ 'question':'size', 'scoreThreshold':60.00 }
返回的 JSON 找到了答案。
{ "answers": [ { "questions": [ "How large a knowledge base can I create?", "What is the max size of a knowledge base?", "How many GB of data can a knowledge base hold?" ], "answer": "The size of the knowledge base depends on the SKU of Azure search you choose when creating the QnA Maker service. Read [here](../concepts/azure-resources.md) for more details.", "score": 71.1, "id": 3, "source": "https://learn.microsoft.com/azure/ai-services/qnamaker/troubleshooting", "metadata": [ { "name": "link_in_answer", "value": "true" }, { "name": "server", "value": "qna_maker" } ], "context": { "isContextOnly": false, "prompts": [] } } ], "debugInfo": null, "activeLearningEnabled": true }
使用非结构化数据源。
我们现支持添加不能用于提取 QnA 的非结构化文档。 提取对查询的响应时,用户可选择在 GenerateAnswer API 中包含或排除非结构化数据集。 我们不支持在 GA 服务中使用非结构化数据集。 它仅在自定义问答中受到支持。
后续步骤
详细了解元数据: