您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

开发自定义命令应用程序Develop Custom Commands applications

本操作指南文章介绍如何开发和配置自定义命令应用程序。In this how-to article, you learn how to develop and configure Custom Commands applications. 自定义命令功能有助于构建丰富的语音命令应用,这些应用已针对语音第一次交互体验进行了优化。The Custom Commands feature helps you build rich voice-command apps that are optimized for voice-first interaction experiences. 此功能最适合任务完成或命令和控制方案。The feature is best suited to task completion or command-and-control scenarios. 它特别适用于物联网 (IoT) 设备以及环境和无外设设备。It's particularly well suited for Internet of Things (IoT) devices and for ambient and headless devices.

在本文中,你将创建一个应用程序,用于打开和关闭电视,设置温度,以及设置警报。In this article, you create an application that can turn a TV on and off, set the temperature, and set an alarm. 创建这些基本命令后,你将了解用于自定义命令的以下选项:After you create these basic commands, you'll learn about the following options for customizing commands:

  • 向命令添加参数Adding parameters to commands
  • 将配置添加到命令参数Adding configurations to command parameters
  • 构建交互规则Building interaction rules
  • 为语音响应创建语言生成模板Creating language-generation templates for speech responses
  • 使用自定义语音工具Using Custom Voice tools

使用简单命令创建应用程序Create an application by using simple commands

首先创建一个空的自定义命令应用程序。Start by creating an empty Custom Commands application. 有关详细信息,请参阅 快速入门For details, refer to the quickstart. 在此应用程序中,创建一个空白项目,而不是导入项目。In this application, instead of importing a project, you create a blank project.

  1. 在 " 名称 " 框中,输入项目名称 " 智能会议室-Lite (或您选择的其他名称) 。In the Name box, enter the project name Smart-Room-Lite (or another name of your choice).

  2. 在 " 语言 " 列表中,选择 " 英语 (美国)"。In the Language list, select English (United States).

  3. 选择或创建 LUIS 资源。Select or create a LUIS resource.

    显示 "新建项目" 窗口的屏幕截图。Screenshot showing the "New project" window.

(可选) 更新 LUIS 资源Update LUIS resources (optional)

您可以更新在 " 新建项目 " 窗口中选择的创作资源。You can update the authoring resource that you selected in the New project window. 还可以设置预测资源。You can also set a prediction resource.

发布自定义命令应用程序时,预测资源用于识别。A prediction resource is used for recognition when your Custom Commands application is published. 在开发和测试阶段,无需预测资源。You don't need a prediction resource during the development and testing phases.

添加 TurnOn 命令Add a TurnOn command

在创建的空智能空间精简自定义命令应用程序中,添加命令。In the empty Smart-Room-Lite Custom Commands application you created, add a command. 命令将处理查询文本 Turn on the tvThe command will process an utterance, Turn on the tv. 它将响应消息 Ok, turning the tv onIt will respond with the message Ok, turning the tv on.

  1. 通过选择左侧窗格顶部的 " 新建命令 " 来创建新命令。Create a new command by selecting New command at the top of the left pane. 此时将打开 新的命令 窗口。The New command window opens.
  2. 对于 " 名称 " 字段,请提供值 TurnOnFor the Name field, provide the value TurnOn.
  3. 选择“创建” 。Select Create.

中间窗格列出该命令的属性。The middle pane lists the properties of the command.

下表说明了命令的配置属性。The following table explains the command's configuration properties. 有关详细信息,请参阅 自定义命令的概念和定义For more information, see Custom Commands concepts and definitions.

配置Configuration 说明Description
示例句子Example sentences 示例最谈话用户可以说触发此命令。Example utterances the user can say to trigger this command.
参数Parameters 完成命令所需的信息。Information required to complete the command.
完成规则Completion rules 要执行以执行命令的操作。Actions to be taken to fulfill the command. 示例:响应用户或与 web 服务进行通信。Examples: responding to the user or communicating with a web service.
交互规则Interaction rules 用于处理更为具体或复杂的情况的其他规则。Other rules to handle more specific or complex situations.

显示命令创建位置的屏幕截图。Screenshot showing where to create a command.

添加示例句子Add example sentences

在 " 示例句子 " 部分中,提供了用户可以使用的示例。In the Example sentences section, you provide an example of what the user can say.

  1. 在中间窗格中,选择 " 示例句子"。In the middle pane, select Example sentences.

  2. 在右侧窗格中,添加示例:In the pane on the right, add examples:

    Turn on the tv
    
  3. 在窗格顶部,选择“保存”。At the top of the pane, select Save.

你还没有参数,因此可以移动到 完成规则 部分。You don't have parameters yet, so you can move to the Completion rules section.

添加完成规则Add a completion rule

接下来,该命令需要完成规则。Next, the command needs a completion rule. 此规则告知用户正在执行执行操作。This rule tells the user that a fulfillment action is being taken.

