You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python SDK创建Dialogflow意图时如何自动标注实体值?

How to Auto-Annotate Entities When Creating Intents with Dialogflow Python SDK in Example Mode

我之前也踩过这个坑,一开始对user_defined参数的理解完全错了!这个参数根本不是用来触发自动标注的,而是用来区分用户自定义实体和Dialogflow自带的系统实体的,所以你手动标注时改它的值当然没区别。

要让Dialogflow自动帮你标注实体,核心是不要手动干预标注过程,让它自己去匹配已有的实体库,具体步骤和注意事项如下:

关键前提

你要自动识别的实体必须已经存在于Dialogflow中:

  • 系统实体(比如@sys.date、@sys.number)可以直接用,不需要额外创建
  • 自定义实体需要先在Dialogflow控制台或者通过SDK创建好,并且添加足够的实体条目

正确的实现方式

添加训练短语时,不要手动拆分Part去标注,直接传入完整的用户问题文本就行。Dialogflow会自动扫描文本,匹配已有的实体并完成标注。

举个例子:

from google.cloud import dialogflow_v2 as dialogflow

def create_intent_with_auto_annotation(project_id, display_name, training_phrases_parts):
    intents_client = dialogflow.IntentsClient()
    parent = dialogflow.AgentsClient.agent_path(project_id)
    
    training_phrases = []
    for phrase_text in training_phrases_parts:
        # 直接传入完整文本,不手动拆分标注
        part = dialogflow.Intent.TrainingPhrase.Part(text=phrase_text)
        training_phrase = dialogflow.Intent.TrainingPhrase(parts=[part])
        training_phrases.append(training_phrase)
    
    intent = dialogflow.Intent(
        display_name=display_name,
        training_phrases=training_phrases
    )
    
    response = intents_client.create_intent(
        request={"parent": parent, "intent": intent}
    )
    print(f"Intent created successfully: {response.display_name}")

# 调用示例:传入完整的用户问题,让Dialogflow自动标注
create_intent_with_auto_annotation(
    project_id="your-project-id",
    display_name="BookFlight",
    training_phrases_parts=[
        "我要预订明天的机票",
        "帮我订下周六去上海的航班",
        "想订一张下周三出发的商务舱"
    ]
)

为什么之前的手动标注没用?

当你手动用Part指定entity_type时,user_defined只是告诉Dialogflow这个实体是你自己定义的(True)还是系统自带的(False),完全不影响自动标注逻辑——因为你已经手动指定了标注内容,Dialogflow不会再去自动识别了。

额外注意事项

如果Dialogflow没有自动标注你期望的实体,可能是这几个原因:

  • 自定义实体的条目太少,Dialogflow无法准确匹配
  • 训练短语里的实体表述和实体库的条目差异太大
  • 实体类型没有正确关联到意图

这时候你可以先手动标注1-2个示例训练短语,让Dialogflow学习到匹配模式,之后再添加的训练短语就会自动标注了。

内容的提问来源于stack exchange,提问作者wphw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:30:06