如何连接反序列化的 QnAResponse 字符串来代替 {filename}?
How to concatenate the deserialized QnAResponse string in place of a {filename}?
在此文档中,它显示了 QnA Maker 服务执行 Rest 调用和反序列化 Json 响应 - 第 3 步 *How to link Luis with Qna
我想我正在寻找的模块字符串的特定部分:
/* START - QnA Maker Response Class */
public class Metadata
{
public string name { get; set; }
public string value { get; set; }
}
public class Answer
{
public IList<string> questions { get; set; }
public string answer { get; set; }
public double score { get; set; }
public int id { get; set; }
public string source { get; set; }
public IList<object> keywords { get; set; }
public IList<Metadata> metadata { get; set; }
}
public class QnAAnswer
{
public IList<Answer> answers { get; set; }
}
/* END - QnA Maker Response Class */
我不会猜测哪一部分会有答案,但 QNA 应该简单地用 filename 回复问题,然后需要将其连接到位@~\folder\folder\ filename.json 所以正确存储的自适应卡显示为响应。
我提供了比必要更多的上下文,因为我敢肯定还有其他人想要一种在需要问题时显示卡片的简单方法。
请随意编辑或使用适当的术语重新措辞。
谢谢
如果我答对了你的问题,你正在寻找将从机器人那里得到的答案。如果是这样,答案将在 HTTP 响应的正文中显示,如下所示
{
"answers": [
{
"questions": [
"What is the closing time?"
],
"answer": "10.30 PM",
"score": 100,
"id": 1,
"source": "Editorial",
"metadata": [
{
"name": "restaurant",
"value": "paradise"
},
{
"name": "location",
"value": "secunderabad"
}
]
}
]
}
所以理论上它会在 Answer.answer
编辑:
你需要先添加这个方法:
public string GetAnswer(string query)
{
var client = new RestClient( qnaServiceHostName + "/qnamaker/knowledgebases/" + knowledgeBaseId + "/generateAnswer");
var request = new RestRequest(Method.POST);
request.AddHeader("authorization", "EndpointKey " + endpointKey);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"question\": \"" + query + "\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
// Deserialize the response JSON
QnAAnswer answer = JsonConvert.DeserializeObject<QnAAnswer>(response.Content);
// Return the answer if present
if (answer.answers.Count > 0)
return answer.answers[0].answer;
else
return "No good match found.";
}
}
然后你可以调用:
Console.WriteLine ("the answer to how is the weather today is : " + GetAswers ("how's the weather today") + "... end of answer");
好了。
QNA should simply reply to a question with a filename which then needs to be concatenated in place of @~\folder\folder\ filename.json so a proper stored Adaptive Card displays as the response.
您似乎想根据 QnA Maker 服务 return 编辑的结果从 json 文件动态渲染自适应卡。实现需求可以参考下面的示例代码
[LuisIntent("Help")]
public async Task HelpIntent(IDialogContext context, LuisResult result)
{
var replymes = context.MakeMessage();
var answer = GetAnswer(result.Query);
var cardjson = await GetCardText($"{answer}");
var results = AdaptiveCard.FromJson(cardjson);
var card = results.Card;
replymes.Attachments.Add(new Attachment()
{
Content = card,
ContentType = AdaptiveCard.ContentType,
Name = "Card"
});
await context.PostAsync(replymes);
context.Wait(MessageReceived);
}
GetCardText 方法:
public async Task<string> GetCardText(string cardName)
{
var path = System.Web.HttpContext.Current.Server.MapPath($"~/AdaptiveCards/{cardName}.json");
if (!File.Exists(path))
return string.Empty;
using (var f = File.OpenText(path))
{
return await f.ReadToEndAsync();
}
}
测试结果:
注:
1) 在我上面的测试中,我的 QnA Maker 服务将 return 文件名直接作为答案,所以我将 returned 答案作为文件名
传递
2) 我在我的机器人应用程序中使用 AdaptiveCards -Version 1.0.3 来构建自适应卡片对象
在此文档中,它显示了 QnA Maker 服务执行 Rest 调用和反序列化 Json 响应 - 第 3 步 *How to link Luis with Qna
我想我正在寻找的模块字符串的特定部分:
/* START - QnA Maker Response Class */
public class Metadata
{
public string name { get; set; }
public string value { get; set; }
}
public class Answer
{
public IList<string> questions { get; set; }
public string answer { get; set; }
public double score { get; set; }
public int id { get; set; }
public string source { get; set; }
public IList<object> keywords { get; set; }
public IList<Metadata> metadata { get; set; }
}
public class QnAAnswer
{
public IList<Answer> answers { get; set; }
}
/* END - QnA Maker Response Class */
我不会猜测哪一部分会有答案,但 QNA 应该简单地用 filename 回复问题,然后需要将其连接到位@~\folder\folder\ filename.json 所以正确存储的自适应卡显示为响应。
我提供了比必要更多的上下文,因为我敢肯定还有其他人想要一种在需要问题时显示卡片的简单方法。
请随意编辑或使用适当的术语重新措辞。
谢谢
如果我答对了你的问题,你正在寻找将从机器人那里得到的答案。如果是这样,答案将在 HTTP 响应的正文中显示,如下所示
{
"answers": [
{
"questions": [
"What is the closing time?"
],
"answer": "10.30 PM",
"score": 100,
"id": 1,
"source": "Editorial",
"metadata": [
{
"name": "restaurant",
"value": "paradise"
},
{
"name": "location",
"value": "secunderabad"
}
]
}
]
}
所以理论上它会在 Answer.answer
编辑:
你需要先添加这个方法:
public string GetAnswer(string query)
{
var client = new RestClient( qnaServiceHostName + "/qnamaker/knowledgebases/" + knowledgeBaseId + "/generateAnswer");
var request = new RestRequest(Method.POST);
request.AddHeader("authorization", "EndpointKey " + endpointKey);
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"question\": \"" + query + "\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
// Deserialize the response JSON
QnAAnswer answer = JsonConvert.DeserializeObject<QnAAnswer>(response.Content);
// Return the answer if present
if (answer.answers.Count > 0)
return answer.answers[0].answer;
else
return "No good match found.";
}
}
然后你可以调用:
Console.WriteLine ("the answer to how is the weather today is : " + GetAswers ("how's the weather today") + "... end of answer");
好了。
QNA should simply reply to a question with a filename which then needs to be concatenated in place of @~\folder\folder\ filename.json so a proper stored Adaptive Card displays as the response.
您似乎想根据 QnA Maker 服务 return 编辑的结果从 json 文件动态渲染自适应卡。实现需求可以参考下面的示例代码
[LuisIntent("Help")]
public async Task HelpIntent(IDialogContext context, LuisResult result)
{
var replymes = context.MakeMessage();
var answer = GetAnswer(result.Query);
var cardjson = await GetCardText($"{answer}");
var results = AdaptiveCard.FromJson(cardjson);
var card = results.Card;
replymes.Attachments.Add(new Attachment()
{
Content = card,
ContentType = AdaptiveCard.ContentType,
Name = "Card"
});
await context.PostAsync(replymes);
context.Wait(MessageReceived);
}
GetCardText 方法:
public async Task<string> GetCardText(string cardName)
{
var path = System.Web.HttpContext.Current.Server.MapPath($"~/AdaptiveCards/{cardName}.json");
if (!File.Exists(path))
return string.Empty;
using (var f = File.OpenText(path))
{
return await f.ReadToEndAsync();
}
}
测试结果:
注:
1) 在我上面的测试中,我的 QnA Maker 服务将 return 文件名直接作为答案,所以我将 returned 答案作为文件名
传递2) 我在我的机器人应用程序中使用 AdaptiveCards -Version 1.0.3 来构建自适应卡片对象