Watson Assistant实体值逻辑处理及烹饪机器人菜品匹配实现咨询
最佳实践:在Watson Assistant中基于食材数组匹配菜品
作为常年用Watson Assistant开发对话机器人的开发者,我来给你梳理几个最适合你场景的实现方案,完美适配你从Ingrediente数组匹配菜品到ListaPlatillos的需求:
1. 先夯实实体提取的基础(确保食材收集准确)
首先要确保你的Ingrediente数组能精准收集用户提到的食材——这一步没做好,后续匹配都是空谈。
- 建议创建自定义实体(比如
@ingredients),把常见食材都加进去,还可以加同义词(比如"salt"和"table salt"),Watson会自动识别用户输入里的食材并提取。 - 在对话节点里,用
$entities.ingredients.values来获取识别到的食材,把它们追加到Ingrediente数组中,比如用Watson的上下文设置:
这样每次用户提到新食材,都会自动加入数组。"Ingrediente": "@append($Ingrediente, $entities.ingredients.values[0].value)"
2. 用Dialog节点的条件逻辑实现基础菜品匹配
如果你的菜品规则比较简单(比如固定食材组合对应固定菜品),直接用Watson Dialog节点的条件判断就能搞定:
- 创建一个专门的“菜品匹配”节点,条件设置为检查
Ingrediente数组包含指定食材,比如:$Ingrediente contains "salt" && $Ingrediente contains "oil" - 在这个节点的“Set context”里,把对应菜品追加到
ListaPlatillos数组:"ListaPlatillos": "@append($ListaPlatillos, "Pan-fried Vegetables")" - 可以给不同的食材组合创建多个分支节点,比如单独有"salt"对应“Boiled Eggs”,有"oil"对应“Stir-fried Rice”等。
3. 用脚本节点处理复杂多食材匹配
如果你的菜品规则比较复杂(比如需要匹配多种食材组合,或者有优先级),推荐用Watson的Script节点(支持JavaScript)来处理:
- 添加一个Script节点,在里面编写逻辑遍历
Ingrediente数组,匹配菜品规则:// 获取当前食材数组和菜品数组 const ingredients = context.Ingrediente || []; let dishes = context.ListaPlatillos || []; // 定义菜品匹配规则 const dishRules = [ { ingredients: ["salt", "oil"], dish: "Pan-fried Vegetables" }, { ingredients: ["salt", "flour"], dish: "Bread" }, { ingredients: ["oil", "garlic"], dish: "Garlic Fried Noodles" } ]; // 遍历规则,匹配食材 dishRules.forEach(rule => { const hasAllIngredients = rule.ingredients.every(ing => ingredients.includes(ing)); if (hasAllIngredients && !dishes.includes(rule.dish)) { dishes.push(rule.dish); } }); // 更新上下文里的菜品数组 context.ListaPlatillos = dishes; - 这种方式的好处是可以灵活扩展规则,比如添加食材数量要求、优先级排序等,比单纯用Dialog节点更灵活。
4. 优化用户体验的小技巧
- 在匹配完成后,给用户反馈匹配结果,比如节点回复:
"Based on your ingredients, you can make: $ListaPlatillos.join(', ')" - 如果没有匹配到菜品,可以设置一个兜底节点,回复类似
"Sorry, I don't have any recipes for your current ingredients. Would you like to add more?"
内容的提问来源于stack exchange,提问作者IvanHid
相关产品推荐
相关产品推荐

