我定期收到带有问题的 excel 文件(我有以前迭代的答案。问题总是相似的),我想自动回复。我在 Microsoft 的 QnAMaker 中创建了一个包含所有问题/答案对的知识库。
我怎样才能调用微软的 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>"}
最佳答案
我找到了两种方法来做到这一点。首先是VBA。其次是电源查询。
VBA
可选设置
备注:
VBA 设置
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
运行后,我得到:
电源查询
创建 HTTP 连接查询
代码:
(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
创建表
然后,您可以将问题添加到表中,转到创建问题/答案表的工作表,然后单击刷新以获取新答案。
关于excel - 如何将 excel 连接到微软的 QnA Maker (VBA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55496567/