有关规则和完成规则的详细信息,请参阅 自定义命令的概念和定义For more information about rules and completion rules, see Custom Commands concepts and definitions.

  1. 选择已 完成 的默认完成规则。Select the default completion rule Done. 然后按如下所示对其进行编辑:Then edit it as follows:

    设置Setting 建议的值Suggested value 说明Description
    名称Name ConfirmationResponse 描述规则用途的名称A name describing the purpose of the rule
    条件Conditions None 确定何时可以运行规则的条件Conditions that determine when the rule can run
    操作Actions 发送语音响应 > 简单编辑器 > 第一变体 > Ok, turning the tv onSend speech response > Simple editor > First variation > Ok, turning the tv on 规则条件为 true 时要执行的操作The action to take when the rule condition is true

    显示在何处创建语音响应的屏幕截图。Screenshot showing where to create a speech response.

  2. 选择 " 保存 " 以保存操作。Select Save to save the action.

  3. 返回 " 完成规则 " 部分,选择 " 保存 " 以保存所有更改。Back in the Completion rules section, select Save to save all changes.

    备注

    无需使用命令附带的默认完成规则。You don't have to use the default completion rule that comes with the command. 您可以删除默认完成规则并添加您自己的规则。You can delete the default completion rule and add your own rule.

添加 SetTemperature 命令Add a SetTemperature command

现在再添加一个命令, SetTemperatureNow add one more command, SetTemperature. 此命令将使用单个查询文本, Set the temperature to 40 degrees 并使用消息进行响应 Ok, setting temperature to 40 degreesThis command will take a single utterance, Set the temperature to 40 degrees, and respond with the message Ok, setting temperature to 40 degrees.

若要创建新命令,请执行命令所用的步骤 TurnOn ,但使用示例句子 Set the temperature to 40 degreesTo create the new command, follow the steps you used for the TurnOn command, but use the example sentence Set the temperature to 40 degrees.

然后编辑现有完成 的规则,如下所示:Then edit the existing Done completion rules as follows:

设置Setting 建议的值Suggested value
名称Name ConfirmationResponse
条件Conditions None
操作Actions 发送语音响应 > 简单编辑器 > 第一变体 > Ok, setting temperature to 40 degreesSend speech response > Simple editor > First variation > Ok, setting temperature to 40 degrees

选择 " 保存 " 以保存对命令所做的所有更改。Select Save to save all changes to the command.

添加 SetAlarm 命令Add a SetAlarm command

创建新 SetAlarm 命令。Create a new SetAlarm command. 使用示例句子 Set an alarm for 9 am tomorrowUse the example sentence Set an alarm for 9 am tomorrow. 然后编辑现有完成 的规则,如下所示:Then edit the existing Done completion rules as follows:

设置Setting 建议的值Suggested value
名称Name ConfirmationResponse
条件Conditions None
操作Actions 发送语音响应 > 简单编辑器 > 第一变体 > Ok, setting an alarm for 9 am tomorrowSend speech response > Simple editor > First variation > Ok, setting an alarm for 9 am tomorrow

选择 " 保存 " 以保存对命令所做的所有更改。Select Save to save all changes to the command.

试试看Try it out

使用 "测试" 窗格测试应用程序的行为:Test the application's behavior by using the test pane:

  1. 在窗格的右上角,选择 " 训练 " 图标。In the upper-right corner of the pane, select the Train icon.
  2. 训练完成后,选择 " 测试"。When the training finishes, select Test.

使用语音或文本试用以下查询文本示例:Try out the following utterance examples by using voice or text:

  • 键入: 将温度设置为40度You type: set the temperature to 40 degrees
  • 预期响应:正常,将温度设置为40度Expected response: Ok, setting temperature to 40 degrees
  • 键入: 打开电视You type: turn on the tv
  • 预期响应: Ok,打开电视Expected response: Ok, turning the tv on
  • 键入:将警报设置为明天上午 9You type: set an alarm for 9 am tomorrow
  • 预期响应:好,将警报设置为明天上午9点Expected response: Ok, setting an alarm for 9 am tomorrow

在 web 聊天界面中显示测试的屏幕截图。Screenshot showing the test in a web-chat interface.

提示

在 "测试" 窗格中,可以选择 " 打开详细 信息" 以获取有关如何处理此语音输入或文本输入的信息。In the test pane, you can select Turn details for information about how this voice input or text input was processed.

向命令添加参数Add parameters to commands

本部分介绍如何将参数添加到命令中。In this section, you learn how to add parameters to your commands. 命令需要参数来完成任务。Commands require parameters to complete a task. 在复杂的方案中,参数可用于定义触发自定义操作的条件。In complex scenarios, parameters can be used to define conditions that trigger custom actions.

为 TurnOn 命令配置参数Configure parameters for a TurnOn command

