Watson Unity Assistant (旧

標準

元記事>「Watson×Unity!初心者でもできる、VR 空間で Unity ちゃんとおしゃべりアプリ!」

https://www.ibm.com/developerworks/jp/cloud/library/unity/

新しいUnity-SDKに対応させるときのメモ。

旧 Watson Conversation →新 Watson Assistant

大きく変更がないならば、こちらの方のアドバイスに従って、SampleAssistant.csの書き換えで対応する。

インスペクターに入力するVersion Dateは、https://www.ibm.com/watson/developercloud/assistant/api/v1/curl.html?curl#list-examples

このあたりを見ると、出てくる (APIのバージョン)
2018/4/21では、2018-02-16だそうであるので、入力は、2018-02-16

===========

//WatsonAssistant.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using IBM.Watson.DeveloperCloud.Services.Assistant.v1;
using IBM.Watson.DeveloperCloud.Connection;
using IBM.Watson.DeveloperCloud.Utilities;

public class WatsonAssistant : MonoBehaviour
{
 #region PLEASE SET THESE VARIABLES IN THE INSPECTOR
 [SerializeField]
 private string _username;
 [SerializeField]
 private string _password;
 [SerializeField]
 private string _url;
 [SerializeField]
 private string _versionDate;
 [SerializeField]
 private string _workspaceId;
 #endregion

private Assistant m_Conversation;
 private string m_Input = "おはよう";

// Use this for initialization
 void Start()
 {
 // Create credential and instantiate service
 Credentials credentials = new Credentials(_username, _password, _url);

m_Conversation = new Assistant(credentials);
 Debug.Log("User: " + m_Input);

m_Conversation.VersionDate = _versionDate;

// Message
 Dictionary<string, object> input = new Dictionary<string, object>();
 input.Add("text", m_Input);
 MessageRequest messageRequest = new MessageRequest()
 {
 Input = input
 };
 m_Conversation.Message(OnMessage, OnFail, _workspaceId, messageRequest);
 }

 void OnMessage(object resp, Dictionary<string, object> customData)
 {
 if (resp is Dictionary<string, object>)
 {
 Dictionary<string, object> dic_resp = (Dictionary<string, object>)resp;

foreach (object o in (List<object>)dic_resp["intents"])
 {
 Dictionary<string, object> dic_intent = (Dictionary<string, object>)o;
 Debug.Log("intent: " + dic_intent["intent"] + ", confidence: " + dic_intent["confidence"]);
 }

Dictionary<string, object> dic_output = (Dictionary<string, object>)dic_resp["output"];
 string res = "";
 foreach (object o in (List<object>)dic_output["text"])
 {
 res += o.ToString();
 }
 Debug.Log("response: " + res);
 }

}
 private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
 {
 Debug.Log("SampleConversation.OnFail() Error received: " + error.ToString());
 }

// Update is called once per frame
 void Update()
 {
 }
}

 

広告

コメントは受け付けていません。