-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathworkflow_orchestrator.py
More file actions
84 lines (57 loc) · 2.14 KB
/
Copy pathworkflow_orchestrator.py
File metadata and controls
84 lines (57 loc) · 2.14 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import json
import restate
from agents import Agent
from pydantic import BaseModel
from restate.ext.openai import DurableRunner
class ReportRequest(BaseModel):
topic: str = "The impact of renewable energy on global economies"
class ResearchTask(BaseModel):
question: str
class TaskList(BaseModel):
tasks: list[ResearchTask]
# <start_here>
planner = Agent(
name="ResearchPlanner",
instructions="You are a research planner. Break the topic into 2-4 research sub-tasks.",
output_type=TaskList,
)
researcher = Agent(
name="Researcher",
instructions="You are a research assistant. Provide a concise, factual answer.",
)
writer = Agent(
name="ReportWriter",
instructions="You are a report writer. Combine the research findings into a cohesive report.",
)
report_service = restate.Service("ResearchReport")
@report_service.handler()
async def generate(ctx: restate.Context, req: ReportRequest) -> dict:
# Step 1: Orchestrator creates a research plan
plan_result = await DurableRunner.run(planner, req.topic)
tasks = plan_result.final_output.tasks
# Step 2: Dispatch workers in parallel
worker_promises = []
for task in tasks:
promise = ctx.service_call(run_researcher, task)
worker_promises.append(promise)
await restate.gather(*worker_promises)
findings = [await p for p in worker_promises]
# Step 3: Combine results into a report
report_result = await DurableRunner.run(
writer,
f"Topic: {req.topic}\n\nResearch findings:\n{json.dumps(findings, indent=2)}",
)
return {"report": report_result.final_output, "task_count": len(tasks)}
researcher_service = restate.Service("Researcher")
@researcher_service.handler()
async def run_researcher(ctx: restate.Context, task: ResearchTask) -> str:
result = await DurableRunner.run(researcher, task.question)
return result.final_output
# <end_here>
if __name__ == "__main__":
import hypercorn
import asyncio
app = restate.app(services=[report_service, researcher_service])
conf = hypercorn.Config()
conf.bind = ["0.0.0.0:9080"]
asyncio.run(hypercorn.asyncio.serve(app, conf))