gpt4 book ai didi

python - 我需要做什么才能让 Watson 聊天机器人持续对话?

转载 作者:行者123 更新时间:2023-11-30 22:29:52 25 4
gpt4 key购买 nike

我想与 Watson 聊天机器人进行持续对话。

目前情况:聊天框不记得您之前发送的对话的状态。

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

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

用户:我要预订

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

用户:明天

聊天机器人:您的 session 时间怎么样?

用户: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
<小时/>

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

最佳答案

如您所见,有很多 examples在 Watson Developer Cloud 中使用来自 IBM Developers 的每个 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))

关于python - 我需要做什么才能让 Watson 聊天机器人持续对话?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46214804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com