2828
2929
3030# [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+ ):
3334 """Returns the result of detect intent with querying Knowledge Connector.
3435
3536 Args:
@@ -41,69 +42,79 @@ def detect_intent_knowledge(project_id, session_id, language_code,
4142 texts: A list of text queries to send.
4243 """
4344 from google .cloud import dialogflow_v2beta1 as dialogflow
45+
4446 session_client = dialogflow .SessionsClient ()
4547
4648 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 ))
4850
4951 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 )
5253
5354 query_input = dialogflow .QueryInput (text = text_input )
5455
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+ )
5759
5860 query_params = dialogflow .QueryParameters (
59- knowledge_base_names = [knowledge_base_path ])
61+ knowledge_base_names = [knowledge_base_path ]
62+ )
6063
6164 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
6566 )
6667 response = session_client .detect_intent (request = request )
6768
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:" )
7679 knowledge_answers = response .query_result .knowledge_answers
7780 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+
8185# [END dialogflow_detect_intent_knowledge]
8286
8387
84- if __name__ == ' __main__' :
88+ if __name__ == " __main__" :
8589 parser = argparse .ArgumentParser (
86- description = __doc__ ,
87- formatter_class = argparse . RawDescriptionHelpFormatter )
90+ description = __doc__ , formatter_class = argparse . RawDescriptionHelpFormatter
91+ )
8892 parser .add_argument (
89- '--project-id' , help = 'Project/agent id. Required.' , required = True )
93+ "--project-id" , help = "Project/agent id. Required." , required = True
94+ )
9095 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+ )
95100 parser .add_argument (
96- ' --language-code' ,
101+ " --language-code" ,
97102 help = 'Language code of the query. Defaults to "en-US".' ,
98- default = 'en-US' )
103+ default = "en-US" ,
104+ )
99105 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." )
104111
105112 args = parser .parse_args ()
106113
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