首先编辑现有 TurnOn 命令以打开和关闭多个设备。Start by editing the existing TurnOn command to turn on and turn off multiple devices.

  1. 现在,此命令将处理 on 和 off 方案,请将命令重命名为 TurnOnOffNow that the command will handle both on and off scenarios, rename the command as TurnOnOff.

    1. 在左侧窗格中,选择 " TurnOn " 命令。In the pane on the left, select the TurnOn command. 然后单击窗格顶部的 " 新建命令 " 旁的省略号 (...) "按钮。Then next to New command at the top of the pane, select the ellipsis (...) button.

    2. 选择 " 重命名"。Select Rename. 在 " 重命名" 命令 窗口中,将名称更改为 TurnOnOffIn the Rename command window, change the name to TurnOnOff.

  2. 将新参数添加到命令。Add a new parameter to the command. 参数表示用户是要打开还是关闭设备。The parameter represents whether the user wants to turn the device on or off.

    1. 在中间窗格的顶部,选择 " 添加"。At top of the middle pane, select Add. 从下拉菜单中选择 " 参数"。From the drop-down menu, select Parameter.

    2. 在右侧窗格的 " 参数 " 部分的 " 名称 " 框中,添加 OnOffIn the pane on the right, in the Parameters section, in the Name box, add OnOff.

    3. 选择“必需”。Select Required. 在 " 为所需参数添加响应 " 窗口中,选择 " 简单编辑器"。In the Add response for a required parameter window, select Simple editor. 第一个变体 字段中,增加 或关闭?In the First variation field, add On or Off?.

    4. 选择“更新”。Select Update.

      屏幕截图,显示选中 "简单编辑器" 选项卡的 "添加对所需参数的响应" 部分。Screenshot that shows the 'Add response for a required parameter' section with the 'Simple editor' tab selected.

    5. 使用下表配置参数的属性。Configure the parameter's properties by using the following table. 有关命令的所有配置属性的信息,请参阅 自定义命令的概念和定义For information about all of the configuration properties of a command, see Custom Commands concepts and definitions.

      配置Configuration 建议的值Suggested value 说明Description
      名称Name OnOff 参数的描述性名称A descriptive name for the parameter
      为全局Is Global 未选定Unselected 指示此参数的值是否全局应用于应用程序中所有命令的复选框。Check box indicating whether a value for this parameter is globally applied to all commands in the application.
      必需Required 选定Selected 指示在命令完成之前是否需要此参数的值的复选框。Check box indicating whether a value for this parameter is required before the command finishes.
      响应所需的参数Response for required parameter 简单编辑器 > On or Off?Simple editor > On or Off? 询问此参数值未知的提示。A prompt asking for the value of this parameter when it isn't known.
      类型Type 字符串String 参数类型,如 Number、String、Date Time 或 Geography。Parameter type, such as Number, String, Date Time, or Geography.
      配置Configuration 接受来自内部目录的预定义输入值Accept predefined input values from an internal catalog 对于字符串,此设置将输入限制为一组可能值。For strings, this setting limits inputs to a set of possible values.
      预定义的输入值Predefined input values on, offon, off 一组可能的值及其别名。Set of possible values and their aliases.
    6. 若要添加预定义的输入值,请选择 " 添加预定义的输入"。To add predefined input values, select Add a predefined input. 在 " 新建项目 " 窗口中,键入 " 名称 ",如前面的表中所示。In New Item window, type Name as shown in the preceding table. 在这种情况下,不使用别名,因此可以将此字段留空。In this case, you're not using aliases, so you can leave this field blank.

      显示如何创建参数的屏幕截图。Screenshot showing how to create a parameter.

    7. 选择 " 保存 " 以保存参数的所有配置。Select Save to save all configurations of the parameter.

添加 SubjectDevice 参数Add a SubjectDevice parameter

  1. 若要添加另一个参数以表示可使用此命令控制的设备的名称,请选择 " 添加"。To add a second parameter to represent the name of the devices that can be controlled by using this command, select Add. 使用以下配置。Use the following configuration.

    设置Setting 建议的值Suggested value
    名称Name SubjectDevice
    为全局Is Global 未选定Unselected
    必需Required 选定Selected
    响应所需的参数Response for required parameter 简单编辑器 > Which device do you want to control?Simple editor > Which device do you want to control?
    类型Type 字符串String
    配置Configuration 接受来自内部目录的预定义输入值Accept predefined input values from an internal catalog
    预定义的输入值Predefined input values tv, fantv, fan
    别名 (tv) Aliases (tv) television, tellytelevision, telly
  2. 选择“保存”。Select Save.

修改示例句子Modify example sentences

对于使用参数的命令,添加涵盖所有可能组合的示例句子很有用。For commands that use parameters, it's helpful to add example sentences that cover all possible combinations. 例如:For example:

  • 完整参数信息: turn {OnOff} the {SubjectDevice}Complete parameter information: turn {OnOff} the {SubjectDevice}
  • 部分参数信息: turn it {OnOff}Partial parameter information: turn it {OnOff}
  • 无参数信息: turn somethingNo parameter information: turn something

使用不同度信息的示例句子允许自定义命令应用程序使用部分信息解析一种单步解析和多轮解析。Example sentences that use varying degrees of information allow the Custom Commands application to resolve both one-shot resolutions and multiple-turn resolutions by using partial information.

了解该信息后,编辑示例句子以使用这些建议的参数:With that information in mind, edit the example sentences to use these suggested parameters:

turn {OnOff} the {SubjectDevice}
{SubjectDevice} {OnOff}
turn it {OnOff}
turn something {OnOff}
turn something

选择“保存”。Select Save.

提示

在示例-句子编辑器中,使用大括号来引用参数。In the example-sentences editor, use curly braces to refer to your parameters. 例如,turn {OnOff} the {SubjectDevice}For example, turn {OnOff} the {SubjectDevice}. 使用选项卡自动完成以前创建的参数。Use a tab for automatic completion backed by previously created parameters.

修改完成规则以包含参数Modify completion rules to include parameters

