Dialogflow v2 api在Python中的身份验证失败

2024-09-28 21:29:10 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我的代码可以在控制台上工作,但是如果我试图导入模块或在web上运行代码,我会得到google.api_core.exceptions.ServiceUnavailable: 503 Deadline Exceeded错误。在

def detect_intent_texts(project_id, session_id, texts, language_code):
    """Returns the result of detect intent with texts as inputs.

    Using the same `session_id` between requests allows continuation
    of the conversaion."""
    import dialogflow_v2 as dialogflow
    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('=' * 20)
        print('Query text: {}'.format(response.query_result.query_text))
        print('Detected intent: {} (confidence: {})\n'.format(
            response.query_result.intent.display_name,
            response.query_result.intent_detection_confidence))


print (detect_intent_texts('newagent-38239', '12121211', ['Hi there'], 'en-US'))

我有身份验证设置和环境变量GOOGLE_APPLICATION_CREDENTIALS设置。正如我前面所说的,代码可以在控制台上运行,或者如果我尝试运行python main.py之类的程序。如果尝试导入模块或在web服务器上运行它,则失败:


Tags: 代码textidinputresponsesessioncoderesult