天天看點

IBM watson 部署一個對話機器人,Java調用 Assistant API

整體步驟

  1. 注冊一個IBM賬戶,http://console.bluemix.net
  2. 在控制台(Dashboard)建立資源(Create resource)
  3. 點選 Watson Assistant 服務
  4. 填寫服務名稱、想要部署的區域,建立一個Watson Assistant服務
  5. 啟動工具(Launch tool)
    IBM watson 部署一個對話機器人,Java調用 Assistant API
  6. 建立Skill
    IBM watson 部署一個對話機器人,Java調用 Assistant API
  7. 建立Assistants
  • 建立一個assistant,并把之前建立好的skill添加進assistant。
  • assistant詳情頁中包含一個分享連結可以在網頁上試用,也包含了API調用所需的id、url、username、password。
  • 我的watson asistant 分享試用位址:https://assistant-chat-jp-tok.watsonplatform.net/web/public/c24919e6-ff48-42c1-99c2-63a66df7f75b
    IBM watson 部署一個對話機器人,Java調用 Assistant API

Skill 建構步驟

  • 建構一個skill大緻需要建立intents、entities、dialog。
  • intents為使用者意圖;entities為需要了解的一些實體;dialog串起整個對話。
  • 以預定餐廳為例:使用者意圖為 “我想訂位置”;entities為星期幾、時間、幾個人這些元素;dialog告訴機器人接收到“我想訂位置” 這個意圖時,需要問什麼、答什麼。
  1. Creating Intents

    參考教程:https://cloud.ibm.com/docs/services/assistant?topic=assistant-intents#intents

  2. Creating Entities

    參考教程:https://cloud.ibm.com/docs/services/assistant?topic=assistant-entities#entities

  3. Build Dialog

    參考教程:https://cloud.ibm.com/docs/services/assistant?topic=assistant-dialog-overview#dialog-overview

我參考教程視訊建立的Skill的json配置檔案:

http://note.youdao.com/noteshare?id=4fb9d8bd65c31c23f1f6a2866bb952c5

機器人釋出到web上

參考教程:http://www-31.ibm.com/ibm/cn/ur/event/pdf/integrate_to_web.pdf

Java調用

  • 參考 demo:https://github.com/watson-developer-cloud/java-sdk
  • demo 中關于 assistant 的說明:https://github.com/watson-developer-cloud/java-sdk/blob/master/assistant/README.md
  • 我用的是v2接口,但 demo 沒有說明參數都是哪裡來的、缺少Endpoint設定、缺少sessionId擷取,調試過程中出現過如下問題:
    • Unauthorized: Access is denied due to invalid credentials. Tip: Did you set the Endpoint?
    • {“code”:401, “error”: “Unauthorized”}
    • {“error”:“Resource not found”,“code”:404}
  • 最終運作通過的代碼如下:
  • Gradle
compile group: 'com.ibm.watson.developer_cloud', name: 'assistant', version: '6.14.0'
           
  • 測試函數
public static void main(String[] args) throws Exception {

        String assistantId = "XXXXXXXXXXXXXXXXXXX"; // Assistant 詳情頁的Assistant ID
        String url = "https://gateway-tok.watsonplatform.net/assistant/api"; // Assistant 詳情頁的Assistant URL的一部分
        String username = "apikey"; // Assistant 詳情頁的 Service Credentials 的 Username
        String password = "XXXXXXXXXXXXXXXXXXX"; //  Assistant 詳情頁的 Service Credentials 的 Password

        Assistant service = new Assistant("2019-03-15"); // 随意
        service.setEndPoint(url);
        service.setUsernameAndPassword(username, password);

        CreateSessionOptions createSessionOptions = new CreateSessionOptions.Builder()
                .assistantId(assistantId)
                .build();
        SessionResponse sessionResponse = service.createSession(createSessionOptions).execute();
        String sessionId = sessionResponse.getSessionId();

        MessageInput input = new MessageInput.Builder()
                .text("Hi")
                .build();
        MessageOptions messageOptions = new MessageOptions.Builder()
                .assistantId(assistantId)
                .sessionId(sessionId)
                .input(input)
                .build();
        MessageResponse messageResponse = service.message(messageOptions).execute();

        System.out.println(messageResponse);
    }