如何打印IBM Watson Assistant返回的答案? [英] How to print the answer returned by IBM Watson Assistant?

查看:101
本文介绍了如何打印IBM Watson Assistant返回的答案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Python Flask app that tries to use IBM Watson Assistant. Below is a code snippet that invokes the message API function. How do I print the returned answer?

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)

推荐答案

您的代码未显示您如何设置和配置消息函数及其输入和输出的记录在API参考中.如果在返回的消息对象上使用json.dumps,则可以看到

Your code does not show how you set up and configure the Python SDK with the credentials for IBM Watson Assistant. The message function with its input and output is documented here in the API reference. If you use json.dumps on the returned message object, you can see the result (response) structure.

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

The result structure depends on the API version which you configure during the SDK initialization (not shown in your code). It can have only text as an array or, with latest API versions, can contain images, options to choose from and more. All is returned in a JSON structure under the output element (which is shown in your code).

这篇关于如何打印IBM Watson Assistant返回的答案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