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

Python 如何打印IBM Watson Assistant返回的答案?,python,flask,ibm-cloud,ibm-watson,watson-conversation,Python,Flask,Ibm Cloud,Ibm Watson,Watson Conversation,我有一个Python Flask应用程序,它尝试使用IBM Watson Assistant。下面是调用消息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

我有一个Python Flask应用程序,它尝试使用IBM Watson Assistant。下面是调用消息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)

您的代码没有显示如何设置和配置。这个如果对返回的消息对象使用
json.dumps
,则可以看到


结果结构取决于您在SDK初始化期间配置的类型(代码中未显示)。它只能以文本作为数组,或者在最新的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
@app.route(/returned\u answer/)
def对话(mensaje):
response=assistant.message(workspace_id='1bef94fd-be51-4996-956c-73f9d0f08c41',input={'text':mensaje})
mens=(json.dumps(响应,缩进=2))
msj=json.loads(mens)
#打印(json.dumps(响应,缩进=2))
打印(msj[“输出”][“文本”][0])#重新打印
rewa=(msj[“输出”][“文本”][0])
返回rewa
为输入消息提供消息标签, ,并在索引页中编写html代码,将/返回的\u应答消息嵌入索引页中

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

您能在这里添加适当的代码片段作为您的答案吗?对于本网站上的答案,外部来源的链接不是很好。@L.ScottJohnson感谢您的反馈,这里是新的,我们将遵守