我该怎么做才能让沃森聊天机器人继续聊天?

2024-03-28 21:57:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想和沃森聊天机器人继续聊天。在

现状: 聊天室不记得您以前发送的对话的状态。在

我在服务器上安装了Django框架并创建了一个沃森.py文件以加载工作区并使用KakaoTalk,一个韩语聊天应用程序。在

我想要的聊天机器人的对话流程如下。在

用户:我想预订

聊天机器人:会议日期是什么时候?

用户:明天

你的会议时间怎么样?

用户:14:00

我们非常需要你的帮助。在


在沃森.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

在视图.py在

^{pr2}$

Tags: text用户frompyimportidreturnresponse
2条回答

如您所见,Watson开发人员云中有很多examples供IBM开发人员使用。看一个使用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))

正如@sayurimizuchi提到的,主要问题是没有维护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

然后举个例子:

^{pr2}$

相关问题 更多 >