You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.2 KiB

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