42 lines
1.3 KiB
Markdown
42 lines
1.3 KiB
Markdown
---
|
|
layout: default
|
|
title: "Task Decomposition"
|
|
parent: "Design"
|
|
nav_order: 2
|
|
---
|
|
|
|
# Task Decomposition
|
|
|
|
Many real-world tasks are too complex for one LLM call. The solution is to decompose them into multiple calls as a [Flow](./flow.md) of Nodes.
|
|
|
|
### Example: Article Writing
|
|
|
|
```python
|
|
class GenerateOutline(Node):
|
|
def prep(self, shared): return shared["topic"]
|
|
def exec(self, topic): return call_llm(f"Create a detailed outline for an article about {topic}")
|
|
def post(self, shared, prep_res, exec_res): shared["outline"] = exec_res
|
|
|
|
class WriteSection(Node):
|
|
def prep(self, shared): return shared["outline"]
|
|
def exec(self, outline): return call_llm(f"Write content based on this outline: {outline}")
|
|
def post(self, shared, prep_res, exec_res): shared["draft"] = exec_res
|
|
|
|
class ReviewAndRefine(Node):
|
|
def prep(self, shared): return shared["draft"]
|
|
def exec(self, draft): return call_llm(f"Review and improve this draft: {draft}")
|
|
def post(self, shared, prep_res, exec_res): shared["final_article"] = exec_res
|
|
|
|
# Connect nodes
|
|
outline = GenerateOutline()
|
|
write = WriteSection()
|
|
review = ReviewAndRefine()
|
|
|
|
outline >> write >> review
|
|
|
|
# Create and run flow
|
|
writing_flow = Flow(start=outline)
|
|
shared = {"topic": "AI Safety"}
|
|
writing_flow.run(shared)
|
|
```
|