使用C# Azure函数调用QuestionAnsweringClient获取知识库答案关联提示的求助
解决方法
首先确保使用最新版的Azure.AI.Language.QuestionAnswering NuGet包,旧版本可能缺失相关属性支持。
调用GetAnswersAsync方法后,返回的Response<KnowledgeBaseAnswers>中,每个KnowledgeBaseAnswer对象的Dialog属性包含关联对话信息,其中Prompts字段就是你需要的提示列表。
示例代码
using Azure; using Azure.AI.Language.QuestionAnswering; var endpoint = new Uri("你的QA服务端点"); var credential = new AzureKeyCredential("你的密钥"); var client = new QuestionAnsweringClient(endpoint, credential); var question = "你的问题"; var projectName = "知识库项目名"; var deploymentName = "部署名"; var options = new QuestionAnsweringOptions { Top = 1 }; Response<KnowledgeBaseAnswers> response = await client.GetAnswersAsync(question, projectName, deploymentName, options); foreach (var answer in response.Value.Answers) { Console.WriteLine($"答案: {answer.Answer}"); // 获取关联的提示列表 if (answer.Dialog != null && answer.Dialog.Prompts != null) { Console.WriteLine("关联提示:"); foreach (var prompt in answer.Dialog.Prompts) { Console.WriteLine($"- 提示文本: {prompt.DisplayText}"); Console.WriteLine($" 关联QnA条目ID: {prompt.QnaId}"); } } }
关键说明
KnowledgeBaseAnswer.Dialog.Prompts是IReadOnlyList<KnowledgeBaseAnswerPrompt>类型,每个KnowledgeBaseAnswerPrompt包含DisplayText(提示显示文本)、QnaId(关联的QnA条目ID)等核心属性。- 若知识库中该答案未配置后续提示,
Dialog会返回null,需先做非空判断再访问属性。
若之前未获取到提示列表,排查方向:
- 检查NuGet包版本,升级到最新稳定版
- 确认知识库中对应答案已配置后续提示
- 确认代码中是否正确访问
Dialog.Prompts属性,而非KnowledgeBaseAnswerDialog的其他无关字段
内容的提问来源于stack exchange,提问作者bhavik katriya
相关产品推荐
相关产品推荐

