首页 > 解决方案 > Dialogflow v2 Beta 1 使用 Python 更新意图

问题描述

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

我阅读了这个文档并在 GitHub 上检查了这个源文件,我认为我收到错误的原因是因为我不理解这部分源代码:

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/*'.

标签: dialogflow-esgrpcgoogle-api-python-client

解决方案


您可以通过使用获得要正确修改的意图

name = client.intent_path(PROJECT_NAME, your_intent_id)

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

这是一个将意图 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)

您的代码中还需要 ope extra import:

from google.protobuf import field_mask_pb2

这样,intent 的 display_name 就会改变。
您也可以对其他属性执行相同操作。请记住按照此文档传递属性期望的值,您也可以从此问题中获得帮助。

希望能帮助到你。


推荐阅读