excel - 如何将 excel 连接到微软的 QnA Maker (VBA)

标签 excel vba qnamaker

我定期收到带有问题的 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

可选设置

enter image description here

  • 通过转到文件 > 选项 > 自定义功能区 > 检查开发人员
  • ,将开发人员选项卡添加到功能区。
  • 转到开发人员选项卡,然后插入 > 按钮
  • 将宏命名为“GetReplies”
  • 点击新建

  • 备注:
  • 我的 QnAKB 基于 this FAQ ,主要是。

  • VBA 设置
  • 在“开发人员”选项卡中,单击 Visual Basic 按钮(如果它没有自动打开)
  • 转到工具 > 引用并启用以下库:
    enter image description here
  • 进口 this JSON library关注 the installation instructions
  • 复制/粘贴:

  • 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
    
  • 适当编辑顶部的“用户设置”

  • 运行后,我得到:

    enter image description here

    电源查询

    创建 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
    
  • 必要时替换变量
  • 将查询重命名为“GetAnswer”
  • 退出 Power Query,保存更改

  • 创建表
  • 用您的问题创建一个表格

  • enter image description here
  • 选择表。表设计选项卡 > 将表重命名为 Answers
  • 选择整个表后,数据选项卡 > 从表/范围
  • 添加列 > 调用自定义函数
  • 列名 = Answers,函数查询 = GetAnswer,问题:ColumnName = Question
  • 好的。确定/退出/保存

  • 然后,您可以将问题添加到表中,转到创建问题/答案表的工作表,然后单击刷新以获取新答案。

    关于excel - 如何将 excel 连接到微软的 QnA Maker (VBA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55496567/

    相关文章:

    vba 计算超链接的点击次数

    vba - 使用其他工作簿宏 : Tools - References in VBA

    vba - Worksheet_BeforeDoubleClick 进行选择

    c# - 根据 QnAMaker 中的输入对结果进行排名

    VBA - 图表的多个系列

    excel - 用户窗体中的月 View - 自动关闭

    vba - 获取 VBA 中所有单元格更改的通知

    excel - 创建一个包含子文件夹和超链接的文件夹

    c# - BOT 框架中 qnA 和 Luis 之间的 Intent Score 映射