使用Python进行Dialogflow v2 Beta 1更新的意图

时间:2019-03-13 07:35:24

标签: dialogflow grpc google-api-python-client

我迷路了。 我的项目中目前有一个Intent,并且我试图以编程方式更新所有字段,因为这是我的项目所需要的。

我阅读了this文档,并在GitHub上检查了this source file,我认为出现错误的原因是因为我不理解源代码的这一部分:

  

Args:               目的(Union [dict,〜google.cloud.dialogflow_v2beta1.types.Intent]):必填。意图   更新。                   格式:projects/<Project ID>/agent/intents/<Intent ID>。                   如果提供了dict,则其格式必须与protobuf相同                   消息:class:~google.cloud.dialogflow_v2beta1.types.Intent

(供参考的第484行)

该平台运行良好,我只是不知道我在这里缺少什么。

我的代码

from constants import *
from google.oauth2 import service_account
import dialogflow_v2beta1
cred = service_account.Credentials.from_service_account_file(AUTH_JSON)
client = dialogflow_v2beta1.IntentsClient(credentials=cred)
params = dialogflow_v2beta1.types.Intent.Parameter(name='test', display_name='test', value='test', is_list=True)
t = dialogflow_v2beta1.types.Intent.Message.Text(text='TEST TEXT')
m = dialogflow_v2beta1.types.Intent.Message(text=t)
p = dialogflow_v2beta1.types.Intent.TrainingPhrase.Part(text='test',entity_type='@test_type', alias='test_alias', user_defined=True)
t = dialogflow_v2beta1.types.Intent.TrainingPhrase(name='test',type=2, parts=[p])
modified_intent = dialogflow_v2beta1.types.Intent(
    display_name='test',       
    messages=[m],
    webhook_state=1,
    is_fallback=False,
    ml_disabled=False,
    input_context_names=PROJECT_DIR+'agent/sessions/-/contexts/' + 'TEST_CONTEXT',
    events='TESTING EVENT',
    training_phrases=[t],
    action='TESTING ACTION',
    reset_contexts=False,
    parameters=[params]
    ) 
name = client.intent_path(PROJECT_NAME, '7b8f2105-53d4-4724-8d4c-0170b8db7028')
intent = client.get_intent(name)
client.update_intent(intent=modified_intent, language_code=LANGUAGE_CODE, intent_view=0)

完整的错误消息

Traceback (most recent call last):
  File "/anaconda/envs/data/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 57, in error_remapped_callable
    return callable_(*args, **kwargs)
  File "/anaconda/envs/data/lib/python3.6/site-packages/grpc/_channel.py", line 550, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "/anaconda/envs/data/lib/python3.6/site-packages/grpc/_channel.py", line 467, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
        status = StatusCode.INVALID_ARGUMENT
        details = "Resource name '' does not match 'projects/*/agent/intents/*'."
        debug_error_string = "{"created":"@1552461629.958860000","description":"Error received from peer","file":"src/core/lib/surface/call.cc","file_line":1036,"grpc_message":"Resource name '' does not match 'projects/*/agent/intents/*'.","grpc_status":3}"
>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "test.py", line 26, in <module>
    client.update_intent(intent=modified_intent, language_code=LANGUAGE_CODE, intent_view=0)
  File "/anaconda/envs/data/lib/python3.6/site-packages/dialogflow_v2beta1/gapic/intents_client.py", line 535, in update_intent
    request, retry=retry, timeout=timeout, metadata=metadata)
  File "/anaconda/envs/data/lib/python3.6/site-packages/google/api_core/gapic_v1/method.py", line 143, in __call__
    return wrapped_func(*args, **kwargs)
  File "/anaconda/envs/data/lib/python3.6/site-packages/google/api_core/retry.py", line 270, in retry_wrapped_func
    on_error=on_error,
  File "/anaconda/envs/data/lib/python3.6/site-packages/google/api_core/retry.py", line 179, in retry_target
    return target()
  File "/anaconda/envs/data/lib/python3.6/site-packages/google/api_core/timeout.py", line 214, in func_with_timeout
    return func(*args, **kwargs)
  File "/anaconda/envs/data/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc), exc)
  File "<string>", line 3, in raise_from
google.api_core.exceptions.InvalidArgument: 400 Resource name '' does not match 'projects/*/agent/intents/*'.

1 个答案:

答案 0 :(得分:1)

您可以使用

来获取要正确修改的意图。
name = client.intent_path(PROJECT_NAME, your_intent_id)

您将完全了解自己的意图。
然后,您需要通过访问它们并分配您的值来更改此意图的值。
之后,您需要在update_intent()函数中传递相同的意图。
还建议使用update_mask来避免更改任何其他字段或将其余字段设置为“无”。

以下是将意图显示名称从问候更新为你好的示例:

client = dialogflow.IntentsClient()
intent_name = client.intent_path(project_id, intent_id)
intent = client.get_intent(intent_name, intent_view=dialogflow.enums.IntentView.INTENT_VIEW_FULL)
intent.display_name = 'hello'
update_mask = field_mask_pb2.FieldMask(paths=['display_name']) 
print(response)

您还需要在代码中添加其他操作:

from google.protobuf import field_mask_pb2

这样,意图的显示名称将被更改。
您也可以对其余属性执行相同的操作。只要记住遵循this documentation来传递属性期望的值,您就可以从this issue那里寻求帮助。

希望有帮助。