【发布时间】:2018-07-02 03:53:27
【问题描述】:
早安。
我目前在大学的一个项目中使用 IBM 的 Watson SDK for unity,该项目非常受故事驱动。该项目的一个部分是多个对话,例如当玩家说出关键字“Play”时,它应该启动 Welcome 节点并跳转到 Dispatch 节点而不等待响应。
在 IBM Watson 云上进行测试时,情况正是如此,显示了 3 段单独的文本,但当在 unity 内部触发时,仅显示第一个“欢迎”节点。我是否遗漏了代码中的某些内容,或者这是否应该由 Watson 在云中解决。
请看下面我附加的对话脚本:
using System.Collections.Generic;
using UnityEngine;
using IBM.Watson.DeveloperCloud.Services.Conversation.v1;
using IBM.Watson.DeveloperCloud.Utilities;
using FullSerializer;
public delegate void ConversationResponseDelegate(string text, string intent, float confidence);
public class WatsonConversation : MonoBehaviour
{
public ConversationResponseDelegate ConversationResponse = delegate { };
[Header("Credentials")]
public string Url;
public string User;
public string Password;
public string WorkspaceId;
private Conversation _conversationApi;
private Dictionary<string, object> _context; // context to persist
private fsSerializer _serializer = new fsSerializer();
// Use this for initialization
void Start()
{
Credentials conversationCred = new Credentials(User, Password, Url);
_conversationApi = new Conversation(conversationCred);
_conversationApi.VersionDate = "2017-07-26";
}
public void SendConversationMessage(string text)
{
MessageRequest messageRequest = new MessageRequest()
{
input = new Dictionary<string, object>()
{
{ "text", text }
},
context = _context
};
if (!_conversationApi.Message(OnConversationResponse, WorkspaceId, messageRequest))
{
Debug.LogError("Failed to send the message to Conversation service!");
}
}
private void OnConversationResponse(object resp, string data)
{
if (resp != null)
{
// Convert resp to fsdata
fsData fsdata = null;
fsResult r = _serializer.TrySerialize(resp.GetType(), resp, out fsdata);
if (!r.Succeeded)
throw new WatsonException(r.FormattedMessages);
// Convert fsdata to MessageResponse
MessageResponse messageResponse = new MessageResponse();
object obj = messageResponse;
r = _serializer.TryDeserialize(fsdata, obj.GetType(), ref obj);
if (!r.Succeeded)
throw new WatsonException(r.FormattedMessages);
// remember the context for the next message
object tempContext = null;
Dictionary<string, object> respAsDict = resp as Dictionary<string, object>;
if (respAsDict != null)
{
respAsDict.TryGetValue("context", out tempContext);
}
if (tempContext != null)
_context = tempContext as Dictionary<string, object>;
else
Debug.LogError("Failed to get context");
if (ConversationResponse != null)
{
string respText = "";
string intent = "";
float confidence = 0f;
if (messageResponse.output.text.Length > 0)
{
respText = messageResponse.output.text[0];
}
if (messageResponse.intents.Length > 0)
{
intent = messageResponse.intents[0].intent;
confidence = messageResponse.intents[0].confidence;
}
ConversationResponse(respText, intent, confidence);
}
}
}
}
【问题讨论】:
标签: unity3d watson-conversation watson