Skip to content

Commit d351a19

Browse files
Merge pull request #384 from watson-developer-cloud/conversation-example
Adding two step process that supports renamed props
2 parents 885f680 + 29b04b8 commit d351a19

4 files changed

Lines changed: 320 additions & 138 deletions

File tree

examples/conversation_v1.py

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,50 @@
1919
# workspaces
2020
#########################
2121

22-
response = conversation.create_workspace(name='test_workspace',
23-
description='Test workspace.',
24-
language='en',
25-
metadata={})
22+
create_workspace_data = {
23+
"name": "test_workspace",
24+
"description": "integration tests",
25+
"language": "en",
26+
"intents": [
27+
{
28+
"intent": "hello",
29+
"description": "string",
30+
"examples": [
31+
{
32+
"text": "good morning"
33+
}
34+
]
35+
}
36+
],
37+
"entities": [
38+
{
39+
"entity": "pizza_toppings",
40+
"description": "Tasty pizza toppings",
41+
"metadata": {
42+
"property": "value"
43+
}
44+
}
45+
],
46+
"counterexamples": [
47+
{
48+
"text": "string"
49+
}
50+
],
51+
"metadata": {},
52+
}
53+
54+
55+
response = conversation.create_workspace(name=create_workspace_data['name'],
56+
description=create_workspace_data['description'],
57+
language='en',
58+
intents=create_workspace_data['intents'],
59+
entities=create_workspace_data['entities'],
60+
counterexamples=create_workspace_data['counterexamples'],
61+
metadata=create_workspace_data['metadata'])
2662
print(json.dumps(response, indent=2))
2763

2864
workspace_id = response['workspace_id']
65+
print("Workspace id ".format(workspace_id))
2966

3067
response = conversation.get_workspace(workspace_id=workspace_id, export=True)
3168
print(json.dumps(response, indent=2))
@@ -48,9 +85,11 @@
4885
# intents
4986
#########################
5087

88+
examples = [{ "text": "good morning"}]
5189
response = conversation.create_intent(workspace_id=workspace_id,
5290
intent='test_intent',
53-
description='Test intent.')
91+
description='Test intent.',
92+
examples=examples)
5493
print(json.dumps(response, indent=2))
5594

5695
response = conversation.get_intent(workspace_id=workspace_id,
@@ -134,8 +173,33 @@
134173
values=values)
135174
print(json.dumps(response, indent=2))
136175

176+
entities = [{
177+
'entity': 'pattern_entity',
178+
'values': [{
179+
'value': 'value0', 'patterns': ['\\d{6}\\w{1}\\d{7}'], 'value_type': 'patterns'
180+
},
181+
{'value': 'value1',
182+
'patterns': ['[-9][0-9][0-9][0-9][0-9]~! [1-9][1-9][1-9][1-9][1-9][1-9]'],
183+
'value_type': 'patterns'},
184+
{'value': 'value2',
185+
'patterns': ['[a-z-9]{17}'],
186+
'value_type': 'patterns'},
187+
{'value': 'value3',
188+
'patterns': [
189+
'\\d{3}(\\ |-)\\d{3}(\\ |-)\\d{4}',
190+
'\\(\\d{3}\\)(\\ |-)\\d{3}(\\ |-)\\d{4}'],
191+
'value_type': 'patterns'},
192+
{'value': 'value4',
193+
'patterns': ['\\b\\d{5}\\b'],
194+
'value_type': 'patterns'}]
195+
}]
196+
response = conversation.create_entity(workspace_id,
197+
entity=entities[0]['entity'],
198+
values=entities[0]['values'])
199+
print(json.dumps(response, indent=2))
200+
137201
response = conversation.get_entity(workspace_id=workspace_id,
138-
entity='test_entity',
202+
entity=entities[0]['entity'],
139203
export=True)
140204
print(json.dumps(response, indent=2))
141205

@@ -198,6 +262,43 @@
198262

199263
conversation.delete_entity(workspace_id, 'test_entity')
200264

265+
#########################
266+
# Dialog nodes
267+
#########################
268+
create_dialog_node = {
269+
"dialog_node": "greeting",
270+
"description": "greeting messages",
271+
"actions": [
272+
{
273+
"name": "hello",
274+
"type": "client",
275+
"parameters": {},
276+
"result_variable": "string",
277+
"credentials": "string"
278+
}
279+
]
280+
}
281+
response = conversation.create_dialog_node(workspace_id,
282+
create_dialog_node['dialog_node'],
283+
create_dialog_node['description'],
284+
actions=create_dialog_node['actions'])
285+
print(json.dumps(response, indent=2))
286+
287+
response = conversation.get_dialog_node(workspace_id,
288+
create_dialog_node['dialog_node'])
289+
print(json.dumps(response, indent=2))
290+
291+
response = conversation.list_dialog_nodes(workspace_id)
292+
print(json.dumps(response, indent=2))
293+
294+
response = conversation.update_dialog_node(workspace_id,
295+
create_dialog_node['dialog_node'],
296+
new_dialog_node='updated_node')
297+
print(json.dumps(response, indent=2))
298+
299+
response = conversation.delete_dialog_node(workspace_id, 'updated_node')
300+
print(json.dumps(response, indent=2))
301+
201302
#########################
202303
# logs
203304
#########################

test/unit/test_conversation_v1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ def test_message():
899899
message = conversation.message(
900900
workspace_id=workspace_id,
901901
input={'text': 'Turn on the lights'},
902-
context=json.dumps(message_ctx))
902+
context=json.dumps(message_ctx['context']))
903903

904904
assert message is not None
905905
assert responses.calls[1].request.url == message_url1

0 commit comments

Comments
 (0)