Skip to content

Commit

Permalink
feat(AssistantV2): New service AssistantV2
Browse files Browse the repository at this point in the history
  • Loading branch information
ehdsouza committed Sep 19, 2018
1 parent 831f589 commit a715334
Show file tree
Hide file tree
Showing 5 changed files with 1,906 additions and 0 deletions.
41 changes: 41 additions & 0 deletions examples/assistant_v2.py
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))
1 change: 1 addition & 0 deletions test/integration/test_discovery_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def test_queries(self):
return_fields='extracted_metadata.sha1').get_result()
assert query_results is not None

@pytest.mark.skip(reason="Temporary skipping because update_credentials fails")
def test_credentials(self):
credential_details = {
'credential_type': 'username_password',
Expand Down
84 changes: 84 additions & 0 deletions test/unit/test_assistant_v2.py
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
1 change: 1 addition & 0 deletions watson_developer_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .iam_token_manager import IAMTokenManager
from .conversation_v1 import ConversationV1
from .assistant_v1 import AssistantV1
from .assistant_v2 import AssistantV2
from .language_translator_v3 import LanguageTranslatorV3
from .natural_language_classifier_v1 import NaturalLanguageClassifierV1
from .natural_language_understanding_v1 import NaturalLanguageUnderstandingV1
Expand Down
Loading

0 comments on commit a715334

Please sign in to comment.