[A-00094]pythonでDialogflow API(GCP)を使用する
pythonでdialogflow APIを使用する方法を記載する。
公式ドキュメントはこちら
https://cloud.google.com/dialogflow/es/docs/quick/api?hl=ja
・セットアップ
pythonライブラリをインストールします。
pip install google-cloud-dialogflow
・作成したChatbotと会話してみる。
前述した公式ドキュメントのガイドでDialogflow EX エージェントの作成を実施してください。
[What is your name ?]→[My name is Dialogflow!]と応答するインテントを作成したエージェントに定義しておいてください
サンプルソースは下記のとおりです。
def detect_intent_texts(project_id, session_id, texts, language_code):
from google.cloud import dialogflow
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
print("Session path: {}\n".format(session))
text_input = dialogflow.TextInput(
text=texts, language_code=language_code)
query_input = dialogflow.QueryInput(text=text_input)
response = session_client.detect_intent(
request={"session": session, "query_input": query_input}
)
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))
if __name__ == "__main__":
detect_intent_texts("<project_id>"
, "123456789"
, "What is your name?"
, "en-US")
(.venv)MacBook-Pro:test2_project$ python dialogflowtest.py
Session path: projects/soy-braid-298113/agent/sessions/123456789
Detected intent: get-agent-name (confidence: 1.0)
Fulfillment text: My name is Dialogflow!
上記の通り、Dialogflowエージェントが応答しました。
コメントを残す