我会定期收到带有问题的excel文件(我从以前的迭代中得到答案。问题总是相似的),并且我想自动答复。我已经在Microsoft QnAMaker中创建了包含所有问题/答案对的知识库。
如何致电Microsoft的QnAMaker来回答来自Excel的问题。我一直在寻找VBA示例,但到目前为止我还没有发现任何东西。 我相信我需要使用vba从excel发出具有授权的HTTP请求,然后处理响应。 有人对我如何提出请求和处理请求有想法吗?
任何帮助表示赞赏。谢谢!
以下是QnAMaker提供的进行呼叫的示例详细信息:
POST /knowledgebases/d02d1d7e-1bc0-461a-8074-37749cae41b9/generateAnswer
Host: https://rfp1.azurewebsites.net/qnamaker
Authorization: EndpointKey cec4b630-9e77-474f-8df4-e6430a5678c8
Content-Type: application/json
{"question":"<Your question>"}
答案 0 :(得分:2)
我找到了两种方法来做到这一点。首先是使用VBA。其次是Power Queries。
通过转到“文件”>“选项”>“自定义功能区”>“检查开发人员”,将“开发人员”标签添加到功能区
转到“开发人员”标签,然后依次插入>按钮
将宏命名为“ GetReplies”
点击“新建”
注释:
从“开发人员”选项卡中,单击“ Visual Basic”按钮(如果它没有自动打开)
复制/粘贴以下内容:
VBA代码:
Sub GetReplies()
'User Settings
Dim questionWorksheetName As String, questionsColumn As String, firstQuestionRow As String, kbHost As String, kbId As String, endpointKey As String
questionWorksheetName = "Sheet1"
questionsColumn = "A"
firstQuestionRow = "2"
kbHost = "https://**********.azurewebsites.net/qnamaker"
kbId = "*******-****-****-****-**********"
endpointKey = "*********-****-****-****-***********"
'Non-User Settings
Dim questionWorksheet As Worksheet
Set questionWorksheet = Sheets(questionWorksheetName)
Dim startCell As String
startCell = questionsColumn & firstQuestionRow
Dim questionsRange As Range
Set questionsRange = questionWorksheet.Range(startCell, questionWorksheet.Range(startCell).End(xlDown))
'Loop through all non-blank cells
Dim answer As String
For Each cell In questionsRange
If Not IsEmpty(cell) Then
answer = GetAnswer(cell.Value, kbHost, kbId, endpointKey)
'Add answer to cell
cell.Offset(0, 1).Value = answer
End If
Next
End Sub
Function GetAnswer(question, kbHost, kbId, endpointKey) As String
'HTTP Request Settings
Dim qnaUrl As String
qnaUrl = kbHost & "/knowledgebases/" & kbId & "/generateAnswer"
Dim contentType As String
contentType = "application/json"
Dim data As String
data = "{""question"":""" & question & """}"
'Send Request
Dim xmlhttp As New MSXML2.XMLHTTP60
xmlhttp.Open "POST", qnaUrl, False
xmlhttp.SetRequestHeader "Content-Type", contentType
xmlhttp.SetRequestHeader "Authorization", "EndpointKey " & endpointKey
xmlhttp.Send data
'Convert response to JSON
Dim json As Dictionary
Set json = JsonConverter.ParseJson(xmlhttp.ResponseText)
Dim answer As Dictionary
For Each answer In json("answers")
'Return response
GetAnswer = answer("answer")
Next
End Function
跑步后,我得到:
数据标签>获取数据>从其他来源>空白查询
单击高级编辑器并复制粘贴
代码:
(Question as text) =>
let
url = "https://***host****.azurewebsites.net/qnamaker/knowledgebases/****kbId******/generateAnswer",
endpointKey = "****endpointKey*****",
table = Excel.CurrentWorkbook(){[Name="Answers"]}[Content],
row = Table.SelectRows(table, each ([Answer] = Question)),
body = "{""question"":""" & Question & """}",
Parsed_JSON = Json.Document(body),
BuildQueryString = Uri.BuildQueryString(Parsed_JSON),
headers = [#"Content-Type"="application/json", #"Authorization"="EndpointKey " & endpointKey],
content = Text.ToBinary(body),
Source = Json.Document(Web.Contents(url, [Headers = headers, Content = content])),
answers = Source[answers],
answers1 = answers{0},
answer = answers1[answer]
in
answer
根据需要替换变量
将查询重命名为“ GetAnswer”
从Power Query退出,保存更改
选择表格。表格设计标签>将表格重命名为答案
在选择整个表格后,依次单击“数据”选项卡>“从表格/范围”
添加列>调用自定义功能
列名=答案,函数查询= GetAnswer,问题:列名=问题
好。确定/退出/保存
然后可以将问题添加到表格中,转到创建“问题/答案”表格的工作表,然后单击“刷新”以获取新答案。