-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfan_out_edges_selective.py
More file actions
56 lines (40 loc) · 1.48 KB
/
Copy pathfan_out_edges_selective.py
File metadata and controls
56 lines (40 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from enum import IntEnum
from typing import Never
from agent_framework import (
WorkflowBuilder,
WorkflowContext,
executor,
)
import asyncio
class Priority(IntEnum):
LOW = 1
NORMAL = 5
HIGH = 10
@executor
async def splitter_executor(work_message: str, ctx: WorkflowContext[str]) -> None:
await ctx.send_message(work_message)
@executor(id="summarizer")
async def summarizer(work_message: str, ctx: WorkflowContext[Never, str]) -> None:
await ctx.yield_output(f'worker 1 summarized: {work_message}')
@executor(id="sentiment_detector")
async def sentiment_detector(work_message: str, ctx: WorkflowContext[Never, str]) -> None:
await ctx.yield_output(f'worker 2 detected sentiment: {work_message}')
@executor(id="keyword_finder")
async def keyword_finder(work_message: str, ctx: WorkflowContext[Never,str]) -> None:
await ctx.yield_output(f'worker 3 found keywords: {work_message}')
async def main():
# Send to specific targets based on partitioner function
builder = WorkflowBuilder()
builder.set_start_executor(splitter_executor)
builder.add_fan_out_edges(
splitter_executor,
[summarizer, sentiment_detector, keyword_finder],
selection_func=lambda message, target_ids: (
[0, 1, 2] if len(message) > 5 else
[1, 2] if message.priority == Priority.NORMAL else
list(range(len(target_ids)))
)
)
workflow = builder.build()
if __name__ == "__main__":
asyncio.run(main())