2017-09-14 3 views
1

私はワトソンのchatbotとの連続的な会話をしたいです。ワトソンのチャットボートの継続的な会話には何が必要ですか?

現在の状況: チャットボックスでは、以前に送信した会話のステータスは記憶されません。

サーバーにDjangoフレームワークをインストールし、ワークスペースを読み込んで、韓国のチャットアプリケーションであるKakaoTalkで作業するwatson.pyファイルを作成しました。

私が望むチャットボットの会話フローは、次のとおりです。

ユーザー:会議の日がある:私は予約

チャットボットを作りたいですか?

ユーザー:明日

チャットボット:どのようにあなたの会議の時間がありますか?

ユーザーイン: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

あなたが見ることができるように、IBM開発者から各APIの使用のためにワトソン開発クラウド内でたくさんexamplesを持っています。 1つの例を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)) 
  • は、Pythonを使って公式API Referenceを参照してください。
+1

が2秒で私を倒します!:-O :) –

+0

こんにちは@ SimonO'Doherty、あなたがaccrescentにもっと情報にしたい場合は、この質問にも答えてください。私はいつものようにuphaote、ハハ!申し訳ありませんが、私はワトソンの開発者です。 –

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が[間違ったキーボード初期化]メッセージを受け取り、エラーが発生しました。 –

関連する問題