flask - Watson assistant deployment on Flask+WSGI server (gunicorn or wsgi)
问题描述
I am deploying my watson assistant chatbot on Flask + Gunicorn + Nginx.
I am able to successfully dockerize and run , but something is breaking my code. Multiple watson assistant sessions are being created while I send the messages to watson services. While I try to reply for an intent I get answer for another intent or slot or does not understand message
I have reviewed all the tutorials on digital ocean and github, but I think creating chatbot session should be handled differently.
app.py
from flask import Flask, render_template,Response,make_response,jsonify
import os
from ibm_watson import AssistantV2
import random
from random import randint
import json
#import report
from io import StringIO
app = Flask(__name__)
conversation = AssistantV2(
iam_apikey = 'key',
url='https://gateway.watsonplatform.net/assistant/api',
version='2019-05-19')
session = conversation.create_session("someid").get_result()
variables = None
#context_val = {}
@app.route('/')
@app.route('/index')
def chat():
return render_template('chat.html')
@app.route('/send_message/<message>')
def send_mesage(message):
text = ''
response = conversation.message(
assistant_id = 'id',
session_id= session['session_id'],input={'text': str(message),'options': {
'return_context': True}}
).get_result()
variables = response['output'].get('user_defined')
#context = response['context']['skills']['main skill']['user_defined']
for i in response['output']['generic']:
text = text+ i['text']+'\n'
return text
if __name__ == "__main__":
app.run(host='0.0.0.0')
wsgi.py
from app import app
if __name__ == "__main__":
app.run()
Dockerfile
FROM python:3.6
WORKDIR /app
ADD . /app
RUN chgrp -R 0 /app/app.log && chmod -R g=u /app/app.log
RUN pip install -r requirements.txt
EXPOSE 8080
CMD ["gunicorn", "-b", "0.0.0.0:8080", "app", "-p 8080:8080"]
RUN chmod 770 /app
USER 1001
解决方案
通过 V2 API使用IBM Watson Assistant 时,您需要注意以下对象:
- 首先,您创建一个Assistant。它管理与 Watson Assistant 的连接。
- 接下来,会话是聊天中的每个用户交互。
- 最后,一条消息在会话中流向 Watson,并返回一个响应。
您可能已经在文档中看到过这个简单的代码示例,您自己的代码 - 在一般层面上 - 类似。要使其工作,您需要为每个用户会话创建 Watson 会话,然后将消息作为相应会话的一部分发送。这样,聊天上下文就可以正确保存。您的代码当前初始化 Watson 并创建一次会话。您需要为每个用户创建一个会话。查看会话管理。
推荐阅读
- linkedin - 如何在 Linkedin 页面管理 api 中检索 Demographic Facets 破坏的有时间限制的追随者统计数据?
- javascript - 使用 Javascript 获取 Html 元素上存在的事件侦听器列表
- typescript - 打字稿消耗的对象仅显示添加的冷杉
- java - 如何在 ActiveMQ Artemis 中调度消息
- neo4j - 为相互关联的数据建模 GraphDb
- image-processing - ImageDataGenerator 找到 0 个属于 2 个类别的图像
- typescript - headerTitleAlign:'left',在带有 TypeScript 选项卡项目的 EXPO 中不起作用
- arduino-c++ - 使用 Arduino 打印氧饱和度
- apache-spark - Spark 未完全读取 AVRO 文件
- vue.js - Select2 不适用于 Vue.js 中动态添加的字段