产品在树莓派上安装和运行,以及调用IBM Waston的服务都可以进行。但是有如下的问题:

  1. 速度很慢,几乎需要半分钟左右才能从对话到声音回答;
  2. 第二句话的时候,会报错。session out,可能是程序对分段连续对话的处理机制有问题。

速度对产品的体验有至关重要的影响。研究证明如果反应控制在200毫秒以内应该是人类感觉到比较”舒服”的时间,长了的话,人类的兴趣也好体验也罢就会速度降低。

有以下几个方案可以考虑产品在回复速度上面的提升:

  1. 线路改进 - 用VPN等手段选择靠近服务器的线路,德国/美国南部/澳大利亚;
  2. 加快用户声音转文本的速度(speech to text);
  3. 加快语料库生成对话回复的速度(text output);
  4. 加快文本转语音部分的速度(text to speech);

首先看下如何加快文本转语音的速度,解决方案是可以利用缓存:
参考:https://www.igorsramos.com/watson-text-to-speech-caching/
总的说来,就是利用一个Control来控制输出的音频,平时从local本地来调取音频,必要的时候从IBM Waston来取,取的东西,按照Index对应文件名存到本地,以便接下来的需要的时候调取。

示意图:

如果Cache Search搜索到了词条,直接播放。如果没有此词条,就调用Waston播放,同时将返回的音频存在本地。
所以理论上说,这个是越用越快的一个东西。相当于遍历了尽可能多的回答,打上标签,然后以备调取

例:

代码:

