首页 > 解决方案 > 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")

标签: pythongoogle-cloud-platformchatbotdialogflow-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"

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

  1. Project 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 中运行意图,您需要使用具有以下角色之一的服务帐户:

在此处输入图像描述

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

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


推荐阅读