|
| 1 | +import json |
| 2 | +import os |
| 3 | +from ibm_watson import DiscoveryV2 |
| 4 | +from ibm_watson.discovery_v2 import TrainingExample |
| 5 | +from ibm_cloud_sdk_core.authenticators import CloudPakForDataAuthenticator, BearerTokenAuthenticator |
| 6 | + |
| 7 | +## Important: Discovery v2 is only available on Cloud Pak for Data. ## |
| 8 | + |
| 9 | +## Authentication ## |
| 10 | +## Option 1: username/password |
| 11 | +authenticator = CloudPakForDataAuthenticator('<your username>', |
| 12 | + '<your password>', |
| 13 | + '<url for authentication>', |
| 14 | + disable_ssl_verification=True) |
| 15 | + |
| 16 | +## Option 2: bearer token |
| 17 | +authenticator = BearerTokenAuthenticator('your bearer token') |
| 18 | + |
| 19 | +## Initialize discovery instance ## |
| 20 | +discovery = DiscoveryV2(version='2019-11-22', authenticator=authenticator) |
| 21 | +discovery.set_service_url( |
| 22 | + '<service url>' |
| 23 | +) |
| 24 | +discovery.set_disable_ssl_verification(True) |
| 25 | + |
| 26 | +PROJECT_ID = 'your project id' |
| 27 | +## List Collections ## |
| 28 | +collections = discovery.list_collections(project_id=PROJECT_ID).get_result() |
| 29 | +print(json.dumps(collections, indent=2)) |
| 30 | + |
| 31 | +## Component settings ## |
| 32 | +settings_result = discovery.get_component_settings( |
| 33 | + project_id=PROJECT_ID).get_result() |
| 34 | +print(json.dumps(settings_result, indent=2)) |
| 35 | + |
| 36 | +## Add Document ## |
| 37 | +COLLECTION_ID = 'your collection id' |
| 38 | +with open(os.path.join(os.getcwd(), '..', 'resources', |
| 39 | + 'simple.html')) as fileinfo: |
| 40 | + add_document_result = discovery.add_document(project_id=PROJECT_ID, |
| 41 | + collection_id=COLLECTION_ID, |
| 42 | + file=fileinfo).get_result() |
| 43 | +print(json.dumps(add_document_result, indent=2)) |
| 44 | +document_id = add_document_result.get('document_id') |
| 45 | + |
| 46 | +## Create Training Data ## |
| 47 | +training_example = TrainingExample(document_id=document_id, |
| 48 | + collection_id=COLLECTION_ID, |
| 49 | + relevance=1) |
| 50 | +create_query = discovery.create_training_query( |
| 51 | + project_id=PROJECT_ID, |
| 52 | + natural_language_query='How is the weather today?', |
| 53 | + examples=[training_example]).get_result() |
| 54 | +print(json.dumps(create_query, indent=2)) |
| 55 | + |
| 56 | +training_queries = discovery.list_training_queries( |
| 57 | + project_id=PROJECT_ID).get_result() |
| 58 | +print(json.dumps(training_queries, indent=2)) |
| 59 | + |
| 60 | +## Queries ## |
| 61 | +query_result = discovery.query( |
| 62 | + project_id=PROJECT_ID, |
| 63 | + collection_ids=[COLLECTION_ID], |
| 64 | + natural_language_query='How is the weather today?').get_result() |
| 65 | +print(json.dumps(query_result, indent=2)) |
| 66 | + |
| 67 | +autocomplete_result = discovery.get_autocompletion( |
| 68 | + project_id=PROJECT_ID, prefix="The content").get_result() |
| 69 | +print(json.dumps(autocomplete_result, indent=2)) |
| 70 | + |
| 71 | +query_notices_result = discovery.query_notices( |
| 72 | + project_id=PROJECT_ID, natural_language_query='warning').get_result() |
| 73 | +print(json.dumps(query_notices_result, indent=2)) |
| 74 | + |
| 75 | +list_fields = discovery.list_fields(project_id=PROJECT_ID).get_result() |
| 76 | +print(json.dumps(list_fields, indent=2)) |
| 77 | + |
| 78 | +## Cleanup ## |
| 79 | +discovery.delete_training_queries(project_id=PROJECT_ID).get_result() |
| 80 | + |
| 81 | +delete_document_result = discovery.delete_document( |
| 82 | + project_id=PROJECT_ID, collection_id=COLLECTION_ID, |
| 83 | + document_id=document_id).get_result() |
| 84 | +print(json.dumps(delete_document_result, indent=2)) |
0 commit comments