如果您使用的是 Watson Conversation,那么您可以只使用 Python WDC SDK。
https://github.com/watson-developer-cloud/python-sdk
对于上面的示例,它将是:
from watson_developer_cloud import ConversationV1
username = 'USERNAME',
password = 'PASSWORD',
version = '2017-05-26',
workspace_id = 'bec28d8f-18c1-4e97-8d08-9c842c658b51'
url = 'https://gateway-s.watsonplatform.net/conversation/api'
conversation = ConversationV1(
username=username
password=password,
version=version,
url=url
}
dialog_nodes = []
welcome_node = {
'output': {
'text': { 'values': [ 'Welcome!' ],
'selection_policy': 'sequential'
}
},
'parent': None,
'context': None,
'metadata': None,
'next_step': None,
'conditions': 'welcome',
'dialog_node': 'Welcome',
'previous_sibling': None
}
dialog_nodes.append(welcome_node)
# this appends to the previous node above, set by previous_sibling
node = {
'dialog_node': 'greeting',
'conditions': '#hello',
'context': None,
'metadata': None,
'next_step': None,
'output':{
'text': { 'values': [ 'Hi! How can I help you?' ]},
'selection_policy': 'sequential'
}
},
'title': 'greeting ',
'previous_sibling': 'Welcome',
'parent': None
}
dialog_nodes.append(node)
## Update the workspace.
response = conversation.update_workspace(
workspace_id=workspace_id,
dialog_nodes=dialog_nodes
)
print(response)
此调用是全有或全无,因此如果您有现有节点,它将删除它们。原因是 SDK 没有单独的节点编辑。但这是一种更快的方法,而不是编辑单个节点(如果您有多个节点)。
如果您想进行单独调用,那么您将需要使用类似requests
, 直到 SDK 更新。
示例(使用上面相同的变量):
import requests
from requests.auth import HTTPBasicAuth
endpoint = '{}/v1/workspaces/{}/dialog_nodes?version={}'.format(url,workspace_id,version)
basic_auth = HTTPBasicAuth(username, password)
# Change the condition to always execute.
node['conditions'] = 'true'
response = requests.post(url=endpoint, auth=basic_auth, json=node)