修改现有的完成规则 ConfirmationResponseModify the existing completion rule ConfirmationResponse.

  1. 在 " 条件 " 部分中,选择 " 添加条件"。In the Conditions section, select Add a condition.

  2. 在 " 新建条件 " 窗口的 " 类型 " 列表中,选择 " 必需参数"。In the New Condition window, in the Type list, select Required parameters. 在下面的列表中,选择 " 麦克风 " 和 " SubjectDevice"。In the list that follows, select both OnOff and SubjectDevice.

  3. 使 IsGlobal 保持未选中状态。Leave IsGlobal unselected.

  4. 选择“创建” 。Select Create.

  5. 在 " 操作 " 部分,通过将鼠标悬停在 " 发送语音响应 " 操作并选择 "编辑" 按钮,对其进行编辑。In the Actions section, edit the Send speech response action by hovering over it and selecting the edit button. 这一次,请使用新创建的 OnOffSubjectDevice 参数:This time, use the newly created OnOff and SubjectDevice parameters:

    Ok, turning the {SubjectDevice} {OnOff}
    
  6. 选择“保存”。Select Save.

通过选择右侧窗格顶部的 " 训练 " 图标来尝试更改。Try out the changes by selecting the Train icon at the top of the pane on the right.

训练完成后,选择 " 测试"。When the training finishes, select Test. 此时将显示 " 测试应用程序 " 窗口。A Test your application window appears. 尝试下列交互:Try the following interactions:

  • 输入: 关闭电视Input: turn off the tv
  • 输出:确定,关闭电视Output: Ok, turning off the tv
  • 输入: 关闭电视Input: turn off the television
  • 输出:确定,关闭电视Output: Ok, turning off the tv
  • 输入:将其 关闭Input: turn it off
  • 输出:要控制哪个设备?Output: Which device do you want to control?
  • 输入: 电视Input: the tv
  • 输出:确定,关闭电视Output: Ok, turning off the tv

为 SetTemperature 命令配置参数Configure parameters for a SetTemperature command

修改 SetTemperature 命令,使其能够在用户指示时设置温度。Modify the SetTemperature command to enable it to set the temperature as the user directs.

添加一个 Temperature 参数。Add a Temperature parameter. 使用以下配置:Use the following configuration:

配置Configuration 建议的值Suggested value
名称Name Temperature
必需Required 选定Selected
响应所需的参数Response for required parameter 简单编辑器 > What temperature would you like?Simple editor > What temperature would you like?
类型Type Number

编辑示例最谈话以使用以下值。Edit the example utterances to use the following values.

set the temperature to {Temperature} degrees
change the temperature to {Temperature}
set the temperature
change the temperature

编辑现有的完成规则。Edit the existing completion rules. 使用以下配置。Use the following configuration.

配置Configuration 建议的值Suggested value
条件Conditions 必需的参数 > 温度Required parameter > Temperature
操作Actions 发送语音响应 > Ok, setting temperature to {Temperature} degreesSend speech response > Ok, setting temperature to {Temperature} degrees

为 SetAlarm 命令配置参数Configure parameters for a SetAlarm command

添加一个名为的参数 DateTimeAdd a parameter called DateTime. 使用以下配置。Use the following configuration.

设置Setting 建议的值Suggested value
名称Name DateTime
必需Required 选定Selected
响应所需的参数Response for required parameter 简单编辑器 > For what time?Simple editor > For what time?
类型Type DateTimeDateTime
日期默认值Date Defaults 如果缺少日期,请使用 today。If the date is missing, use today.
时间默认值Time Defaults 如果缺少该时间,则使用当天的开始时间。If the time is missing, use the start of the day.

备注

本文主要使用字符串、数字和日期时间参数类型。This article mostly uses String, Number, and DateTime parameter types. 有关所有受支持的参数类型及其属性的列表,请参阅 自定义命令的概念和定义For a list of all supported parameter types and their properties, see Custom Commands concepts and definitions.

编辑示例最谈话。Edit the example utterances. 请使用以下值。Use the following values.

set an alarm for {DateTime}
set alarm {DateTime}
alarm for {DateTime}

编辑现有的完成规则。Edit the existing completion rules. 使用以下配置。Use the following configuration.

设置Setting 建议的值Suggested value
操作Actions 发送语音响应 > Ok, alarm set for {DateTime}Send speech response > Ok, alarm set for {DateTime}

使用与不同命令相关的最谈话测试这三个命令。Test the three commands together by using utterances related to different commands. (可以在不同的命令之间切换。 ) (You can switch between the different commands.)

  • 输入: 设置警报Input: Set an alarm
  • 输出:什么时间?Output: For what time?
  • 输入: 打开电视Input: Turn on the tv
  • 输出:好,打开电视Output: Ok, turning the tv on
  • 输入: 设置警报Input: Set an alarm
  • 输出:什么时间?Output: For what time?
  • 输入: 下午 5Input: 5 pm
  • 输出:正常,为 2020-05-01 17:00:00 设置的警报Output: Ok, alarm set for 2020-05-01 17:00:00

将配置添加到命令参数Add configurations to command parameters

在本部分中,将详细了解高级参数配置,包括:In this section, you learn more about advanced parameter configuration, including:

  • 参数值如何属于自定义命令应用程序外定义的集。How parameter values can belong to a set that's defined outside of the Custom Commands application.
  • 如何对参数值添加验证子句。How to add validation clauses on the parameter values.

将参数配置为外部目录实体Configure a parameter as an external catalog entity

使用自定义命令功能,您可以将字符串类型参数配置为引用通过 web 终结点承载的外部目录。The Custom Commands feature allows you to configure string-type parameters to refer to external catalogs hosted over a web endpoint. 因此,无需编辑自定义命令应用程序即可单独更新外部目录。So you can update the external catalog independently without editing the Custom Commands application. 此方法在目录条目很多的情况下很有用。This approach is useful in cases where the catalog entries are numerous.

