繁体   English   中英

如何打印IBM Watson Assistant返回的答案?

[英]How to print the answer returned by IBM Watson Assistant?

我有一个尝试使用IBM Watson Assistant的Python Flask应用程序。 下面是调用消息API函数的代码段。 如何打印返回的答案?

import json, _watson, requests, jsonify
import watson_developer_cloud
from flask import Flask, render_template
from flask_socketio import SocketIO, send


@app.route('/')
def index():
    return render_template('index.html')


@socketio.on('message')
def handleMessage(msg):
    print("Message: "+msg)
    msg = _watson.conversacion(msg)
    send(msg, broadcast=False)

def conversacion(mensaje):
    response = assistant.message(workspace_id='1bef94fd-be51-4996-956c-73f9d0f08c41', input={'text': mensaje})
    mens = (json.dumps(response, indent=2))
    msj = json.loads(mens)
    # print(json.dumps(response, indent=2))
    print(msj["output"]["text"][0])  # mensaje de respuesta
    rewa = (msj["output"]["text"][0])
    return rewa


if __name__=='__main__':
    socketio.run(app)

您的代码未显示如何使用IBM Watson Assistant的凭证设置和配置Python SDK API参考中记录了消息功能及其输入和输出 如果在返回的消息对象上使用json.dumps ,则可以看到结果(响应)结构

结果结构取决于您在SDK初始化期间配置的API版本 (代码中未显示)。 它只能将文本作为数组,或者使用最新的API版本,它可以包含图像,可供选择的选项等。 所有内容都以JSON结构的形式在output元素下显示(如代码所示)。

将您返回的答案发布到另一页

@app.route(/returned_answer/<mensaje>)
def conversacion(mensaje):
        response = assistant.message(workspace_id='1bef94fd-be51-4996-956c-73f9d0f08c41', input={'text': mensaje})
        mens = (json.dumps(response, indent=2))
        msj = json.loads(mens)
        # print(json.dumps(response, indent=2))
        print(msj["output"]["text"][0])  # mensaje de respuesta
        rewa = (msj["output"]["text"][0])
        return rewa

给您的输入消息一个消息标签,然后在索引页中编写html代码以将/ returned_answer消息嵌入索引页

  <button onclick="window.location.href = ('/returned_answer/'+document.getElementById('message_id').value)  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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