2

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
data_henrik
  • 16,724
  • 2
  • 28
  • 49
Quantum Dreamer
  • 432
  • 1
  • 6
  • 17

1 Answers1

1

When working with IBM Watson Assistant with the V2 API, you need to be aware of the following objects:

  • First, you create an Assistant. It manages the connection to Watson Assistant.
  • Next, a Session is a per user interaction within the chat.
  • Last, a Message flows to Watson within a session with a Response coming back.

You probably have seen this simple code sample in the docs, your own code is - on a general level - similar. To make it work, you need to create Watson sessions per user sessions, then send the messages as part of the corresponding session. That way, the chat context is kept correctly. Your code currently initialize Watson and creates a session once. You need to create a session per user. Look into session management.

data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • in my app.py conversation = AssistantV2( iam_apikey = 'key', url='https://gateway.watsonplatform.net/assistant/api', version='2019-05-19') creates an assistant and session = conversation.create_session("someid").get_result() creates a session – Quantum Dreamer Aug 01 '19 at 16:03
  • Exactly, but how many users do you have that interact with Watson? And sessions time out after some time https://cloud.ibm.com/docs/services/assistant?topic=assistant-assistant-settings – data_henrik Aug 02 '19 at 06:09
  • I am expecting this app to create a new user session for every use connected. I am aware of WSGI and Nginx but new session is not created for every user and I getting invalid session from IBM after deploying on NGINX. So I strongly suspect something is wrong the way I am handling the conversation. – Quantum Dreamer Aug 02 '19 at 19:34
  • 1
    I added to my answer. Your code creates a session only once when it starts and after initializing Watson. You need to do it separately and per user, e.g., when displaying the chat window the first time. – data_henrik Aug 03 '19 at 07:16
  • 1
    You can post-process the input string, you can define pattern-based entitities and much more. – data_henrik Aug 28 '19 at 17:46