重新使用 SubjectDevice 命令中的参数 TurnOnOffReuse the SubjectDevice parameter from the TurnOnOff command. 此参数的当前配置 接受来自内部目录的预定义输入The current configuration for this parameter is Accept predefined inputs from internal catalog. 此配置引用参数配置中设备的静态列表。This configuration refers to a static list of devices in the parameter configuration. 将此内容移到可以单独更新的外部数据源。Move out this content to an external data source that can be updated independently.

若要移动内容,请首先添加新的 web 终结点。To move the content, start by adding a new web endpoint. 在左侧窗格中,请参阅 " Web 终结点 " 部分。In the pane on the left, go to the Web endpoints section. 在该处添加新的 web 终结点。There, add a new web endpoint. 使用以下配置。Use the following configuration.

设置Setting 建议的值Suggested value
名称Name getDevices
URLURL https://aka.ms/speech/cc-sampledevices
方法Method GETGET

如果 URL 的建议值对你不起作用,则配置并托管一个 web 终结点,该终结点将返回一个 JSON 文件,该文件包含可控制的设备列表。If the suggested value for the URL doesn't work for you, configure and host a web endpoint that returns a JSON file that consists of the list of the devices that can be controlled. Web 终结点应返回格式如下的 JSON 文件:The web endpoint should return a JSON file formatted as follows:

{
    "fan" : [],
    "refrigerator" : [
        "fridge"
    ],
    "lights" : [
        "bulb",
        "bulbs",
        "light"
        "light bulb"
    ],
    "tv" : [
        "telly",
        "television"
        ]
}

接下来,请执行 " SubjectDevice 参数设置" 页。Next go the SubjectDevice parameter settings page. 设置以下属性。Set up the following properties.

设置Setting 建议的值Suggested value
配置Configuration 接受来自外部目录的预定义输入Accept predefined inputs from external catalog
目录终结点Catalog endpoint getDevices
方法Method GETGET

再选择“保存”。Then select Save.

重要

除非在左侧窗格的 " web 终结点 " 部分中设置了 web 终结点,否则不会显示用于将参数配置为接受来自外部目录的输入的选项。You won't see an option to configure a parameter to accept inputs from an external catalog unless you have the web endpoint set in the Web endpoint section in the pane on the left.

选择 " 训练",尝试一下。Try it out by selecting Train. 训练完成后,选择 " 测试 ",并尝试几个交互。After the training finishes, select Test and try a few interactions.

  • 输入: 开启Input: turn on
  • 输出:要控制哪个设备?Output: Which device do you want to control?
  • 输入: 光源Input: lights
  • 输出:好,打开灯Output: Ok, turning the lights on

备注

你现在可以控制在 web 终结点上承载的所有设备。You can now control all the devices hosted on the web endpoint. 但仍需要训练应用程序来测试新的更改,然后重新发布应用程序。But you still need to train the application to test the new changes and then republish the application.

向参数添加验证Add validation to parameters

验证 是应用于某些参数类型的构造,这些参数类型允许你配置对参数值的约束。Validations are constructs that apply to certain parameter types that allow you to configure constraints on the parameter's value. 如果值不在约束范围内,则会提示您进行更正。They prompt you for corrections if values don't fall within the constraints. 有关扩展验证构造的参数类型的列表,请参阅 自定义命令的概念和定义For a list of parameter types that extend the validation construct, see Custom Commands concepts and definitions.

使用命令测试验证 SetTemperatureTest out validations by using the SetTemperature command. 使用以下步骤添加参数的验证 TemperatureUse the following steps to add a validation for the Temperature parameter.

  1. 在左侧窗格中,选择 " SetTemperature " 命令。In the pane on the left, select the SetTemperature command.

  2. 在中间窗格中,选择 " 温度"。In the middle pane, select Temperature.

  3. 在右侧窗格中,选择 " 添加验证"。In the pane on the right, select Add a validation.

  4. 在 " 新建验证 " 窗口中,按下表所示配置验证。In the New validation window, configure validation as shown in the following table. 然后选择“创建”。Then select Create.

    参数配置Parameter configuration 建议的值Suggested value 说明Description
    最小值Min Value 60 对于 Number 参数,此参数可以采用的最小值For Number parameters, the minimum value this parameter can assume
    最大值Max Value 80 对于 Number 参数,此参数可以采用的最大值For Number parameters, the maximum value this parameter can assume
    故障响应Failure response 简单编辑器 > 第一变体 > Sorry, I can only set temperature between 60 and 80 degrees. What temperature do you want?Simple editor > First variation > Sorry, I can only set temperature between 60 and 80 degrees. What temperature do you want? 如果验证失败,则提示要求输入新值A prompt to ask for a new value if the validation fails

    显示如何添加范围验证的屏幕截图。Screenshot showing how to add a range validation.

通过选择右侧窗格顶部的 " 训练 " 图标来尝试一下。Try it out by selecting the Train icon at the top of the pane on the right. 训练完成后,选择 " 测试"。After the training finishes, select Test. 尝试几个交互:Try a few interactions:

  • 输入: 将温度设置为72度Input: Set the temperature to 72 degrees
  • 输出:正常,将温度设置为72度Output: Ok, setting temperature to 72 degrees
  • 输入: 将温度设置为45度Input: Set the temperature to 45 degrees
  • 输出:抱歉,只能设置60到80度之间的温度Output: Sorry, I can only set temperature between 60 and 80 degrees
  • 输入: 改为72度Input: make it 72 degrees instead
  • 输出:正常,将温度设置为72度Output: Ok, setting temperature to 72 degrees

