【问题标题】:DialogFlow detectIntentTextDialogFlow 检测意图文本
【发布时间】:2018-05-07 14:57:41
【问题描述】:

我正在使用带有 Python API 的 DF,这是代码,我无法使用检测意图文本。

如果我使用第二行,我会收到下一个错误: google.api_core.exceptions.PermissionDenied:“projects/newagent/agent”上的 403 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")

【问题讨论】:

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


    【解决方案1】:

    会话 ID 的格式应为 projects/&lt;Project ID&gt;/agent/sessions/&lt;Session ID&gt;(&lt;Project ID&gt; 是您的代理所在的 GCP 项目的 ID,&lt;Session ID&gt; 是您用于正在进行的会话的 ID),如 @987654321 所示@。

    在您的代码中,我看到您正在调用 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之类的格式,不支持斜杠/,所以你用错了@987654322 @。
    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 Admin 和 Dialogflow API Client 可以查询意图,因此,需要其中之一才能发出您尝试执行的请求类型用你的脚本。

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

    【讨论】:

    • 您好,感谢您的解释,但没有奏效。我有正确的项目 ID,我只是写了 newAgent 以使一切变得更容易。全称是newAgent-bjs83。使用您发布的代码(使用 dialogflow 或 dialogflow_v2)不能解决问题我得到以下异常:google.api_core.exceptions.PermissionDenied: 403 IAM 权限 'dialogflow.sessions.detectIntent' on 'projects/newagent-b6bb7/agent '否认。任何想法的原因?非常感谢!!
    • 忘了说,在行:session_client.detect_intent(session=session, query_input=query_input)
    • 当我说你有错误的项目 ID 时,我的意思是你传递了一个格式为 newagent/agent/environments... 的字符串作为项目 ID,而它应该只是 newagent。正如您在the documentation 中看到的,正确的格式只是projects/&lt;Project ID&gt;/agent/sessions/&lt;Session ID&gt;。这就是为什么我知道构建的会话路径是错误的。如果您使用print(session) 打印它,它应该会显示类似该格式的内容。
    • 无论如何,请查看我的编辑以了解有关如何处理403 PermissionDenied 错误的更多详细信息。
    【解决方案2】:

    当一个身份调用 Google Cloud Platform API 时,Google Cloud Identity and Access Management (IAM) 要求该身份拥有适当的权限才能使用您必须为其创建自定义角色然后分配给服务帐号的资源。然后,您将使用该服务帐户调用 Google Cloud Platform API。 Here 您可以搜索 Dialogflow 并查看仅自定义角色支持 DF。这就是您拥有 google.api_core.exceptions.PermissionDenied: 403 IAM 权限“dialogflow.sessions.detectIntent”的原因。执行以下步骤:

    1. 转到您在 Google Cloud Platform 中的项目,然后选择角色,如下所示:
    2. 然后单击创建角色,插入角色名称和相关字段。然后单击添加权限并在过滤器中搜索“服务:Dialogflow”。选择您想要的权限,然后点击创建。
    3. 然后选择这个:
    4. 点击创建服务帐户和选择角色选项,键入并搜索您在第 2 步中创建的角色并保存帐户。
    5. 执行此操作:。将显示服务帐户列表。点击“创建凭据”按钮。
    6. 选择在上述步骤中创建的服务帐户并选择 JSON。然后选择创建。下载一个 JSON 文件。
    7. 将该文件添加到您的代码中:os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'yourfilename.json'
    8. 在 Dialogflow 控制台中使用您已完成上述所有步骤的 google 项目创建代理。享受:D

    【讨论】:

    • 请不要在答案(包括屏幕截图)中分享项目 ID、密钥和其他敏感信息,因为这可能会导致安全问题。
    猜你喜欢
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多