-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
detect_intent_texts.py
92 lines (73 loc) · 2.98 KB
/
detect_intent_texts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# Copyright 2017 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""DialogFlow API Detect Intent Python sample with text inputs.
Examples:
python detect_intent_texts.py -h
python detect_intent_texts.py --project-id PROJECT_ID \
--session-id SESSION_ID \
"hello" "book a meeting room" "Mountain View"
python detect_intent_texts.py --project-id PROJECT_ID \
--session-id SESSION_ID \
"tomorrow" "10 AM" "2 hours" "10 people" "A" "yes"
"""
import argparse
import uuid
# [START dialogflow_es_detect_intent_text]
def detect_intent_texts(project_id, session_id, texts, language_code):
"""Returns the result of detect intent with texts as inputs.
Using the same `session_id` between requests allows continuation
of the conversation."""
from google.cloud import dialogflow
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
print("Session path: {}\n".format(session))
for text in texts:
text_input = dialogflow.TextInput(text=text, language_code=language_code)
query_input = dialogflow.QueryInput(text=text_input)
response = session_client.detect_intent(
request={"session": session, "query_input": query_input}
)
print("=" * 20)
print("Query text: {}".format(response.query_result.query_text))
print(
"Detected intent: {} (confidence: {})\n".format(
response.query_result.intent.display_name,
response.query_result.intent_detection_confidence,
)
)
print("Fulfillment text: {}\n".format(response.query_result.fulfillment_text))
# [END dialogflow_es_detect_intent_text]
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"--project-id", help="Project/agent id. Required.", required=True
)
parser.add_argument(
"--session-id",
help="Identifier of the DetectIntent session. " "Defaults to a random UUID.",
default=str(uuid.uuid4()),
)
parser.add_argument(
"--language-code",
help='Language code of the query. Defaults to "en-US".',
default="en-US",
)
parser.add_argument("texts", nargs="+", type=str, help="Text inputs.")
args = parser.parse_args()
detect_intent_texts(
args.project_id, args.session_id, args.texts, args.language_code
)