添加交互规则Add interaction rules

交互规则是处理特定或复杂情况的 其他 规则。Interaction rules are additional rules that handle specific or complex situations. 尽管你可以随意创作自己的交互规则,但在此示例中,你将在以下情况下使用交互规则:Although you're free to author your own interaction rules, in this example you use interaction rules for the following scenarios:

  • 确认命令Confirming commands
  • 向命令添加单步更正Adding a one-step correction to commands

有关交互规则的详细信息,请参阅 自定义命令的概念和定义For more information about interaction rules, see Custom Commands concepts and definitions.

向命令添加确认Add confirmations to a command

若要添加确认,请使用 SetTemperature 命令。To add a confirmation, you use the SetTemperature command. 若要获得确认,请使用以下步骤创建交互规则:To achieve confirmation, create interaction rules by using the following steps:

  1. 在左侧窗格中,选择 " SetTemperature " 命令。In the pane on the left, select the SetTemperature command.

  2. 在中间窗格中,通过选择 " 添加" 来添加交互规则。In the middle pane, add interaction rules by selecting Add. 然后选择 "交互规则" " > 确认" 命令Then select Interaction rules > Confirm command.

    此操作将添加三个交互规则。This action adds three interaction rules. 规则要求用户确认警报的日期和时间。The rules ask the user to confirm the date and time of the alarm. 它们需要确认 ("是" 或 "否") 。They expect a confirmation (yes or no) for the next turn.

    1. 使用以下配置修改 Confirm 命令 交互规则:Modify the Confirm command interaction rule by using the following configuration:

      1. 更改名称以 确认温度Change the name to Confirm temperature.
      2. 添加新条件:所需参数 > 温度Add a new condition: Required parameters > Temperature.
      3. 添加新操作:键入 > 发送语音响应 > 是否确实要将温度设置为 {温度} 度?Add a new action: Type > Send speech response > Are you sure you want to set the temperature as {Temperature} degrees?
      4. 在 " 预期 " 部分中,保留 " 要求用户确认" 的默认值。In the Expectations section, leave the default value of Expecting confirmation from user.

      显示如何创建必需参数响应的屏幕截图。Screenshot showing how to create the required parameter response.

    2. 修改 确认成功 交互规则以处理成功的确认 (用户是否) 。Modify the Confirmation succeeded interaction rule to handle a successful confirmation (the user said yes).

      1. 将名称更改为 确认温度已成功Change the name to Confirmation temperature succeeded.
      2. 使现有 确认为成功 状态。Leave the existing Confirmation was successful condition.
      3. 添加新条件:键入 > 所需的参数 > 温度Add a new condition: Type > Required parameters > Temperature.
      4. 保留 "执行完成规则" 的默认 执行后状态 值。Leave the default Post-execution state value as Execute completion rules.
    3. 修改 确认拒绝 交互规则以在拒绝确认时处理方案, (用户未提到) 。Modify the Confirmation denied interaction rule to handle scenarios when confirmation is denied (the user said no).

      1. 将名称更改为 " 确认温度已拒绝"。Change the name to Confirmation temperature denied.
      2. 保留现有 确认拒绝 条件。Leave the existing Confirmation was denied condition.
      3. 添加新条件:键入 > 所需的参数 > 温度Add a new condition: Type > Required parameters > Temperature.
      4. 添加新操作:键入" > 发送语音响应 > 无问题"。什么是温度?Add a new action: Type > Send speech response > No problem. What temperature then?.
      5. 更改默认的 执行后状态 值,以 等待用户的输入Change the default Post-execution state value to Wait for user's input.

重要

本文介绍如何使用内置确认功能。In this article, you use the built-in confirmation capability. 还可以手动逐个添加交互规则。You can also manually add interaction rules one by one.

通过选择 定型 来尝试更改。Try out the changes by selecting Train. 训练完成后,选择 " 测试"。When the training finishes, select Test.

  • 输入将温度设置为80度Input: Set temperature to 80 degrees
  • 输出:是否确实要将温度设置为80度?Output: are you sure you want to set the temperature as 80 degrees?
  • 输入Input: No
  • 输出:没有问题。Output: No problem. 什么是温度?What temperature then?
  • 输入72 度Input: 72 degrees
  • 输出:是否确实要将温度设置为72度?Output: are you sure you want to set the temperature as 72 degrees?
  • 输入Input: Yes
  • 输出:正常,将温度设置为72度Output: OK, setting temperature to 72 degrees

在命令中实现更正Implement corrections in a command

在本部分中,你将配置一个单步更正。In this section, you'll configure a one-step correction. 此更正在执行完成操作后使用。This correction is used after the fulfillment action has run. 如果命令尚未完成,还会看到一个示例,说明如何默认启用更正。You'll also see an example of how a correction is enabled by default if the command isn't fulfilled yet. 若要在命令未完成时添加更正,请添加新参数 AlarmToneTo add a correction when the command isn't finished, add the new parameter AlarmTone.

