Skip to content

Commit

Permalink
feat(discoveryv2): Add examples for discoveryv2
Browse files Browse the repository at this point in the history
  • Loading branch information
ehdsouza committed Nov 21, 2019
1 parent 2ce0ad3 commit 2b54527
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 80 deletions.
83 changes: 83 additions & 0 deletions examples/discovery_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import json
import os
from ibm_watson import DiscoveryV2
from ibm_watson.discovery_v2 import TrainingExample
from ibm_cloud_sdk_core.authenticators import CloudPakForDataAuthenticator, BearerTokenAuthenticator


## Authentication ##
## Option 1: username/password
authenticator = CloudPakForDataAuthenticator('<your username>',
'<your password>',
'<url for authentication>',
disable_ssl_verification=True)

## Option 2: bearer token
authenticator = BearerTokenAuthenticator('your bearer token')

## Initialize discovery instance ##
discovery = DiscoveryV2(version='2019-11-22', authenticator=authenticator)
discovery.set_service_url(
'https://zen-gm-cpd-zen-gm.apps.big-smoke-lb-1.fyre.ibm.com/discovery/deweyan-poet/instances/1574286017227/api'
)
discovery.set_disable_ssl_verification(True)

PROJECT_ID = 'your project id'
## List Collections ##
collections = discovery.list_collections(project_id=PROJECT_ID).get_result()
print(json.dumps(collections, indent=2))

## Component settings ##
settings_result = discovery.get_component_settings(
project_id=PROJECT_ID).get_result()
print(json.dumps(settings_result, indent=2))

## Add Document ##
COLLECTION_ID = 'your collection id'
with open(os.path.join(os.getcwd(), '..', 'resources',
'simple.html')) as fileinfo:
add_document_result = discovery.add_document(project_id=PROJECT_ID,
collection_id=COLLECTION_ID,
file=fileinfo).get_result()
print(json.dumps(add_document_result, indent=2))
document_id = add_document_result.get('document_id')

## Create Training Data ##
training_example = TrainingExample(document_id=document_id,
collection_id=COLLECTION_ID,
relevance=1)
create_query = discovery.create_training_query(
project_id=PROJECT_ID,
natural_language_query='How is the weather today?',
examples=[training_example]).get_result()
print(json.dumps(create_query, indent=2))

training_queries = discovery.list_training_queries(
project_id=PROJECT_ID).get_result()
print(json.dumps(training_queries, indent=2))

## Queries ##
query_result = discovery.query(
project_id=PROJECT_ID,
collection_ids=[COLLECTION_ID],
natural_language_query='How is the weather today?').get_result()
print(json.dumps(query_result, indent=2))

autocomplete_result = discovery.get_autocompletion(
project_id=PROJECT_ID, prefix="The content").get_result()
print(json.dumps(autocomplete_result, indent=2))

query_notices_result = discovery.query_notices(
project_id=PROJECT_ID, natural_language_query='warning').get_result()
print(json.dumps(query_notices_result, indent=2))

list_fields = discovery.list_fields(project_id=PROJECT_ID).get_result()
print(json.dumps(list_fields, indent=2))

## Cleanup ##
discovery.delete_training_queries(project_id=PROJECT_ID).get_result()

delete_document_result = discovery.delete_document(
project_id=PROJECT_ID, collection_id=COLLECTION_ID,
document_id=document_id).get_result()
print(json.dumps(delete_document_result, indent=2))
76 changes: 43 additions & 33 deletions ibm_watson/discovery_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,10 +759,10 @@ def delete_training_queries(self, project_id, **kwargs):

def create_training_query(self,
project_id,
natural_language_query,
examples,
*,
natural_language_query=None,
filter=None,
examples=None,
**kwargs):
"""
Create training query.
Expand All @@ -772,21 +772,19 @@ def create_training_query(self,
:param str project_id: The ID of the project. This information can be found
from the deploy page of the Discovery administrative tooling.
:param str natural_language_query: (optional) The natural text query for
the training query.
:param str natural_language_query: The natural text query for the training
query.
:param list[TrainingExample] examples: Array of training examples.
:param str filter: (optional) The filter used on the collection before the
**natural_language_query** is applied.
:param list[TrainingExample] examples: (optional) Array of training
examples.
:param dict headers: A `dict` containing the request headers
:return: A `DetailedResponse` containing the result, headers and HTTP status code.
:rtype: DetailedResponse
"""

if project_id is None:
raise ValueError('project_id must be provided')
if examples is not None:
examples = [self._convert_model(x) for x in examples]
examples = [self._convert_model(x) for x in examples]

