如何将Excel与IBM Watson连接

时间:2019-04-03 07:31:17

标签: excel vba ibm-watson

我会定期收到一个包含问题的文档(我从以前的迭代中得到答案。问题总是相似的),并且我想自动答复。 我试图将Excel工作表连接到IBM Watson,以便我可以尝试教它回答问题。 我正在使用下面的代码连接到watson,但收到以下错误,我不确定为什么: “ #ERROR:401-{” code“:401,” error“:”未经授权“}”

我认为这与我的凭据的语法有关,但是我找不到任何支持示例。

不胜感激,举个例子是理想的。谢谢!

Function INTENT(query As String) As String
' IBM WATSON conversation service

If Len(query) > 0 Then 'If query is not empty
    Dim xmlhttp As New MSXML2.XMLHTTP60, workspaceID As String, workspaceURL As String, authUsername As String, authPassword As String, response As String, body As String

    ' WATSON Conversation Credentials
    **workspaceID = "a53a2519-30c5-47d5-a98b-cac2bf22fc5c"**
    'The API key from the first page of the project

    workspaceURL = "https://gateway.watsonplatform.net/assistant/api/v1/workspaces/" + workspaceID + "/message?version=2018–07–10" 'API requests require a version parameter
    **authUsername = "apikey"
    authPassword = "luVydRf_mxwAxvLHu0kFvTnIQMJttDLGnoMYSDkhpCrx"**


    body = "{""input"":{""text"":"" " + Replace(query, Chr(34), "") + " ""}}" 'The user input in JSON format. Also removed double quotes from input

    xmlhttp.Open "POST", workspaceURL, False, authUsername, authPassword
    xmlhttp.setRequestHeader "Content-Type", "application/json"

    xmlhttp.Send body

    If xmlhttp.Status = 200 Then 'Request success
        response = xmlhttp.responseText
        INTENT = parseINTENT(response)
    Else 'Request fail
        INTENT = "#ERROR: " & xmlhttp.Status & " - " & xmlhttp.responseText
    End If
End If
End Function

Function parseINTENT(text As String) As String
'Parse intent and confidence from JSON response

Dim p1 As Integer, p2 As Integer

p1 = InStr(text, "[{" + Chr(34) + "intent")
p2 = InStr(p1, text, "}]")
parseINTENT = Mid(text, p1, (p2 - p1) + 2)

End Function

0 个答案:

没有答案