在左窗格中,选择 " SetAlarm " 命令。In the left pane, select the SetAlarm command. 然后添加新参数 AlarmToneThen and add the new parameter AlarmTone.

  • 路径名 > AlarmToneName > AlarmTone
  • 类型 > 字符串Type > String
  • 默认值 > ChimesDefault Value > Chimes
  • 配置 > 接受来自内部目录的预定义输入值Configuration > Accept predefined input values from the internal catalog
  • 预定义的输入值 > Chimes曲调回显 (这些值是单个预定义的输入。 ) Predefined input values > Chimes, Jingle, and Echo (These values are individual predefined inputs.)

接下来,将 DateTime 参数的响应更新为 "就绪", 以将音调设置为 {AlarmTone}。什么时间?Next, update the response for the DateTime parameter to Ready to set alarm with tone as {AlarmTone}. For what time?. 然后按如下所示修改完成规则:Then modify the completion rule as follows:

  1. 选择现有完成规则 " ConfirmationResponse"。Select the existing completion rule ConfirmationResponse.
  2. 在右侧窗格中,将鼠标悬停在现有操作上,然后选择 " 编辑"。In the pane on the right, hover over the existing action and select Edit.
  3. 将语音响应更新为 OK, alarm set for {DateTime}. The alarm tone is {AlarmTone}Update the speech response to OK, alarm set for {DateTime}. The alarm tone is {AlarmTone}.

重要

在执行命令时,无需任何显式配置即可更改警报音。The alarm tone can change without any explicit configuration in an ongoing command. 例如,在命令尚未完成时,它可能会更改。For example, it can change when the command hasn't finished yet. 默认情况 下,将对所有命令参数启用更正,而不管 turn 号如何,如果该命令尚未完成。A correction is enabled by default for all of the command parameters, regardless of the turn number, if the command is yet to be fulfilled.

完成命令后,实现更正Implement a correction when a command is finished

自定义命令平台允许进行单步更正,即使命令已完成。The Custom Commands platform allows for one-step correction even when the command has finished. 默认情况下不启用此功能。This feature isn't enabled by default. 必须显式配置此设置。It must be explicitly configured.

使用以下步骤配置单步更正:Use the following steps to configure a one-step correction:

  1. SetAlarm 命令中,添加类型 " 更新上一个" 命令 的交互规则以更新以前设置的警报。In the SetAlarm command, add an interaction rule of the type Update previous command to update the previously set alarm. 将交互规则重命名为 " 更新以前的警报"。Rename the interaction rule as Update previous alarm.
  2. 保留默认条件: 需要更新上一个命令Leave the default condition: Previous command needs to be updated.
  3. 添加新条件:键入 > 必需的参数 > 日期时间Add a new condition: Type > Required Parameter > DateTime.
  4. 添加新操作:键入 > 发送语音响应 > 简单编辑器 > 正在将以前的警报时间更新为 {DateTime}Add a new action: Type > Send speech response > Simple editor > Updating previous alarm time to {DateTime}.
  5. 命令完成 后保留默认的 执行后状态 值。Leave the default Post-execution state value as Command completed.

通过选择 定型 来尝试更改。Try out the changes by selecting Train. 等待训练完成,然后选择 " 测试"。Wait for the training to finish, and then select Test.

  • 输入设置警报。Input: Set an alarm.
  • 输出:准备好将音频设置为 Chimes。Output: Ready to set alarm with tone as Chimes. 什么时间?For what time?
  • 输入将音频设置为曲调,将其设置为明天上午9点。Input: Set an alarm with the tone as Jingle for 9 am tomorrow.
  • 输出:正常,为 2020-05-21 09:00:00 设置的警报。Output: OK, alarm set for 2020-05-21 09:00:00. 警报音为曲调。The alarm tone is Jingle.
  • 输入否,上午8点。Input: No, 8 am.
  • 输出:将以前的警报时间更新为 2020-05-29 08:00。Output: Updating previous alarm time to 2020-05-29 08:00.

备注

在实际应用程序中,在此更正规则的 " 操作 " 部分中,还需要将活动发送回客户端或调用 HTTP 终结点,以更新系统中的警报时间。In a real application, in the Actions section of this correction rule, you'll also need to send back an activity to the client or call an HTTP endpoint to update the alarm time in your system. 此操作应独自负责更新警报时间。This action should be solely responsible for updating the alarm time. 它不应负责命令的任何其他属性。It shouldn't be responsible for any other attribute of the command. 在这种情况下,该属性将为警报音。In this case, that attribute would be the alarm tone.

为语音响应添加语言生成模板Add language-generation templates for speech responses

语言生成 (LG) 模板使你可以自定义发送到客户端的响应。Language-generation (LG) templates allow you to customize the responses sent to the client. 它们会在响应中引入变体。They introduce variance into the responses. 可以通过使用来实现语言生成:You can achieve language generation by using:

  • 语言生成模板。Language-generation templates.
  • 自适应表达式。Adaptive expressions.

自定义命令模板基于机器人框架的 LG 模板Custom Commands templates are based on the Bot Framework's LG templates. 由于自定义命令功能会在 () 参数或操作中的语音响应时创建新的 LG 模板,因此无需指定 LG 模板的名称。Because the Custom Commands feature creates a new LG template when required (for speech responses in parameters or actions), you don't have to specify the name of the LG template.

因此,你无需定义模板,如下所示:So you don't need to define your template like this:

   # CompletionAction
   - Ok, turning {OnOff} the {SubjectDevice}
   - Done, turning {OnOff} the {SubjectDevice}
   - Proceeding to turn {OnOff} {SubjectDevice}

