griptape-ai / griptape

Modular Python framework for AI agents and workflows with chain-of-thought reasoning, tools, and memory.
https://www.griptape.ai
Apache License 2.0
1.95k stars 160 forks source link

Don't require passing `tasks` on `Workflow()` if using imperative syntax. #896

Closed vachillo closed 3 months ago

vachillo commented 3 months ago

Is your feature request related to a problem? Please describe. When using the imperative syntax to build a Workflow, you should not have to also explicitly list all tasks in the workflow as well.

Describe the solution you'd like For example (taken from the docs):

from griptape.tasks import PromptTask
from griptape.structures import Workflow
from griptape.rules import Rule

animal_task = PromptTask("Name an animal", id="animal")
adjective_task = PromptTask("Describe {{ parent_outputs['animal'] }} with an adjective", id="adjective")
new_animal_task = PromptTask("Name a {{ parent_outputs['adjective'] }} animal", id="new-animal")

adjective_task.add_parent(animal_task)
new_animal_task.add_parent(adjective_task)

workflow = Workflow(
    tasks=[animal_task, adjective_task, new_animal_task],
    rules=[Rule("output a single lowercase word")],
)

workflow.run()

the explicit passing of the list of tasks should not be required and instead it should be possible to do this:

from griptape.tasks import PromptTask
from griptape.structures import Workflow
from griptape.rules import Rule

workflow = Workflow( 
    rules=[Rule("output a single lowercase word")],
)

animal_task = PromptTask("Name an animal", id="animal")
adjective_task = PromptTask("Describe {{ parent_outputs['animal'] }} with an adjective", id="adjective")
new_animal_task = PromptTask("Name a {{ parent_outputs['adjective'] }} animal", id="new-animal")

workflow.add_task(animal_task)
adjective_task.add_parent(animal_task)
new_animal_task.add_parent(adjective_task)

workflow.run()

where the only task added to the workflow contains references to its other tasks, and the workflow should work out the DAG by itself.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

shhlife commented 3 months ago

I love this enhancement idea - it'll make things much more intuitive! :)

collindutter commented 3 months ago

I think this should be implemented via the following:

  1. Change BaseTask.parents/children from a property to an attrs field.
  2. Change BaseTask.parent_ids/child_ids from an attrs field to a property.
  3. Change BaseTask.add_parent/add_child to only accept task: BaseTask. Add the Task to BaseTask.parents/children.
  4. Add Structure.resolve_tasks() that recursively explores all children of the Tasks in Structure.tasks. If a child is not found in Structure.tasks, add it.
vachillo commented 3 months ago

focusing on improving documentation rather than changing the syntax. closes #922