Skip to content

Commit 611b7c9

Browse files
feat(assistant_v2,disco_v1): add answers property to response model, fix typo
1 parent ab880f0 commit 611b7c9

File tree

4 files changed

+159
-26
lines changed

4 files changed

+159
-26
lines changed

ibm_watson/assistant_v2.py

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4843,6 +4843,12 @@ class SearchResult():
48434843
:attr SearchResultHighlight highlight: (optional) An object containing segments
48444844
of text from search results with query-matching text highlighted using HTML
48454845
`<em>` tags.
4846+
:attr List[SearchResultAnswer] answers: (optional) An array specifying segments
4847+
of text within the result that were identified as direct answers to the search
4848+
query. Currently, only the single answer with the highest confidence (if any) is
4849+
returned.
4850+
**Note:** This property uses the answer finding beta feature, and is available
4851+
only if the search skill is connected to a Discovery v2 service instance.
48464852
"""
48474853

48484854
def __init__(self,
@@ -4852,7 +4858,8 @@ def __init__(self,
48524858
body: str = None,
48534859
title: str = None,
48544860
url: str = None,
4855-
highlight: 'SearchResultHighlight' = None) -> None:
4861+
highlight: 'SearchResultHighlight' = None,
4862+
answers: List['SearchResultAnswer'] = None) -> None:
48564863
"""
48574864
Initialize a SearchResult object.
48584865
@@ -4873,13 +4880,21 @@ def __init__(self,
48734880
:param SearchResultHighlight highlight: (optional) An object containing
48744881
segments of text from search results with query-matching text highlighted
48754882
using HTML `<em>` tags.
4883+
:param List[SearchResultAnswer] answers: (optional) An array specifying
4884+
segments of text within the result that were identified as direct answers
4885+
to the search query. Currently, only the single answer with the highest
4886+
confidence (if any) is returned.
4887+
**Note:** This property uses the answer finding beta feature, and is
4888+
available only if the search skill is connected to a Discovery v2 service
4889+
instance.
48764890
"""
48774891
self.id = id
48784892
self.result_metadata = result_metadata
48794893
self.body = body
48804894
self.title = title
48814895
self.url = url
48824896
self.highlight = highlight
4897+
self.answers = answers
48834898

48844899
@classmethod
48854900
def from_dict(cls, _dict: Dict) -> 'SearchResult':
@@ -4906,6 +4921,10 @@ def from_dict(cls, _dict: Dict) -> 'SearchResult':
49064921
if 'highlight' in _dict:
49074922
args['highlight'] = SearchResultHighlight.from_dict(
49084923
_dict.get('highlight'))
4924+
if 'answers' in _dict:
4925+
args['answers'] = [
4926+
SearchResultAnswer.from_dict(x) for x in _dict.get('answers')
4927+
]
49094928
return cls(**args)
49104929

49114930
@classmethod
@@ -4929,6 +4948,8 @@ def to_dict(self) -> Dict:
49294948
_dict['url'] = self.url
49304949
if hasattr(self, 'highlight') and self.highlight is not None:
49314950
_dict['highlight'] = self.highlight.to_dict()
4951+
if hasattr(self, 'answers') and self.answers is not None:
4952+
_dict['answers'] = [x.to_dict() for x in self.answers]
49324953
return _dict
49334954

49344955
def _to_dict(self):
@@ -4950,6 +4971,78 @@ def __ne__(self, other: 'SearchResult') -> bool:
49504971
return not self == other
49514972

49524973

4974+
class SearchResultAnswer():
4975+
"""
4976+
An object specifing a segment of text that was identified as a direct answer to the
4977+
search query.
4978+
4979+
:attr str text: The text of the answer.
4980+
:attr float confidence: The confidence score for the answer, as returned by the
4981+
Discovery service.
4982+
"""
4983+
4984+
def __init__(self, text: str, confidence: float) -> None:
4985+
"""
4986+
Initialize a SearchResultAnswer object.
4987+
4988+
:param str text: The text of the answer.
4989+
:param float confidence: The confidence score for the answer, as returned
4990+
by the Discovery service.
4991+
"""
4992+
self.text = text
4993+
self.confidence = confidence
4994+
4995+
@classmethod
4996+
def from_dict(cls, _dict: Dict) -> 'SearchResultAnswer':
4997+
"""Initialize a SearchResultAnswer object from a json dictionary."""
4998+
args = {}
4999+
if 'text' in _dict:
5000+
args['text'] = _dict.get('text')
5001+
else:
5002+
raise ValueError(
5003+
'Required property \'text\' not present in SearchResultAnswer JSON'
5004+
)
5005+
if 'confidence' in _dict:
5006+
args['confidence'] = _dict.get('confidence')
5007+
else:
5008+
raise ValueError(
5009+
'Required property \'confidence\' not present in SearchResultAnswer JSON'
5010+
)
5011+
return cls(**args)
5012+
5013+
@classmethod
5014+
def _from_dict(cls, _dict):
5015+
"""Initialize a SearchResultAnswer object from a json dictionary."""
5016+
return cls.from_dict(_dict)
5017+
5018+
def to_dict(self) -> Dict:
5019+
"""Return a json dictionary representing this model."""
5020+
_dict = {}
5021+
if hasattr(self, 'text') and self.text is not None:
5022+
_dict['text'] = self.text
5023+
if hasattr(self, 'confidence') and self.confidence is not None:
5024+
_dict['confidence'] = self.confidence
5025+
return _dict
5026+
5027+
def _to_dict(self):
5028+
"""Return a json dictionary representing this model."""
5029+
return self.to_dict()
5030+
5031+
def __str__(self) -> str:
5032+
"""Return a `str` version of this SearchResultAnswer object."""
5033+
return json.dumps(self.to_dict(), indent=2)
5034+
5035+
def __eq__(self, other: 'SearchResultAnswer') -> bool:
5036+
"""Return `true` when self and other are equal, false otherwise."""
5037+
if not isinstance(other, self.__class__):
5038+
return False
5039+
return self.__dict__ == other.__dict__
5040+
5041+
def __ne__(self, other: 'SearchResultAnswer') -> bool:
5042+
"""Return `true` when self and other are not equal, false otherwise."""
5043+
return not self == other
5044+
5045+
49535046
class SearchResultHighlight():
49545047
"""
49555048
An object containing segments of text from search results with query-matching text

ibm_watson/discovery_v1.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11967,33 +11967,33 @@ class StatusDetails():
1196711967
"""
1196811968
Object that contains details about the status of the authentication process.
1196911969

11970-
:attr bool authentication: (optional) Indicates whether the credential is
11970+
:attr bool authenticated: (optional) Indicates whether the credential is
1197111971
accepted by the target data source.
11972-
:attr str error_message: (optional) If `authentication` is `false`, a message
11972+
:attr str error_message: (optional) If `authenticated` is `false`, a message
1197311973
describes why the authentication was unsuccessful.
1197411974
"""
1197511975

1197611976
def __init__(self,
1197711977
*,
11978-
authentication: bool = None,
11978+
authenticated: bool = None,
1197911979
error_message: str = None) -> None:
1198011980
"""
1198111981
Initialize a StatusDetails object.
1198211982

11983-
:param bool authentication: (optional) Indicates whether the credential is
11983+
:param bool authenticated: (optional) Indicates whether the credential is
1198411984
accepted by the target data source.
11985-
:param str error_message: (optional) If `authentication` is `false`, a
11985+
:param str error_message: (optional) If `authenticated` is `false`, a
1198611986
message describes why the authentication was unsuccessful.
1198711987
"""
11988-
self.authentication = authentication
11988+
self.authenticated = authenticated
1198911989
self.error_message = error_message
1199011990

1199111991
@classmethod
1199211992
def from_dict(cls, _dict: Dict) -> 'StatusDetails':
1199311993
"""Initialize a StatusDetails object from a json dictionary."""
1199411994
args = {}
11995-
if 'authentication' in _dict:
11996-
args['authentication'] = _dict.get('authentication')
11995+
if 'authenticated' in _dict:
11996+
args['authenticated'] = _dict.get('authenticated')
1199711997
if 'error_message' in _dict:
1199811998
args['error_message'] = _dict.get('error_message')
1199911999
return cls(**args)
@@ -12006,8 +12006,8 @@ def _from_dict(cls, _dict):
1200612006
def to_dict(self) -> Dict:
1200712007
"""Return a json dictionary representing this model."""
1200812008
_dict = {}
12009-
if hasattr(self, 'authentication') and self.authentication is not None:
12010-
_dict['authentication'] = self.authentication
12009+
if hasattr(self, 'authenticated') and self.authenticated is not None:
12010+
_dict['authenticated'] = self.authenticated
1201112011
if hasattr(self, 'error_message') and self.error_message is not None:
1201212012
_dict['error_message'] = self.error_message
1201312013
return _dict

test/unit/test_assistant_v2.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4009,6 +4009,10 @@ def test_search_result_serialization(self):
40094009
search_result_highlight_model['url'] = ['testString']
40104010
search_result_highlight_model['foo'] = ['testString']
40114011

4012+
search_result_answer_model = {} # SearchResultAnswer
4013+
search_result_answer_model['text'] = 'testString'
4014+
search_result_answer_model['confidence'] = 0
4015+
40124016
# Construct a json representation of a SearchResult model
40134017
search_result_model_json = {}
40144018
search_result_model_json['id'] = 'testString'
@@ -4017,6 +4021,7 @@ def test_search_result_serialization(self):
40174021
search_result_model_json['title'] = 'testString'
40184022
search_result_model_json['url'] = 'testString'
40194023
search_result_model_json['highlight'] = search_result_highlight_model
4024+
search_result_model_json['answers'] = [search_result_answer_model]
40204025

40214026
# Construct a model instance of SearchResult by calling from_dict on the json representation
40224027
search_result_model = SearchResult.from_dict(search_result_model_json)
@@ -4033,6 +4038,36 @@ def test_search_result_serialization(self):
40334038
search_result_model_json2 = search_result_model.to_dict()
40344039
assert search_result_model_json2 == search_result_model_json
40354040

4041+
class TestModel_SearchResultAnswer():
4042+
"""
4043+
Test Class for SearchResultAnswer
4044+
"""
4045+
4046+
def test_search_result_answer_serialization(self):
4047+
"""
4048+
Test serialization/deserialization for SearchResultAnswer
4049+
"""
4050+
4051+
# Construct a json representation of a SearchResultAnswer model
4052+
search_result_answer_model_json = {}
4053+
search_result_answer_model_json['text'] = 'testString'
4054+
search_result_answer_model_json['confidence'] = 0
4055+
4056+
# Construct a model instance of SearchResultAnswer by calling from_dict on the json representation
4057+
search_result_answer_model = SearchResultAnswer.from_dict(search_result_answer_model_json)
4058+
assert search_result_answer_model != False
4059+
4060+
# Construct a model instance of SearchResultAnswer by calling from_dict on the json representation
4061+
search_result_answer_model_dict = SearchResultAnswer.from_dict(search_result_answer_model_json).__dict__
4062+
search_result_answer_model2 = SearchResultAnswer(**search_result_answer_model_dict)
4063+
4064+
# Verify the model instances are equivalent
4065+
assert search_result_answer_model == search_result_answer_model2
4066+
4067+
# Convert model instance back to dict and verify no loss of data
4068+
search_result_answer_model_json2 = search_result_answer_model.to_dict()
4069+
assert search_result_answer_model_json2 == search_result_answer_model_json
4070+
40364071
class TestModel_SearchResultHighlight():
40374072
"""
40384073
Test Class for SearchResultHighlight
@@ -4567,13 +4602,18 @@ def test_runtime_response_generic_runtime_response_type_search_serialization(sel
45674602
search_result_highlight_model['url'] = ['testString']
45684603
search_result_highlight_model['foo'] = ['testString']
45694604

4605+
search_result_answer_model = {} # SearchResultAnswer
4606+
search_result_answer_model['text'] = 'testString'
4607+
search_result_answer_model['confidence'] = 0
4608+
45704609
search_result_model = {} # SearchResult
45714610
search_result_model['id'] = 'testString'
45724611
search_result_model['result_metadata'] = search_result_metadata_model
45734612
search_result_model['body'] = 'testString'
45744613
search_result_model['title'] = 'testString'
45754614
search_result_model['url'] = 'testString'
45764615
search_result_model['highlight'] = search_result_highlight_model
4616+
search_result_model['answers'] = [search_result_answer_model]
45774617

45784618
response_generic_channel_model = {} # ResponseGenericChannel
45794619
response_generic_channel_model['channel'] = 'testString'

0 commit comments

Comments
 (0)