您可以改为定义不带名称的模板正文,如下所示:Instead, you can define the body of the template without the name, like this:

显示模板编辑器示例的屏幕截图。Screenshot showing a template editor example.

此更改引入了发送到客户端的语音响应的变化形式。This change introduces variation into the speech responses that are sent to the client. 对于查询文本,会从提供的选项中随机选取相应的语音响应。For an utterance, the corresponding speech response is randomly picked out of the provided options.

通过利用 LG 模板,你还可以使用自适应表达式为命令定义复杂的语音响应。By taking advantage of LG templates, you can also define complex speech responses for commands by using adaptive expressions. 有关详细信息,请参阅 LG 模板格式For more information, see the LG templates format.

默认情况下,自定义命令功能支持所有功能,但有以下细微差异:By default, the Custom Commands feature supports all capabilities, with the following minor differences:

  • 在 LG 模板中,实体表示为 ${entityName}In the LG templates, entities are represented as ${entityName}. 自定义命令功能不使用实体。The Custom Commands feature doesn't use entities. 但是,可以将参数用作 ${parameterName} 表示形式或表示形式的变量 {parameterName}But you can use parameters as variables with either the ${parameterName} representation or the {parameterName} representation.
  • 自定义命令功能不支持模板撰写和扩展,因为您永远不会直接编辑 lg 文件。The Custom Commands feature doesn't support template composition and expansion, because you never edit the .lg file directly. 仅编辑自动创建的模板的响应。You edit only the responses of automatically created templates.
  • 自定义命令功能不支持 LG 注入的自定义函数。The Custom Commands feature doesn't support custom functions that LG injects. 支持预定义函数。Predefined functions are supported.
  • 自定义命令功能不支持选项,例如 strictreplaceNulllineBreakStyleThe Custom Commands feature doesn't support options, such as strict, replaceNull, and lineBreakStyle.

向 TurnOnOff 命令添加模板响应Add template responses to a TurnOnOff command

修改 TurnOnOff 命令以添加新参数。Modify the TurnOnOff command to add a new parameter. 使用以下配置。Use the following configuration.

设置Setting 建议的值Suggested value
名称Name SubjectContext
为全局Is Global 未选定Unselected
必需Required 未选定Unselected
类型Type 字符串String
默认值Default value all
配置Configuration 接受来自内部目录的预定义输入值Accept predefined input values from internal catalog
预定义的输入值Predefined input values room, bathroom, allroom, bathroom, all

修改完成规则Modify a completion rule

编辑现有完成规则 ConfirmationResponse 的 "操作" 部分。Edit the Actions section of the existing completion rule ConfirmationResponse. 在 " 编辑操作 " 窗口中,切换到 " 模板编辑器"。In the Edit action window, switch to Template Editor. 然后,将文本替换为以下示例。Then replace the text with the following example.

- IF: @{SubjectContext == "all" && SubjectDevice == "lights"}
    - Ok, turning all the lights {OnOff}
- ELSEIF: @{SubjectDevice == "lights"}
    - Ok, turning {OnOff} the {SubjectContext} {SubjectDevice}
- ELSE:
    - Ok, turning the {SubjectDevice} {OnOff}
    - Done, turning {OnOff} the {SubjectDevice}

使用以下输入和输出定型和测试应用程序。Train and test your application by using the following input and output. 请注意响应的变体。Notice the variation of responses. 变体由模板值的多个替代项创建,也通过使用自适应表达式来创建。The variation is created by multiple alternatives of the template value and also by use of adaptive expressions.

  • 输入: 打开电视Input: turn on the tv
  • 输出:好,打开电视Output: Ok, turning the tv on
  • 输入: 打开电视Input: turn on the tv
  • 输出:已完成,已打开电视Output: Done, turned on the tv
  • 输入: 关闭光源Input: turn off the lights
  • 输出:确定,关闭所有灯光Output: Ok, turning all the lights off
  • 输入: 关闭房间光源Input: turn off room lights
  • 输出:确定,关闭房间灯光Output: Ok, turning off the room lights

使用自定义语音Use a custom voice

自定义自定义命令响应的另一种方法是选择输出语音。Another way to customize Custom Commands responses is to select an output voice. 使用以下步骤将默认语音切换为自定义语音:Use the following steps to switch the default voice to a custom voice:

  1. 在自定义命令应用程序的左侧窗格中,选择 " 设置"。In your Custom Commands application, in the pane on the left, select Settings.
  2. 在中间窗格中,选择 " 自定义语音"。In the middle pane, select Custom Voice.
  3. 在表中,选择自定义语音或公共语音。In the table, select a custom voice or public voice.
  4. 选择“保存”。Select Save.

显示示例句子和参数的屏幕截图。Screenshot showing sample sentences and parameters.

备注

对于公共语音,神经类型仅适用于特定区域。For public voices, neural types are available only for specific regions. 有关详细信息,请参阅 语音服务支持的区域For more information, see Speech service supported regions.

您可以在 " 自定义语音 项目" 页面上创建自定义语音。You can create custom voices on the Custom Voice project page. 有关详细信息,请参阅 自定义语音入门For more information, see Get started with Custom Voice.

现在,应用程序将在选定的语音中做出响应,而不是默认的语音。Now the application will respond in the selected voice, instead of the default voice.

后续步骤Next steps