简体   繁体   中英

Watson Unity SDK, Jump to, Evaluate Response

Good Morning.

I am currently using IBM's Watson SDK for unity for a project at university which is very story driven. One part of this project is multiple dialogues, for example when the player says the keyword 'Play' it should initiate the Welcome node and jump to the Dispatch node without waiting for a response.

When testing on the IBM Watson cloud this is exactly the case, 3 separate pieces of text are shown but when triggered inside unity only the first 'Welcome' node is shown. Am I missing something from code or is this something that should be tackled by Watson in the cloud.

我的调教工作区图片

Please see below my attached Conversation script:

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);
            }
        }
    }
}

Your code is only looking at the first item in the array of text responses back, or the 0th item. Loop through the messageResponse.output.text[n] array.

For an additional reference, I've made the same mistake in the past and wrote this post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM