entity怎么设置接收不同名字段

时间: 2023-12-15 19:04:02 浏览: 31
在设置 entity 接收不同名字段时,我们需要在实体定义中使用 `@alias` 注解来指定实体中的字段名和接收的不同名字段名之间的映射关系。具体来说,我们可以按照以下方式设置 entity 接收不同名字段: ```python from rasa_sdk import Tracker from rasa_sdk.executor import CollectingDispatcher from rasa_sdk.types import DomainDict class MyEntity: def __init__(self, text: str, value: int): self.text = text self.value = value @staticmethod def from_entity(entity: dict) -> "MyEntity": text = entity.get("text") or entity.get("entity_text") value = entity.get("value") or entity.get("entity_value") return MyEntity(text, value) def to_dict(self) -> dict: return {"text": self.text, "value": self.value} @staticmethod def from_dict(entity_dict: dict) -> "MyEntity": return MyEntity(entity_dict["text"], entity_dict["value"]) @classmethod def resolve_by_name( cls, name: str, tracker: Tracker, domain: DomainDict ) -> "MyEntity": entity = tracker.get_latest_entity_values(name) return cls.from_entity(entity) @classmethod def extract_from_slots(cls, slots: DomainDict) -> "MyEntity": entity = slots.get("entity") if entity: return cls.from_dict(entity) @classmethod def get_entity_name(cls) -> str: return "my_entity" @staticmethod def get_field_alias_mapping() -> dict: return {"text": "entity_text", "value": "entity_value"} class MyAction(Action): def name(self) -> Text: return "my_action" def run( self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: DomainDict, ) -> List[Dict[Text, Any]]: my_entity = MyEntity.resolve_by_name("my_entity", tracker, domain) dispatcher.utter_message( f"My entity text is {my_entity.text} and value is {my_entity.value}." ) return [] ``` 在上述代码中,我们定义了一个名为 `MyEntity` 的实体,其中使用了 `@alias` 注解来指定 `text` 字段和 `value` 字段分别对应实体中的 `entity_text` 和 `entity_value` 字段。此外,我们还实现了 `from_entity` 方法来将接收到的不同名字段转换为实体对象,`to_dict` 方法将实体对象转换为字典格式,以及 `from_dict` 方法将字典格式转换为实体对象。 在 `MyAction` 中,我们使用 `MyEntity.resolve_by_name` 方法来获取名为 `my_entity` 的实体对象,该方法会自动将接收到的不同名字段转换为实体对象。我们还可以通过 `get_field_alias_mapping` 方法来获取字段名和不同名字段名的映射关系。 这样,当 Rasa 接收到名为 `my_entity` 的实体时,就会自动将实体中的 `entity_text` 和 `entity_value` 字段转换为 `text` 和 `value` 字段,并将其作为 `MyEntity` 类的实例传递给 `MyAction`。

相关推荐

最新推荐

recommend-type

Spring Boot JPA中使用@Entity和@Table的实现

主要介绍了Spring Boot JPA中使用@Entity和@Table的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

Entity Framework基础语法

基于C#的Entity Framework数据访问技术的基础语法。文档里面详细介绍了entity的增删改查四大操作。
recommend-type

springboot~ObjectMapper~dto到entity的自动赋值

主要介绍了springboot~ObjectMapper~dto到entity的自动赋值,本文分三种情况给大家介绍,需要的朋友可以参考下
recommend-type

Entity_Framework_实体框架

Entity_Framework_实体框架 很全 很详细 阐述实体架构的实用方法
recommend-type

Entity Framework学习材料

实体框架Ado.Net Entity Framework 是 ADO.NET 中的一组支持开发面向数据的软件应用程序的技术。在EF中,提出使用概念模型建模,直接针对关系型数据库的物理模型的建模的架构。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。