[{"id":"3de6aca1.301b04","type":"exec","z":"5d32c0c0.5a8a3","command":"omxplayer","addpay":true,"append":"","useSpawn":"","timer":"","name":"Play","x":630,"y":80,"wires":[[],[],[]]},{"id":"45f20593.7f955c","type":"function","z":"5d32c0c0.5a8a3","name":"Play Audio","func":"\nbase = \"/tmp/cache_speech/\";\nmsg.payload = base + msg.file;\n\nreturn msg;","outputs":"1","noerr":0,"x":427.1428756713867,"y":219.999981880188,"wires":[["1704af8e.4f3a3"]]},{"id":"5051c394.5cb85c","type":"function","z":"5d32c0c0.5a8a3","name":"Cache Search","func":"msg.query = msg.payload;\n\n// for latency computation\nvar id = msg._msgid.replace('.',''); //remove '.' as that messes up flow.get;\nvar now = new Date().getTime();\nflow.set(id, now);\n// end of latency\n\nvar cache = flow.get ('cache_speech') || 0;\nif (cache === 0) \n{\n    msg.cache = \"Cache not initialized.\";\n    return [null, msg];\n}\n\nvar len = cache.length;\n\nfor (var idx=0;idx<len;idx++)\n{\n    if (cache[idx].key === msg.query)\n    {\n        msg.file = cache[idx].file;\n        msg.cache = \"Found in Cache\";\n        return [msg, null];\n    } \n}\n\nmsg.cache = \"Entry not in Cache\";\nreturn [null, msg];","outputs":"2","noerr":0,"x":202.14287567138672,"y":200.999981880188,"wires":[["45f20593.7f955c"],["efaeee4c.1a2a2"]]},{"id":"79ddfad6.6d7c74","type":"function","z":"5d32c0c0.5a8a3","name":"Latency","func":"\nvar id = msg._msgid;\nid = id.replace('.',''); //remove '.' as that messes up flow.get\nvar now = new Date().getTime();\n\n\nvar startTime = flow.get(id);\nvar dt = now - startTime;\nmsg.latency = \n{\n    'tts': dt + \"ms\", \n    'id':id, \n    'start':startTime,\n    'end':now\n};\nreturn msg;\n","outputs":1,"noerr":0,"x":640,"y":140,"wires":[["c1b220aa.57df8"]]},{"id":"ed5811e.f032df","type":"function","z":"5d32c0c0.5a8a3","name":"name file","func":"//generate the filename\nvar d = new Date();\nvar n = d.getTime();\nmsg.file = n + \".ogg\";\n\nreturn msg;\n","outputs":1,"noerr":0,"x":200,"y":340,"wires":[["6c548c82.edc9e4","45f20593.7f955c","839d03f6.a6ced"]]},{"id":"c1b220aa.57df8","type":"debug","z":"5d32c0c0.5a8a3","name":"tts latency","active":true,"console":"false","complete":"latency.tts","x":790,"y":140,"wires":[]},{"id":"efaeee4c.1a2a2","type":"watson-text-to-speech","z":"5d32c0c0.5a8a3","name":"Watson TTS","lang":"english","voice":"en-US_MichaelVoice","format":"audio/ogg; codecs=opus","x":190,"y":280,"wires":[["ed5811e.f032df"]]},{"id":"6c548c82.edc9e4","type":"function","z":"5d32c0c0.5a8a3","name":"Cache Save","func":"var cache = flow.get ('cache_speech') || 0;\nif (cache === 0) \n{\n    return null;\n}\n\nvar newEntry =\n    {    \n      'key': msg.query,\n      'file': msg.file\n    };\ncache.push(newEntry);\n\nflow.set('cache_speech', cache);\n\nmsg.payload = cache;\nreturn msg;","outputs":1,"noerr":0,"x":367.1428756713867,"y":339.999981880188,"wires":[["93641ef2.f6896"]]},{"id":"839d03f6.a6ced","type":"function","z":"5d32c0c0.5a8a3","name":"Save Speech","func":"base = \"/tmp/cache_speech/\";\nmsg.filename = base + msg.file;\nmsg.payload = msg.speech;\nreturn msg;","outputs":1,"noerr":0,"x":367.1428756713867,"y":379.999981880188,"wires":[["391f9a2c.8ac976"]]},{"id":"93641ef2.f6896","type":"file","z":"5d32c0c0.5a8a3","name":"cache_speech","filename":"/tmp/cache_speech/cache_speech.json","appendNewline":false,"createDir":false,"overwriteFile":"true","x":537.1428756713867,"y":339.999981880188,"wires":[]},{"id":"391f9a2c.8ac976","type":"file","z":"5d32c0c0.5a8a3","name":"SaveSpeech","filename":"","appendNewline":false,"createDir":false,"overwriteFile":"true","x":527.1428756713867,"y":379.999981880188,"wires":[]},{"id":"cd3b9857.dafc58","type":"function","z":"5d32c0c0.5a8a3","name":"reset cache file","func":"//TODO: write something to rm -rf *.ogg in the cache folder\n\nvar cache = \n    [\n        {    \n            key: \"cache test A\",\n            file: \"abc.wav\"\n        },\n        {    \n            key: \"cache test B\",\n            file: \"efg.wav\"\n            \n        }\n    ];\nflow.set('cache_speech', cache);\nmsg.payload = cache;\nreturn msg;","outputs":"1","noerr":0,"x":300,"y":560,"wires":[["53652bb2.620434"]]},{"id":"36dc7f41.3d1ce","type":"inject","z":"5d32c0c0.5a8a3","name":"DELETE","topic":"","payload":"go","payloadType":"str","repeat":"","crontab":"","once":false,"x":120,"y":560,"wires":[["cd3b9857.dafc58"]]},{"id":"db331921.fbd6b8","type":"inject","z":"5d32c0c0.5a8a3","name":"SHOW","topic":"","payload":"this is a test","payloadType":"str","repeat":"","crontab":"","once":false,"x":110,"y":520,"wires":[["bf70964b.fd6418"]]},{"id":"bf70964b.fd6418","type":"function","z":"5d32c0c0.5a8a3","name":"dump mem cache","func":"var cache = flow.get ('cache_speech');\nmsg.payload = cache;\nreturn msg;","outputs":"1","noerr":0,"x":310,"y":520,"wires":[["c9e81f8c.c54a9"]]},{"id":"fa3d97e0.883498","type":"file in","z":"5d32c0c0.5a8a3","name":"cache_speech","filename":"/tmp/cache_speech/cache_speech.json","format":"utf8","x":300,"y":480,"wires":[["85419828.fe5188"]]},{"id":"1eecfdb9.9cf8a2","type":"inject","z":"5d32c0c0.5a8a3","name":"LOAD","topic":"","payload":"go","payloadType":"str","repeat":"","crontab":"","once":true,"x":110,"y":480,"wires":[["fa3d97e0.883498"]]},{"id":"51fb6bd8.3bdea4","type":"debug","z":"5d32c0c0.5a8a3","name":"","active":false,"console":"false","complete":"payload","x":670,"y":480,"wires":[]},{"id":"53652bb2.620434","type":"file","z":"5d32c0c0.5a8a3","name":"cache_speech","filename":"/tmp/cache_speech/cache_speech.json","appendNewline":false,"createDir":true,"overwriteFile":"true","x":480,"y":560,"wires":[]},{"id":"c9e81f8c.c54a9","type":"debug","z":"5d32c0c0.5a8a3","name":"","active":true,"console":"false","complete":"payload","x":490,"y":520,"wires":[]},{"id":"85419828.fe5188","type":"function","z":"5d32c0c0.5a8a3","name":"load cache","func":"var cache = JSON.parse(msg.payload);\n\nflow.set('cache_speech', cache);\n\nmsg.payload = cache;\n\nreturn msg;\n","outputs":"1","noerr":0,"x":490,"y":480,"wires":[["51fb6bd8.3bdea4"]]},{"id":"340934f.0a540cc","type":"comment","z":"5d32c0c0.5a8a3","name":"------------------------tts cache ---------------------------------------------------------------------------","info":"","x":370,"y":440,"wires":[]},{"id":"90933cae.df713","type":"inject","z":"5d32c0c0.5a8a3","name":"Hello world!","topic":"","payload":"Hello world!","payloadType":"str","repeat":"","crontab":"","once":false,"x":110,"y":60,"wires":[["985ccdc2.0cf15"]]},{"id":"647b59ff.a58d98","type":"inject","z":"5d32c0c0.5a8a3","name":"Long Text","topic":"","payload":"I have been assigned to handle your order status request. I am sorry to inform you that the items you requested are back-ordered. We apologize for the inconvenience. We don't know when those items will become available. Maybe next week but we are not sure at this time. Because we want you to be a happy customer, management has decided to give you a 50% discount!","payloadType":"str","repeat":"","crontab":"","once":false,"x":100,"y":100,"wires":[["985ccdc2.0cf15"]]},{"id":"985ccdc2.0cf15","type":"link out","z":"5d32c0c0.5a8a3","name":"","links":["636a1d0d.8f2214"],"x":255,"y":80,"wires":[]},{"id":"636a1d0d.8f2214","type":"link in","z":"5d32c0c0.5a8a3","name":"input","links":["985ccdc2.0cf15"],"x":75,"y":200,"wires":[["5051c394.5cb85c"]]},{"id":"1704af8e.4f3a3","type":"link out","z":"5d32c0c0.5a8a3","name":"","links":["bdc2f653.e17cc8"],"x":575,"y":220,"wires":[]},{"id":"bdc2f653.e17cc8","type":"link in","z":"5d32c0c0.5a8a3","name":"play","links":["1704af8e.4f3a3"],"x":535,"y":80,"wires":[["3de6aca1.301b04","79ddfad6.6d7c74"]]}]


References:
[1] The Incredible Thing We Do During Conversations http://www.theatlantic.com/science/archive/2016/01/the-incredible-thing-we-do-during-conversations/422439/