问题
I want to have a continuous conversation with watson chatbot.
current situation : The chatbox does not remember the status of the conversation you previously sent.
I installed the Django framework on the server and created a watson.py file to load the workspace and work with KakaoTalk, a Korean chat application.
The chatbot's conversation flow that I want is as follows.
User: I want to make a reservation
Chatbot: When is the meeting date?
User: tomorrow
Chatbot: How is your meeting time?
User: 14:00
We need your help very much.
watson.py
import json
from watson_developer_cloud import ConversationV1
from .models import Test
from . import views
import simplejson
conversation = ConversationV1(
username = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
password = "xxxxxxxxxxxxxxxxxxxxxx",
version = '2017-05-26' )
workspace_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #workspace
def test(return_str):
result = ''
except Exception as err:
pass
response = conversation.message(
workspace_id = workspace_id,
message_input = {'text': return_str},
)
for i in range(len(response['output']['text'])):
result += response['output']['text'][i] +'\n'
return result
views.py
import json
from django.shortcuts import render
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from .models import Jidum, Test
from . import watson
# Create your views here.
def keyboard(request):
return JsonResponse({
'type':'text',
})
@csrf_exempt
def message(request):
message = ((request.body).decode('utf-8'))
return_json_str = json.loads(message)
return_str = return_json_str['content']
return JsonResponse({
'message': {
'text': watson.test(return_str),
},
'keyboard': {
'type':'text',
},
})
回答1:
As you can see, has a lot examples inside Watson Developer Cloud for use each API from IBM Developers. Take a look in one example using Watson Conversation.
When you wanna use the same conversation for multiple requests (message), you need to include context object from the previous response.
But remember, you need to create the conversation flow inside your workspace.
For example:
import json
from watson_developer_cloud import ConversationV1
#########################
# message
#########################
conversation = ConversationV1(
username='YOUR SERVICE USERNAME',
password='YOUR SERVICE PASSWORD',
version='2017-04-21')
# replace with your own workspace_id
workspace_id = '0a0c06c1-8e31-4655-9067-58fcac5134fc'
# this example don't include
response = conversation.message(workspace_id=workspace_id, message_input={
'text': 'What\'s the weather like?'})
print(json.dumps(response, indent=2))
# This example include the context object from the previous response.
# response = conversation.message(workspace_id=workspace_id, message_input={
# 'text': 'turn the wipers on'},
# context=response['context']) //example
# print(json.dumps(response, indent=2))
- See the Official API Reference using Python.
回答2:
As @SayuriMizuguchi mentions the main issue is with not maintaining the context
object.
Based on your sample above, you would do something like:
context = {}
def test(input_string):
except Exception as err:
pass
response = conversation.message(
workspace_id = workspace_id,
message_input = {'text': return_str},
context = context
)
result = '\n'.join(response['output']['text'])
context = response['context']
return result
Then to run that as an example:
response = test('I want to make a reservation')
print(response)
response = test('tomorrow')
print(response)
来源:https://stackoverflow.com/questions/46214804/what-do-i-have-to-do-for-the-continuous-conversation-of-watson-chatbot