-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(AssistantV2): New service AssistantV2
- Loading branch information
Showing
5 changed files
with
1,906 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from __future__ import print_function | ||
import json | ||
from watson_developer_cloud import AssistantV2 | ||
|
||
# If service instance provides API key authentication | ||
# assistant = AssistantV2( | ||
# version='2017-04-21', | ||
# ## url is optional, and defaults to the URL below. Use the correct URL for your region. | ||
# url='https://gateway.watsonplatform.net/assistant/api', | ||
# iam_apikey='iam_apikey') | ||
|
||
assistant = AssistantV2( | ||
username='YOUR SERVICE USERNAME', | ||
password='YOUR SERVICE PASSWORD', | ||
## url is optional, and defaults to the URL below. Use the correct URL for your region. | ||
url='https://gateway.watsonplatform.net/assistant/api', | ||
version='2017-04-21') | ||
|
||
######################### | ||
# Sessions | ||
######################### | ||
|
||
session = assistant.create_session("<YOUR ASSISTANT ID>").get_result() | ||
print(json.dumps(session, indent=2)) | ||
|
||
assistant.delete_session("<YOUR ASSISTANT ID>", "<YOUR SESSION ID>").get_result() | ||
|
||
######################### | ||
# Message | ||
######################### | ||
|
||
message = assistant.message( | ||
"<YOUR ASSISTANT ID>", | ||
"<YOUR SESSION ID>", | ||
input={'text': 'What\'s the weather like?'}, | ||
context={ | ||
'metadata': { | ||
'deployment': 'myDeployment' | ||
} | ||
}).get_result() | ||
print(json.dumps(message, indent=2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# coding: utf-8 | ||
import json | ||
import responses | ||
import watson_developer_cloud | ||
|
||
platform_url = 'https://gateway.watsonplatform.net' | ||
service_path = '/assistant/api' | ||
base_url = '{0}{1}'.format(platform_url, service_path) | ||
|
||
@responses.activate | ||
def test_create_session(): | ||
endpoint = '/v2/assistants/{0}/sessions'.format('bogus_id') | ||
url = '{0}{1}'.format(base_url, endpoint) | ||
response = {'session_id': 'session_id'} | ||
responses.add( | ||
responses.POST, | ||
url, | ||
body=json.dumps(response), | ||
status=200, | ||
content_type='application/json') | ||
service = watson_developer_cloud.AssistantV2( | ||
username='username', password='password', version='2017-02-03') | ||
session = service.create_session('bogus_id').get_result() | ||
assert len(responses.calls) == 1 | ||
assert responses.calls[0].request.url.startswith(url) | ||
assert session == response | ||
|
||
|
||
@responses.activate | ||
def test_delete_session(): | ||
endpoint = '/v2/assistants/{0}/sessions/{1}'.format('bogus_id', | ||
'session_id') | ||
url = '{0}{1}'.format(base_url, endpoint) | ||
response = {} | ||
responses.add( | ||
responses.DELETE, | ||
url, | ||
body=json.dumps(response), | ||
status=200, | ||
content_type='application/json') | ||
service = watson_developer_cloud.AssistantV2( | ||
username='username', password='password', version='2017-02-03') | ||
delete_session = service.delete_session('bogus_id', | ||
'session_id').get_result() | ||
assert len(responses.calls) == 1 | ||
assert responses.calls[0].request.url.startswith(url) | ||
assert delete_session == response | ||
|
||
|
||
@responses.activate | ||
def test_message(): | ||
endpoint = '/v2/assistants/{0}/sessions/{1}/message'.format( | ||
'bogus_id', 'session_id') | ||
url = '{0}{1}'.format(base_url, endpoint) | ||
response = { | ||
'output': { | ||
'generic': [{ | ||
'text': | ||
'I did not understand that. I can help you get pizza, tell a joke or find a movie.', | ||
'response_type': | ||
'text' | ||
}], | ||
'entities': [], | ||
'intents': [{ | ||
'confidence': 0.8521236419677736, | ||
'intent': 'Weather' | ||
}] | ||
} | ||
} | ||
responses.add( | ||
responses.POST, | ||
url, | ||
body=json.dumps(response), | ||
status=200, | ||
content_type='application/json') | ||
service = watson_developer_cloud.AssistantV2( | ||
username='username', password='password', version='2017-02-03') | ||
message = service.message( | ||
'bogus_id', 'session_id', input={ | ||
'text': 'What\'s the weather like?' | ||
}).get_result() | ||
assert len(responses.calls) == 1 | ||
assert responses.calls[0].request.url.startswith(url) | ||
assert message == response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.