您现在访问的是微软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. 在“名称”框中,输入项目名称 Smart-Room-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

在创建的空 Smart-Room-Lite 自定义命令应用程序中添加一个命令。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 on 做出响应。It 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 degrees 做出响应。This 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:

  • 键入:“set the temperature to 40 degrees”You type: set the temperature to 40 degrees
  • 预期响应:“Ok, setting temperature to 40 degrees”Expected response: Ok, setting temperature to 40 degrees
  • 键入:“turn on the tv”You type: turn on the tv
  • 预期响应:“Ok, turning the tv on”Expected response: Ok, turning the tv on
  • 键入:“set an alarm for 9 am tomorrow”You type: set an alarm for 9 am tomorrow
  • 预期响应:“Ok, setting an alarm for 9 am tomorrow”Expected response: Ok, setting an alarm for 9 am tomorrow

显示在网络聊天界面中测试的屏幕截图。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. 既然该命令要处理打开和关闭设备的方案,因此请将其重命名为 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. 在“第一个变体”字段中,添加“On or Off?”。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 参数类型,例如“数字”、“字符串”、“日期时间”或“地理位置”。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. 在随后出现的列表中,选择“OnOff”和“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:

  • 输入:“turn off the tv”Input: turn off the tv
  • 输出:“Ok, turning off the tv”Output: Ok, turning off the tv
  • 输入:“turn off the television”Input: turn off the television
  • 输出:“Ok, turning off the tv”Output: Ok, turning off the tv
  • 输入:“turn it off”Input: turn it off
  • 输出:“Which device do you want to control?”Output: Which device do you want to control?
  • 输入:“the tv”Input: the tv
  • 输出:“Ok, turning off 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 必需参数 > TemperatureRequired parameter > Temperature
操作Actions 发送语音响应 > Ok, setting temperature to {Temperature} degreesSend speech response > Ok, setting temperature to {Temperature} degrees

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

添加名为 DateTime 的参数。Add 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 如果缺少日期,则使用当天的日期。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.)

  • 输入:“Set an alarm”Input: Set an alarm
  • 输出:“For what time?”Output: For what time?
  • 输入:“Turn on the tv”Input: Turn on the tv
  • 输出:“Ok, turning the tv on”Output: Ok, turning the tv on
  • 输入:“Set an alarm”Input: Set an alarm
  • 输出:“For what time?”Output: For what time?
  • 输入:“5 pm”Input: 5 pm
  • 输出:“Ok, alarm set for 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.

重新使用 TurnOnOff 命令中的 SubjectDevice 参数。Reuse 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 值不适合你,请配置并托管一个可返回 JSON 文件(其中包含可控制的设备的列表)的 Web 终结点。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.

  • 输入:“turn on”Input: turn on
  • 输出:“Which device do you want to control?”Output: Which device do you want to control?
  • 输入:“lights”Input: lights
  • 输出:“Ok, turning the lights on”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.

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

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

  2. 在中间窗格中,选择“Temperature”。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 适用于数字参数,表示此参数可以采用的最小值For Number parameters, the minimum value this parameter can assume
    最大值Max Value 80 适用于数字参数,表示此参数可以采用的最大值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:

  • 输入:“Set the temperature to 72 degrees”Input: Set the temperature to 72 degrees
  • 输出:“Ok, setting temperature to 72 degrees”Output: Ok, setting temperature to 72 degrees
  • 输入:“Set the temperature to 45 degrees”Input: Set the temperature to 45 degrees
  • 输出:“Sorry, I can only set temperature between 60 and 80 degrees”Output: Sorry, I can only set temperature between 60 and 80 degrees
  • 输入:“make it 72 degrees instead”Input: make it 72 degrees instead
  • 输出:“Ok, setting temperature to 72 degrees”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. 使用以下配置修改“确认命令”交互规则:Modify the Confirm command interaction rule by using the following configuration:

      1. 将名称更改为“确认温度”。Change the name to Confirm temperature.
      2. 添加新条件:“必需参数” > “Temperature”。Add a new condition: Required parameters > Temperature.
      3. 添加新操作:“类型” > “发送语音响应” > “Are you sure you want to set the temperature as {Temperature} degrees?”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. 修改“确认成功”交互规则,以处理成功确认(用户说“yes”)。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. 添加新条件:“类型” > “必需参数” > “Temperature”。Add a new condition: Type > Required parameters > Temperature.
      4. 将默认的“执行后状态”值保留为“执行完成规则”。 Leave the default Post-execution state value as Execute completion rules.
    3. 修改“拒绝确认”交互规则,以处理拒绝确认(用户说“no”)时的情况。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. 添加新条件:“类型” > “必需参数” > “Temperature”。Add a new condition: Type > Required parameters > Temperature.
      4. 添加新操作:“类型” > “发送语音响应” > “No problem.What temperature then?”。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.

  • 输入:“Set temperature to 80 degrees”Input: Set temperature to 80 degrees
  • 输出:“are you sure you want to set the temperature as 80 degrees?”Output: are you sure you want to set the temperature as 80 degrees?
  • 输入:“No”Input: No
  • 输出:“No problem.Output: No problem. What temperature then?”What temperature then?
  • 输入:“72 degrees”Input: 72 degrees
  • 输出:“are you sure you want to set the temperature as 72 degrees?”Output: are you sure you want to set the temperature as 72 degrees?
  • 输入:“Yes”Input: Yes
  • 输出:“OK, setting temperature to 72 degrees”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
  • “默认值” > “Chimes”Default Value > Chimes
  • “配置” > “接受来自内部目录的预定义输入值”Configuration > Accept predefined input values from the internal catalog
  • “预定义输入值” > “Chimes”、“Jingle”和“Echo”(这些值是单独的预定义输入。)Predefined input values > Chimes, Jingle, and Echo (These values are individual predefined inputs.)

接下来,将 DateTime 参数的响应更新为“Ready to set alarm with tone as {AlarmTone}.For what time?”。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. 默认情况下会为所有命令参数启用更正,而不管轮次编号是什么(如果命令尚待履行)。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. 添加新条件:“类型” > “必需参数” > “DateTime”。Add a new condition: Type > Required Parameter > DateTime.
  4. 添加新操作:“类型” > “发送语音响应” > “简单编辑器” > “Updating previous alarm time to {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.

  • 输入:“Set an alarm.”Input: Set an alarm.
  • 输出:“Ready to set alarm with tone as Chimes.Output: Ready to set alarm with tone as Chimes. For what time?”For what time?
  • 输入:“Set an alarm with the tone as Jingle for 9 am tomorrow.”Input: Set an alarm with the tone as Jingle for 9 am tomorrow.
  • 输出:“OK, alarm set for 2020-05-21 09:00:00.Output: OK, alarm set for 2020-05-21 09:00:00. The alarm tone is Jingle.”The alarm tone is Jingle.
  • 输入:“No, 8 am.”Input: No, 8 am.
  • 输出:“Updating previous alarm time to 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.

自定义命令模板基于 Bot Framework 的 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.
  • 自定义命令功能不支持 strictreplaceNulllineBreakStyle 等选项。The 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.

  • 输入:“turn on the tv”Input: turn on the tv
  • 输出:“Ok, turning the tv on”Output: Ok, turning the tv on
  • 输入:“turn on the tv”Input: turn on the tv
  • 输出:“Done, turned on the tv”Output: Done, turned on the tv
  • 输入:“turn off the lights”Input: turn off the lights
  • 输出:“Ok, turning all the lights off”Output: Ok, turning all the lights off
  • 输入:“turn off room lights”Input: turn off room lights
  • 输出:“Ok, turning off the 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