headers = {}
if 'headers' in kwargs:
Expand All @@ -799,8 +797,8 @@ def create_training_query(self,

data = {
'natural_language_query': natural_language_query,
'filter': filter,
'examples': examples
'examples': examples,
'filter': filter
}

url = '/v2/projects/{0}/training_data/queries'.format(
Expand Down Expand Up @@ -855,10 +853,10 @@ def get_training_query(self, project_id, query_id, **kwargs):
def update_training_query(self,
project_id,
query_id,
natural_language_query,
examples,
*,
natural_language_query=None,
filter=None,
examples=None,
**kwargs):
"""
Update a training query.
Expand All @@ -868,12 +866,11 @@ def update_training_query(self,
:param str project_id: The ID of the project. This information can be found
from the deploy page of the Discovery administrative tooling.
:param str query_id: The ID of the query used for training.
:param str natural_language_query: (optional) The natural text query for
the training query.
:param str natural_language_query: The natural text query for the training
query.
:param list[TrainingExample] examples: Array of training examples.
:param str filter: (optional) The filter used on the collection before the
**natural_language_query** is applied.
:param list[TrainingExample] examples: (optional) Array of training
examples.
:param dict headers: A `dict` containing the request headers
:return: A `DetailedResponse` containing the result, headers and HTTP status code.
:rtype: DetailedResponse
Expand All @@ -883,8 +880,11 @@ def update_training_query(self,
raise ValueError('project_id must be provided')
if query_id is None:
raise ValueError('query_id must be provided')
if examples is not None:
examples = [self._convert_model(x) for x in examples]
if natural_language_query is None:
raise ValueError('natural_language_query must be provided')
if examples is None:
raise ValueError('examples must be provided')
examples = [self._convert_model(x) for x in examples]

headers = {}
if 'headers' in kwargs:
Expand All @@ -897,8 +897,8 @@ def update_training_query(self,

data = {
'natural_language_query': natural_language_query,
'filter': filter,
'examples': examples
'examples': examples,
'filter': filter
}

url = '/v2/projects/{0}/training_data/queries/{1}'.format(
Expand Down Expand Up @@ -5354,16 +5354,16 @@ class TrainingExample():
:attr str document_id: The document ID associated with this training example.
:attr str collection_id: The collection ID associated with this training
example.
:attr int relevance: (optional) The relevance of the training example.
:attr int relevance: The relevance of the training example.
:attr date created: (optional) The date and time the example was created.
:attr date updated: (optional) The date and time the example was updated.
"""

def __init__(self,
document_id,
collection_id,
relevance,
*,
relevance=None,
created=None,
updated=None):
"""
Expand All @@ -5373,7 +5373,7 @@ def __init__(self,
example.
:param str collection_id: The collection ID associated with this training
example.
:param int relevance: (optional) The relevance of the training example.
:param int relevance: The relevance of the training example.
:param date created: (optional) The date and time the example was created.
:param date updated: (optional) The date and time the example was updated.
"""
Expand Down Expand Up @@ -5409,6 +5409,10 @@ def _from_dict(cls, _dict):
)
if 'relevance' in _dict:
args['relevance'] = _dict.get('relevance')
else:
raise ValueError(
'Required property \'relevance\' not present in TrainingExample JSON'
)
if 'created' in _dict:
args['created'] = _dict.get('created')
if 'updated' in _dict:
Expand Down Expand Up @@ -5450,36 +5454,34 @@ class TrainingQuery():
Object containing training query details.
:attr str query_id: (optional) The query ID associated with the training query.
:attr str natural_language_query: (optional) The natural text query for the
training query.
:attr str natural_language_query: The natural text query for the training query.
:attr str filter: (optional) The filter used on the collection before the
**natural_language_query** is applied.
:attr date created: (optional) The date and time the query was created.
:attr date updated: (optional) The date and time the query was updated.
:attr list[TrainingExample] examples: (optional) Array of training examples.
:attr list[TrainingExample] examples: Array of training examples.
"""

def __init__(self,
natural_language_query,
examples,
*,
query_id=None,
natural_language_query=None,
filter=None,
created=None,
updated=None,
examples=None):
updated=None):
"""
Initialize a TrainingQuery object.
:param str natural_language_query: The natural text query for the training
query.
:param list[TrainingExample] examples: Array of training examples.
:param str query_id: (optional) The query ID associated with the training
query.
:param str natural_language_query: (optional) The natural text query for
the training query.
:param str filter: (optional) The filter used on the collection before the
**natural_language_query** is applied.
:param date created: (optional) The date and time the query was created.
:param date updated: (optional) The date and time the query was updated.
:param list[TrainingExample] examples: (optional) Array of training
examples.
"""
self.query_id = query_id
self.natural_language_query = natural_language_query
Expand All @@ -5505,6 +5507,10 @@ def _from_dict(cls, _dict):
args['query_id'] = _dict.get('query_id')
if 'natural_language_query' in _dict:
args['natural_language_query'] = _dict.get('natural_language_query')
else:
raise ValueError(
'Required property \'natural_language_query\' not present in TrainingQuery JSON'
)
if 'filter' in _dict:
args['filter'] = _dict.get('filter')
if 'created' in _dict:
Expand All @@ -5515,6 +5521,10 @@ def _from_dict(cls, _dict):
args['examples'] = [
TrainingExample._from_dict(x) for x in (_dict.get('examples'))
]
else:
raise ValueError(
'Required property \'examples\' not present in TrainingQuery JSON'
)
return cls(**args)

def _to_dict(self):
Expand Down
Loading

0 comments on commit 2b54527

Please sign in to comment.