亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Python中使用dialogflow客戶端

如何在Python中使用dialogflow客戶端

慕姐8265434 2023-09-05 20:21:15
我正在嘗試的是在 python 中獲得響應import dialogflowfrom google.api_core.exceptions import InvalidArgumentDIALOGFLOW_PROJECT_ID = 'imposing-fx-333333'DIALOGFLOW_LANGUAGE_CODE = 'en'GOOGLE_APPLICATION_CREDENTIALS = 'imposing-fx-333333-e6e3cb9e4adb.json'text_to_be_analyzed = "Hi! I'm David and I'd like to eat some sushi, can you help me?"session_client = dialogflow.SessionsClient()session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID)text_input = dialogflow.types.TextInput(text=text_to_be_analyzed, language_code=DIALOGFLOW_LANGUAGE_CODE)query_input = dialogflow.types.QueryInput(text=text_input)try:    response = session_client.detect_intent(session=session, query_input=query_input)except InvalidArgument:    raiseprint("Query text:", response.query_result.query_text)print("Detected intent:", response.query_result.intent.display_name)print("Detected intent confidence:", response.query_result.intent_detection_confidence)print("Fulfillment text:", response.query_result.fulfillment_text)我無法驗證憑據google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started這是我在 stackoverflow 中的第一個問題:)我知道我已經做了很多
查看完整描述

2 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

您需要從 導出服務帳戶密鑰 (JSON)文件,并將環境變量設置GOOGLE_APPLICATION_CREDENTIALS為包含服務帳戶密鑰的 JSON 文件的文件路徑。然后你就可以調用dialogflow了。


獲取服務帳戶密鑰的步驟: 確保您使用的是 Dialogflow v2。轉到常規設置并單擊您的服務帳戶。這會將您重定向到 Google Cloud Platform 項目的服務帳戶頁面。下一步是為服務帳戶創建新密鑰。現在創建一個服務帳戶并選擇 JSON 作為輸出密鑰。按照說明操作,JSON 文件將下載到您的計算機。該文件將用作GOOGLE_APPLICATION_CREDENTIALS.


現在在代碼中,


import os

import dialogflow

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/path/to/file.json"

project_id = "your_project_id"

session_id = "your_session_id"

language_code = "en"

session_client = dialogflow.SessionsClient()

session = session_client.session_path(project_id, session_id)


text_input = dialogflow.types.TextInput(text=text, language_code=language_code)

query_input = dialogflow.types.QueryInput(text=text_input)

response_dialogflow = session_client.detect_intent(session=session, query_input=query_input)



查看完整回答
反對 回復 2023-09-05
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

如果您想從文件系統中獲取文件,此方法也適用。推薦的方法是使用環境變量



    import json

    from google.cloud import dialogflow_v2

    from google.oauth2 import *

session_client = None

dialogflow_key = None

creds_file = "/path/to/json/file.json"


dialogflow_key = json.load(open(creds_file))

credentials = (service_account.Credentials.from_service_account_info(dialogflow_key))


session_client = dialogflow_v2.SessionsClient(credentials=credentials)


print("it works : " + session_client.DEFAULT_ENDPOINT) if session_client is not None 

else print("does not work")


我忘記添加主要文章抱歉...這里是:


https://googleapis.dev/python/google-auth/latest/user-guide.html#service-account-private-key-files


查看完整回答
反對 回復 2023-09-05
  • 2 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號