理解 YAML

发布: (2026年1月3日 GMT+8 20:50)
5 分钟阅读
原文: Dev.to

Source: Dev.to

请提供您希望翻译的正文内容,我将把它翻译成简体中文并保持原有的格式、Markdown 语法以及技术术语不变。

内容

  • YAML
  • 意图
  • 实体
  • 槽位

YAML (or YML)

YAML 是一种标记语言。与 HTML 或 XML 不同,它并不用于网站设计;在 RASA 中,它构成了所有配置文件的 结构基础

  • YAML 通过 缩进 来创建层级块。
  • 可以把 type 看作是包含其子类型(例如 intents、slots、entities)的超类。

常见块结构

- type:
    examples: |
      - e.g. 1
      - e.g. 2
      - e.g. 3

此模式常用于使用示例短语定义 intentsslotsentities

意图

意图 捕获用户想要表达的内容——消息背后的目的。RASA 的 NLU 组件将许多相同意义的表达映射到单一意图。

示例意图

用户话语映射意图
我觉得我胃疼intent_symptom_stomach_ache
我的胃疼intent_symptom_stomach_ache
我可能有腹痛intent_symptom_stomach_ache

意图在流水线中的流动

[User query] → [NLU: intent & entity prediction] → [Dialogue manager] → [Bot response]

预测的 JSON 表示

{
  "intent": {
    "name": "report_symptom",
    "confidence": 0.92
  },
  "entities": [
    {"entity": "symptom", "value": "cough"},
    {"entity": "duration", "value": "two days"}
  ]
}

示例 NLU 文件 (nlu.yml)

version: "3.1"

nlu:
  - intent: greet
    examples: |
      - hello
      - hi
      - good morning

  - intent: report_symptom
    examples: |
      - I have a headache
      - My head hurts
      - I've been coughing for two days

domain.yml 中声明意图

intents:
  - greet
  - goodbye
  - affirm
  - deny
  - mood_great
  - mood_unhappy
  - report_symptom

Source:

实体

虽然意图回答用户想要的 什么,但 实体 提供了在话语中提到的 具体数值

  • 实体使机器人能够提供详细的、上下文感知的信息。
  • 它们由 NLU 组件提取,并传递给对话管理器。

实体提取示例(JSON)

{
  "entities": [
    {"entity": "symptom", "value": "fever"},
    {"entity": "duration", "value": "three days"}
  ]
}

nlu.yml 中定义实体

nlu:
  - intent: report_symptom
    examples: |
      - I have a [fever](symptom)
      - I've been coughing for [two days](duration)
      - My [head](body_part) hurts

实体类型

  • Word(自由文本)
  • Categorical(预定义列表)
  • Numerical(数字、日期等)
  • Lookup / Regex(基于模式)

Slots

Slots 是 RASA 的记忆系统。

  • 实体单个 用户消息中提取。
  • 槽位 在多个回合之间保持,允许机器人保留上下文。

为什么需要槽位

User: I have a fever.
Bot:  How long have you had it?
User: Three days.

机器人必须记住症状是 发烧,同时询问 持续时间。槽位使这种连续性成为可能;如果没有槽位,每一次对话都会被视为孤立的输入。

槽位流程图

[User input] → [Intent & Entity extraction] → [Slot filling / update] → [Dialogue manager (policies)] → [Bot response]

domain.yml 中定义槽位

slots:
  symptom:
    type: text
    influence_conversation: true
  duration:
    type: text
    influence_conversation: true

当实体被提取后,RASA 可以自动将其映射到相应的槽位(如果名称匹配)。这种映射桥接了 NLU 输出与对话管理器的决策过程。

TL;DR

  • YAML 为所有 RASA 配置文件提供结构。
  • Intents 捕获用户的目标。
  • Entities 捕获话语中的具体信息片段。
  • Slots 在多轮对话中存储这些信息,使对话具备上下文关联性和连贯性。

理解这些构建块是继续学习 Stories、Rules 和 Policies 的前提,这些组件根据 intents、entities 和 slots 来编排机器人的响应。

JSON 表示中的槽位

{
  "slots": {
    "symptom": "fever",
    "duration": "three days"
  }
}

槽位类型

  • 文本槽 – 存储原始字符串(例如,症状、姓名)
  • 分类槽 – 将值限制在预定义集合中(例如,轻度 / 中度 / 重度
  • 布尔槽 – 真/假 标记(例如,emergency_present
  • 浮点/整数槽 – 数值,例如年龄或剂量
  • 列表槽 – 存储多个值(例如,多种症状)

下一篇博客:即将发布。

Back to Blog

相关文章

阅读更多 »

RGB LED 支线任务 💡

markdown !Jennifer Davishttps://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%...

Mendex:我为何构建

介绍 大家好。今天我想分享一下我是谁、我在构建什么以及为什么。 早期职业生涯与倦怠 我在 17 年前开始我的 developer 生涯……