Dialogflow v2 Beta 1 更新意图 Python

Dialogflow v2 Beta 1 Update Intent with Python

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

我在 GitHub 上阅读了 this documentation and checked this source file,我认为我收到错误的原因是因为我不理解源代码的这一部分:

Args: intent (Union[dict, ~google.cloud.dialogflow_v2beta1.types.Intent]): Required. The intent to update. Format: projects/<Project ID>/agent/intents/<Intent ID>. If a dict is provided, it must be of the same form as the protobuf message :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/*'.

您可以使用

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

您将获得完整的意图定义。
然后您需要通过访问它们并分配您的值来更改此意图的值。
之后,您需要在 update_intent() 函数中传递相同的意图。
还建议使用 update_mask 以避免更改任何其他字段或设置其余字段 None。

这是一个将意图 display_name 从 greet 更新为 hello 的示例:

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

这样,意图的 display_name 就会改变。
您也可以对其余属性执行相同操作。请记住通过遵循 this documentation and you can take help from this issue 来传递 属性 期望的值。

希望对您有所帮助。