如何使用 Python 在 Alexa 中添加动态实体?

How to add Dynamic Entities in Alexa with Python?

我正在 Python 开发 Alexa Skill,我需要为用户动态添加实体以更新插槽类型,以便用户可以选择一个选项。

我在 Use Dynamic Entities for Customized Interactions there is documentation and example for Node.js and Java, but no examples for Python. Looking at the Documentation 页面 Python SDK,我不清楚如何在 Python 中做同样的事情。

我创建了一个名为 test 的插槽类型并尝试了以下代码:

test_directive = {"object_type": "Dialog.UpdateDynamicEntities", "update_behavior": "REPLACE", "types": [{"name": "test", "values": [{"id": "round-rock", "name": {"value": "Round Rock Express", "synonyms": ["Round Rock", "Express"]}}, {"id": "corpus-christi", "name": {"value": "Corpus Christi Hooks", "synonyms": ["Corpus Christi", "Hooks", "Corpus"]}}]}]}

response_builder.add_directive(test_directive)

但我收到错误消息:

'dict' object has no attribute 'object_type'

在 Python 中添加动态实体的正确方法是什么?

我在 link 中找到了答案:https://forums.developer.amazon.com/questions/210684/dynamic-entities-in-python.html。 Python中的一个简短示例:

entity_synonyms_1 = EntityValueAndSynonyms(value="entity 1", synonyms=["synonym 1", "synonym 2"])
entity_1 = Entity(id="entity_id_1", name=entity_synonyms_1)

entity_synonyms_2 = EntityValueAndSynonyms(value="entity 2", synonyms=["synonym 3", "synonym 4"])
entity_2 = Entity(id="entity_id_2", name=entity_synonyms_2)                

replace_entity_directive = DynamicEntitiesDirective(update_behavior=UpdateBehavior.REPLACE, types=[EntityListItem(name="custom_type", values=[entity_1, entity_2])])

response_builder.add_directive(replace_entity_directive)