[英]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.