python - DialogFlow 检测意图文本

标签 python google-cloud-platform chatbot dialogflow-es

我正在将 DF 与 Python API 结合使用,这是代码,但我无法使用检测意图文本。

如果我使用第二行,我会收到下一个错误: google.api_core.exceptions.PermissionDenied:403“projects/newagent/agent”上的 IAM 权限“dialogflow.sessions.detectIntent”被拒绝。

如果我使用第一个: google.api_core.exceptions.InvalidArgument:400资源名称“projects/newagent/agent/environments/draft/users//agent/sessions/5276b6d4-a0b6-4e91-84d3-16512d1f3299”与“projects//agent”不匹配/environments//users//sessions/'。

我已在 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")

最佳答案

session ID 的格式应为 projects/<Project ID>/agent/sessions/<Session ID> (<Project ID> 您的代理所在的 GCP 项目的 ID 和 <Session ID> 您用于正在进行的 session 的 ID),如 this documentation page 中所示。 .

在您的代码中,我看到您正在调用 detect_intent_texts()功能如下:

project_id = "newagent/agent/environments/draft/users/<User Number>"
session_id = str(uuid.uuid4())
texts = "Que tal?"
language_code = "es-ES"

我在这里看到两个主要错误:

  1. 项目 ID 格式错误,应该是您的 GCP 项目的 ID,通常格式为 my-first-project或类似的,和斜杠 /不支持,因此您使用了错误的 Project ID .
  2. 文本应该是 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 中运行意图,您需要使用具有以下角色之一的服务帐户:

enter image description here

Dialogflow API AdminDialogflow API Client 可以查询意图,因此,需要其中之一才能发出您尝试执行的请求类型用你的脚本。

我看到您说您的用户对该项目拥有所有者权限。但是,问题可能是您使用了错误的服务帐户。为了正确设置身份验证,请按照 the steps detailed in the docs 操作。总之,您必须创建一个具有正确权限的服务帐户,下载其 JSON key ,并通过运行命令 export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/key.json" 将其用作环境变量。无论您在何处运行脚本。

关于python - DialogFlow 检测意图文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50217133/

相关文章:

python - 使用 minit ='matrix' 时,scipy 的 kmeans2 算法是否也会权衡初始质心集?

google-app-engine - 将 Go 应用程序部署到 Google App Engine 时出错

google-cloud-platform - 如何指定根文件夹以使用 Cloud SDK 部署应用程序?

node.js - Facebook Messenger Bot - 如何使用 NodeJS 获取用户信息?

python - 如何在 AWS Lex 中添加多个响应

python - 匹配两个 Python 字符串中的字符

python - 是否可以用每组中的观察次数来注释 seaborn fiddle 图?

python - Pandas - 关于应用功能缓慢的解释

kubernetes - Kubernetes集群从Google Container Engine中消失了

c# - 可以使用 C# 检索本地存储值