-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #285 from PrefectHQ/planning
Update planning module
- Loading branch information
Showing
11 changed files
with
274 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,45 @@ | ||
--- | ||
title: Planning | ||
title: AI Planning | ||
description: Use AI to generate new tasks. | ||
icon: compass | ||
--- | ||
|
||
<Tip> | ||
Automatically generate subtasks to break complex tasks into manageable steps. | ||
</Tip> | ||
The `plan()` function in ControlFlow extends the capabilities of AI workflows by allowing dynamic generation of tasks. This feature allows you to leverage AI for creating structured, goal-oriented task sequences programmatically. | ||
|
||
ControlFlow has many features that help you structure your AI workflows into small, well-defined tasks. However, sometimes it will be impractical or even impossible to identify all the necessary tasks upfront. In these cases, you can use ControlFlow's planning capabilities to automatically generate subtasks based on the main task's objective. | ||
## Purpose of AI planning | ||
|
||
By calling the `generate_subtasks()` method on a task, you can instruct an AI agent to come up with a plan for achieving the main task. The agent will generate a series of subtasks, each representing a step in the plan, and set up the necessary dependencies between them. | ||
While ControlFlow allows manual creation of tasks for AI workflows, there are scenarios where automatically generating tasks can be beneficial: | ||
|
||
1. **Dynamic Task Generation**: When the specific steps to achieve a goal aren't known in advance. | ||
2. **Complex Problem Decomposition**: For objectives that require breaking down into subtasks based on context or intermediate results. | ||
3. **Adaptive Workflows**: In processes that need to adjust based on changing conditions or new information. | ||
|
||
|
||
## The `plan()` function | ||
|
||
The `plan()` function takes a high-level objective and generates a structured sequence of tasks to achieve that goal. Here's a basic example: | ||
|
||
```python | ||
import controlflow as cf | ||
|
||
task = cf.Task( | ||
objective="Compare the height of the tallest building " | ||
"in North America to the tallest building in Europe", | ||
tasks = cf.plan( | ||
objective="Analyze customer feedback data", | ||
n_tasks=3 # Optionally specify the number of tasks | ||
) | ||
|
||
task.generate_subtasks() | ||
|
||
print([f'{i+1}: {t.objective}' for i, t in enumerate(task.subtasks)]) | ||
# Execute the generated plan | ||
cf.run_tasks(tasks) | ||
``` | ||
|
||
Running the above code will print something like: | ||
|
||
```python | ||
[ | ||
"1: Identify the Tallest Building in North America", | ||
"2: Identify the Tallest Building in Europe", | ||
"3: Obtain Height of the Tallest Building in North America", | ||
"4: Obtain Height of the Tallest Building in Europe", | ||
"5: Compare the Heights", | ||
] | ||
``` | ||
If you investigate more closely, you'll see that the subtasks have proper dependencies. In the above example, #3 depends on #1, #4 depends on #2, and #5 depends on #3 and #4. And of course, the parent task depends on all of them. | ||
|
||
ControlFlow's orchestration engine will not allow the parent task to be considered complete until all of its subtasks have been successfully executed, which is why this is an effective way to structure complex workflows. | ||
|
||
<Tip> | ||
Subtask generation isn't magic: it's a ControlFlow flow! | ||
</Tip> | ||
|
||
## Customizing subtask generation | ||
You can influence subtask generation in a few ways. | ||
In this example, `plan()` will generate a list of 3 tasks that, when completed, should result in an analysis of customer feedback data. These tasks might include steps like "Load data", "Preprocess text", "Perform sentiment analysis", etc. | ||
|
||
### Planning agent | ||
## Advanced usage | ||
|
||
By default, subtasks are generated by the first agent assigned to the parent task. You can customize this by passing an `agent` argument to `generate_subtasks()`. | ||
The `plan` function can do more than just generate tasks that achieve an objective. | ||
|
||
### Dependencies | ||
|
||
### Instructions | ||
If appropriate, `plan` can generate tasks that depend on each other or have parent/child relationships. You can influence this behavior by providing `instructions`. | ||
|
||
You can provide natural language `instructions` to help the agent generate subtasks. This is especially useful when the task is ambiguous or requires domain-specific knowledge. | ||
### Agents and tools | ||
You can pass a list of agents or tools to the `plan` function. It will take these into account when generating tasks and assign agents or tools to tasks as needed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .run import run, run_async, run_tasks, run_tasks_async | ||
from .plan import plan |
Oops, something went wrong.