匹配节点
概述
Match节点用于将字符串输入与一系列正则表达式进行匹配。每个正则表达式对应该节点的一个输出。
该节点可以配置为运行第一个匹配的输出端口,或者运行所有匹配的输出端口。如果未找到匹配项,则将执行Unmatched
输出端口。
该节点可用于根据字符串内容控制流程路由。例如,您可以使用Match节点检查用户消息是否包含特定关键词,并相应地引导对话流向。
如果提供了Value
输入,当找到匹配项时,该值将通过对应的输出端口传递。如果未提供Value
输入,当找到匹配项时,Test
输入将通过对应的输出端口传递。
- 输入
- 输出
- 编辑器设置
输入项
标题 | 数据类型 | 描述 | 默认值 | 备注 |
---|---|---|---|---|
Test | string | The string to be tested against the regular expressions. | (required) | The input will be coerced into a string if it is not a string. |
Value | any | The value to be passed to the output port if a match is found. | (optional) | If not provided, the Test input will be used as the value. |
输出
标题 | 数据类型 | 描述 | 备注 |
---|---|---|---|
Case [i] | any | The output port corresponding to the ith regular expression. | Dynamic number of outputs based on the number of regular expressions configured in the node. |
Unmatched | any | The output port that is executed if no match is found for the input string. | If a match is found for the input string, this output port will not be executed. |
编辑器设置
设置 | 描述 | 默认值 | 使用输入切换 | 输入数据类型 |
---|---|---|---|---|
独占模式 | 如果启用,则仅运行第一个匹配的用例,其他所有用例都不会运行。如果禁用,则将运行所有匹配的用例。 | 关闭 | 否 | 不适用 |
Cases | The list of regular expression cases to match against. | YES , NO | No | N/A |
示例1:基于AI输入的路由控制流程
- 创建一个Chat Node并将
Prompt
设置为一个问题,例如:您想先了解关于猫还是狗的信息?请只选择一个!我会为您提供两者的信息,我只是想知道您想先听哪个。
- 创建一个Match节点,并将Chat节点的
Message
输出连接到Match节点的Test
输入。 - 在Match Node的主体中,将正则表达式设置为
cats
和dogs
。 - 添加三对If节点和文本节点。将Match节点的
Case 1
输出连接到第一个If节点的If
输入,Match节点的Case 2
输出连接到第二个If节点的If
输入,Match节点的Unmatched
输出连接到第三个If节点的If
输入。将文本节点连接到If节点的Value
输入。 - The text of the three Text Nodes should be "You chose cats!", "You chose dogs!", and "Sorry, I didn't understand that. Please try again.", respectively.
- 运行图形。根据AI的响应,将执行相应的If节点,其中包含AI选择的文本
错误处理
如果未提供Test
输入,Match Node将会报错。如果输入字符串未找到匹配项,则将执行Unmatched
输出端口。
常见问题
问:多个案例可以同时匹配吗?
A: 是的,可以同时匹配多个条件。如果多个条件匹配,所有对应的输出端口都会执行。这在未来可能会成为一个可切换的选项,以便只执行第一个匹配的条件。
问:我可以使用Match节点来匹配数字吗?
A: 是的,您可以使用Match节点来匹配数字。但是,Test
输入将被强制转换为字符串进行匹配。
问:我可以使用Match节点来匹配字符串列表吗?
A: 是的,您可以使用Match节点来匹配字符串列表。不过,Test
输入会被强制转换为字符串进行匹配。默认的强制转换会将每个字符串放在单独的行上进行测试。