2017-09-14 138 views
1

我想與watson聊天機器人進行持續對話。關於watson chatbot的連續對話,我需要做些什麼?

當前情況: 聊天箱不記得您之前發送的對話的狀態。

我在服務器上安裝了Django框架,並創建了一個watson.py文件來加載工作區,並使用韓國聊天應用程序KakaoTalk。

我想要的chatbot的會話流程如下。

用戶:我想預約

聊天機器人:什麼時候開會日期?

用戶:明天

聊天機器人:你的會議時間?

用戶:14:00

我們需要您的幫助很大。


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', 
         }, 
       }) 

回答

3

正如你可以看到,有很多examples沃森開發雲內使用IBM的開發者每個API 。看看使用Watson Conversation的一個示例。

當您想要爲多個請求(消息)使用相同的對話時,您需要包含上一個響應中的上下文對象。

但請記住,您需要在工作區內創建對話流程。

例如:

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)) 
+1

2秒打我!:-O :) –

+0

嗨@ SimonO'Doherty,如果您想要增加更多信息,請回答這個問題。我會喜歡,就像我一樣,哈哈!對不起,我是Watson Developer激情。 –

1

作爲@SayuriMizuguchi提到的主要問題是與不維護context對象。

基於以上的樣品,你會做這樣的事情:

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 

然後運行爲例:

response = test('I want to make a reservation') 
print(response) 
response = test('tomorrow') 
print(response) 
+0

我試圖按照您教給我的方式修改源代碼,但KaKaotalk得到了[錯誤的鍵盤初始化]消息,並且發生了錯誤。 –

相關問題