MapOperator
算子是最通用的算子,常用于将函数应用于输入数据。
有两种方式可以使用 MapOpeartor
算子
使用Map函数构建MapOperator
from dbgpt.core.awel import DAG, MapOperator
with DAG("awel_hello_world") as dag:
task = MapOperator(map_function=lambda x: print(f"Hello, {x}!"))
实现一个自定义MapOperator
from dbgpt.core.awel import DAG, MapOperator
class MyMapOperator(MapOperator[str, None]):
async def map(self, x: str) -> None:
print(f"Hello, {x}!")
with DAG("awel_hello_world") as dag:
task = MyMapOperator()
例子
数字加倍
在awel_tutorial
目录创建一个新的文件,命名为map_operator_double_number.py
, 添加如下代码
import asyncio
from dbgpt.core.awel import DAG, MapOperator
class DoubleNumberOperator(MapOperator[int, int]):
async def map(self, x: int) -> int:
print(f"Received {x}, returning {x * 2}")
return x * 2
with DAG("awel_double_number") as dag:
task = DoubleNumberOperator()
assert asyncio.run(task.call(2)) == 4
运行下面的命令执行对应的代码, 可以看到如下的输出 “Received 2, returning 4“
poetry run python awel_tutorial/map_operator_double_number.py
Received 2, returning 4