python - DialogFlow 检测意图文本
问题描述
我正在使用带有 Python API 的 DF,这是代码,我不能使用检测意图文本。
如果我使用第二行,我会收到下一个错误:google.api_core.exceptions.PermissionDenied: 403 IAM permission 'dialogflow.sessions.detectIntent' on 'projects/newagent/agent' denied。
如果我使用第一个: google.api_core.exceptions.InvalidArgument: 400 Resource name 'projects/newagent/agent/environments/draft/users//agent/sessions/5276b6d4-a0b6-4e91-84d3-16512d1f3299' 不匹配'项目//代理/环境//用户//会话/ '。
我已在 Google Cloud 上启用结算功能,并且用户拥有所有者权限。出了什么问题?
def detect_intent_texts(project_id, session_id, texts, language_code):
session_client = dialogflow_v2.SessionsClient()
#----------------------------------------------------------Lines that I talk about in the question---------------------------------------------------------------------------------------------------
#session = session_client.session_path(project_id, session_id)
session = "projects/newagent/agent/environments/draft/users/<user id>/sessions/6344a857-9de5-406c-ba0f-c71b7b3ffdba"
#----------------------------------------------------------Lines that I talk about in the question---------------------------------------------------------------------------------------------------
for text in texts:
text_input = dialogflow_v2.types.TextInput(text=text, language_code=language_code)
query_input = dialogflow_v2.types.QueryInput(text=text_input)
response = session_client.detect_intent(session=session, query_input=query_input)
detect_intent_texts("newagent/agent/environments/draft/users/<User Number>",str(uuid.uuid4()),"Que tal?","es-ES")
解决方案
会话 ID 应具有格式projects/<Project ID>/agent/sessions/<Session ID>
(<Project ID>
即您的代理所在的 GCP 项目<Session ID>
的 ID 以及您用于正在进行的会话的 ID),如本文档页面所示。
在您的代码中,我看到您正在调用如下detect_intent_texts()
函数:
project_id = "newagent/agent/environments/draft/users/<User Number>"
session_id = str(uuid.uuid4())
texts = "Que tal?"
language_code = "es-ES"
我在这里看到两个主要错误:
- Project ID 格式错误,应该是您的 GCP 项目的 ID,通常格式类似
my-first-project
或类似,并且不支持斜杠/
,所以您使用了错误的Project ID。 - 文本应该是一个 Python 字符串列表,例如
["hello"]
,而不仅仅是"hello"
.
仅作为示例,以下最小代码提供以下结果:
import dialogflow
def detect_intent_texts(project_id, session_id, texts, language_code):
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.types.TextInput(text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(session=session, query_input=query_input)
print('Fulfillment text: {}\n'.format(response.query_result.fulfillment_text))
detect_intent_texts("my-project","abcd",["hello"],"en-US")
结果:
user@my-project:~/dialogflow$ python detect_intent_minimal.py
Session path: projects/my-project/agent/sessions/abcd
Fulfillment text: Hi!
因此,我怀疑将 更改project_id
为正确的值并将更改texts
为列表应该可以解决您的问题。
编辑:
我已经能够403 PermissionDenied
通过使用没有所需权限的服务帐户来重现您在消息中看到的问题。
为了在 Dialogflow 中运行意图,您需要使用具有以下角色之一的服务帐户:
Dialogflow API Admin和Dialogflow API Client可以查询意图,因此,需要其中之一才能发出您尝试使用脚本执行的请求类型。
我看到您说您的用户对该项目具有所有者权限。但是,问题可能是您使用了错误的服务帐户。为了正确设置身份验证,请按照文档中详述的步骤进行操作。总之,您必须创建一个具有正确权限的服务帐户,下载其 JSON 密钥,并通过在export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/key.json"
运行脚本的任何位置运行命令将其用作环境变量。
推荐阅读
- wso2 - 如何在 WSO2 EI 属性调解器中为 SYSTEM_DATE 添加一天?
- memory-leaks - LiteDB 高内存使用 - 插入时 OOM
- mysql - 我如何以及在哪里可以更改 time_out
- flutter - 我想在特定时间间隔内在 Flutter 应用程序的同一屏幕上打开和关闭相机
- javascript - 解释这个 JavaScript 递归
- flask - 如何在运行时中止下载 API
- java - Kubernetes 中 hazelcast 的 NoClassDefFoundError 和 DuplicateInstanceNameException
- php - 如果我将消息发送给另一个用户,那么我希望在 php 和 MySQL 中看到该消息
- python - python 3,NamedTemporaryFile,权限被拒绝
- shell - 如何使用 shell 从多个文件中获取平均列数?