早上好。
我目前正在使用IBM的Watson SDK来统一大学的项目,这是一个非常故事驱动的项目。该项目的一部分是多个对话,例如当玩家说出关键字'Play'时,它应该启动Welcome节点并跳转到Dispatch节点而不等待响应。
在IBM Watson云上进行测试时,情况确实如此,显示了3个单独的文本,但在统一内部触发时,仅显示第一个“欢迎”节点。我是否遗漏了代码中的内容,或者这是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);
}
}
}
}
答案 0 :(得分:1)
您的代码仅查看文本响应数组中的第一项或第0项。遍历messageResponse.output.text [n]数组。
作为额外的参考,我在过去犯了同样的错误并写了post。