28
28
29
29
30
30
# [START dialogflow_detect_intent_knowledge]
31
- def detect_intent_knowledge (project_id , session_id , language_code ,
32
- knowledge_base_id , texts ):
31
+ def detect_intent_knowledge (
32
+ project_id , session_id , language_code , knowledge_base_id , texts
33
+ ):
33
34
"""Returns the result of detect intent with querying Knowledge Connector.
34
35
35
36
Args:
@@ -41,69 +42,79 @@ def detect_intent_knowledge(project_id, session_id, language_code,
41
42
texts: A list of text queries to send.
42
43
"""
43
44
from google .cloud import dialogflow_v2beta1 as dialogflow
45
+
44
46
session_client = dialogflow .SessionsClient ()
45
47
46
48
session_path = session_client .session_path (project_id , session_id )
47
- print (' Session path: {}\n ' .format (session_path ))
49
+ print (" Session path: {}\n " .format (session_path ))
48
50
49
51
for text in texts :
50
- text_input = dialogflow .TextInput (
51
- text = text , language_code = language_code )
52
+ text_input = dialogflow .TextInput (text = text , language_code = language_code )
52
53
53
54
query_input = dialogflow .QueryInput (text = text_input )
54
55
55
- knowledge_base_path = dialogflow .KnowledgeBasesClient \
56
- .knowledge_base_path (project_id , knowledge_base_id )
56
+ knowledge_base_path = dialogflow .KnowledgeBasesClient .knowledge_base_path (
57
+ project_id , knowledge_base_id
58
+ )
57
59
58
60
query_params = dialogflow .QueryParameters (
59
- knowledge_base_names = [knowledge_base_path ])
61
+ knowledge_base_names = [knowledge_base_path ]
62
+ )
60
63
61
64
request = dialogflow .DetectIntentRequest (
62
- session = session_path ,
63
- query_input = query_input ,
64
- query_params = query_params
65
+ session = session_path , query_input = query_input , query_params = query_params
65
66
)
66
67
response = session_client .detect_intent (request = request )
67
68
68
- print ('=' * 20 )
69
- print ('Query text: {}' .format (response .query_result .query_text ))
70
- print ('Detected intent: {} (confidence: {})\n ' .format (
71
- response .query_result .intent .display_name ,
72
- response .query_result .intent_detection_confidence ))
73
- print ('Fulfillment text: {}\n ' .format (
74
- response .query_result .fulfillment_text ))
75
- print ('Knowledge results:' )
69
+ print ("=" * 20 )
70
+ print ("Query text: {}" .format (response .query_result .query_text ))
71
+ print (
72
+ "Detected intent: {} (confidence: {})\n " .format (
73
+ response .query_result .intent .display_name ,
74
+ response .query_result .intent_detection_confidence ,
75
+ )
76
+ )
77
+ print ("Fulfillment text: {}\n " .format (response .query_result .fulfillment_text ))
78
+ print ("Knowledge results:" )
76
79
knowledge_answers = response .query_result .knowledge_answers
77
80
for answers in knowledge_answers .answers :
78
- print (' - Answer: {}' .format (answers .answer ))
79
- print (' - Confidence: {}' .format (
80
- answers .match_confidence ))
81
+ print (" - Answer: {}" .format (answers .answer ))
82
+ print (" - Confidence: {}" .format (answers .match_confidence ))
83
+
84
+
81
85
# [END dialogflow_detect_intent_knowledge]
82
86
83
87
84
- if __name__ == ' __main__' :
88
+ if __name__ == " __main__" :
85
89
parser = argparse .ArgumentParser (
86
- description = __doc__ ,
87
- formatter_class = argparse . RawDescriptionHelpFormatter )
90
+ description = __doc__ , formatter_class = argparse . RawDescriptionHelpFormatter
91
+ )
88
92
parser .add_argument (
89
- '--project-id' , help = 'Project/agent id. Required.' , required = True )
93
+ "--project-id" , help = "Project/agent id. Required." , required = True
94
+ )
90
95
parser .add_argument (
91
- ' --session-id' ,
92
- help = ' ID of the DetectIntent session. '
93
- 'Defaults to a random UUID.' ,
94
- default = str ( uuid . uuid4 ()) )
96
+ " --session-id" ,
97
+ help = " ID of the DetectIntent session. " "Defaults to a random UUID." ,
98
+ default = str ( uuid . uuid4 ()) ,
99
+ )
95
100
parser .add_argument (
96
- ' --language-code' ,
101
+ " --language-code" ,
97
102
help = 'Language code of the query. Defaults to "en-US".' ,
98
- default = 'en-US' )
103
+ default = "en-US" ,
104
+ )
99
105
parser .add_argument (
100
- '--knowledge-base-id' ,
101
- help = 'The id of the Knowledge Base to query against' ,
102
- required = True )
103
- parser .add_argument ('texts' , nargs = '+' , type = str , help = 'Text inputs.' )
106
+ "--knowledge-base-id" ,
107
+ help = "The id of the Knowledge Base to query against" ,
108
+ required = True ,
109
+ )
110
+ parser .add_argument ("texts" , nargs = "+" , type = str , help = "Text inputs." )
104
111
105
112
args = parser .parse_args ()
106
113
107
- detect_intent_knowledge (args .project_id , args .session_id ,
108
- args .language_code , args .knowledge_base_id ,
109
- args .texts )
114
+ detect_intent_knowledge (
115
+ args .project_id ,
116
+ args .session_id ,
117
+ args .language_code ,
118
+ args .knowledge_base_id ,
119
+ args .texts ,
120
+ )
0 commit comments