more flow doc changes

This commit is contained in:
Brandon Hancock
2024-09-23 15:21:54 -04:00
parent 6ba6ac7fcc
commit bfaba72da2

View File

@@ -175,9 +175,8 @@ class StateExampleFlow(Flow[ExampleState]):
async def main(): async def main():
flow = StateExampleFlow() flow = StateExampleFlow()
final_output = await flow.kickoff() final_output = await flow.kickoff()
print("---- Final Output ----") print(f"Final Output: {final_output}")
print(final_output) print("Final State:")
print("---- Final State ----")
print(flow.state) print(flow.state)
asyncio.run(main()) asyncio.run(main())
@@ -188,9 +187,8 @@ In this example, the state is updated by both `first_method` and `second_method`
The output of the Flow will be: The output of the Flow will be:
``` ```
---- Final Output ---- Final Output: Hello from first_method - updated by second_method
Hello from first_method - updated by second_method Final State:
---- Final State ----
counter=2 message='Hello from first_method - updated by second_method' counter=2 message='Hello from first_method - updated by second_method'
``` ```
@@ -491,37 +489,25 @@ The `main.py` file is where you create your flow and connect the crews together.
Here's an example of how you can connect the `poem_crew` in the `main.py` file: Here's an example of how you can connect the `poem_crew` in the `main.py` file:
```python:src/crewai/cli/templates/flow/main.py
startLine: 1
endLine: 54
```
In this example, the `PoemFlow` class defines a flow that generates a sentence count, uses the `PoemCrew` to generate a poem, and then saves the poem to a file. The flow is kicked off by calling the `kickoff()` method.
### Customizing Your Flow
You can customize your flow by adding more crews and defining additional methods in the `main.py` file. Use the `@start` decorator to mark the starting methods of your flow and the `@listen` decorator to define methods that should be triggered by the output of other methods.
For example, you can add another crew and connect it to the existing flow:
```python ```python
#!/usr/bin/env python
import asyncio import asyncio
from random import randint from random import randint
from pydantic import BaseModel from pydantic import BaseModel
from crewai.flow.flow import Flow, listen, start from crewai.flow.flow import Flow, listen, start
from .crews.poem_crew.poem_crew import PoemCrew from .crews.poem_crew.poem_crew import PoemCrew
from .crews.another_crew.another_crew import AnotherCrew
class ExampleState(BaseModel): class PoemState(BaseModel):
sentence_count: int = 1 sentence_count: int = 1
poem: str = "" poem: str = ""
analysis: str = ""
class ExampleFlow(Flow[ExampleState]): class PoemFlow(Flow[PoemState]):
@start() @start()
def generate_sentence_count(self): def generate_sentence_count(self):
print("Generating sentence count") print("Generating sentence count")
# Generate a number between 1 and 5
self.state.sentence_count = randint(1, 5) self.state.sentence_count = randint(1, 5)
@listen(generate_sentence_count) @listen(generate_sentence_count)
@@ -529,31 +515,31 @@ class ExampleFlow(Flow[ExampleState]):
print("Generating poem") print("Generating poem")
poem_crew = PoemCrew().crew() poem_crew = PoemCrew().crew()
result = poem_crew.kickoff(inputs={"sentence_count": self.state.sentence_count}) result = poem_crew.kickoff(inputs={"sentence_count": self.state.sentence_count})
print("Poem generated", result.raw)
self.state.poem = result.raw self.state.poem = result.raw
@listen(generate_poem) @listen(generate_poem)
def analyze_poem(self): def save_poem(self):
print("Analyzing poem") print("Saving poem")
analysis_crew = AnotherCrew().crew() with open("poem.txt", "w") as f:
result = analysis_crew.kickoff(inputs={"poem": self.state.poem}) f.write(self.state.poem)
self.state.analysis = result.raw
@listen(analyze_poem) async def run():
def save_results(self): """
print("Saving results") Run the flow.
with open("results.txt", "w") as f: """
f.write(f"Poem: {self.state.poem}\nAnalysis: {self.state.analysis}") poem_flow = PoemFlow()
await poem_flow.kickoff()
async def main(): def main():
flow = ExampleFlow() asyncio.run(run())
await flow.kickoff()
asyncio.run(main()) if __name__ == "__main__":
main()
``` ```
In this example, the `ExampleFlow` class defines a flow that generates a sentence count, uses the `PoemCrew` to generate a poem, uses another crew (`AnotherCrew`) to analyze the poem, and then saves the results to a file. In this example, the `PoemFlow` class defines a flow that generates a sentence count, uses the `PoemCrew` to generate a poem, and then saves the poem to a file. The flow is kicked off by calling the `kickoff()` method.
By following this structure, you can easily build complex flows that connect multiple crews and manage the flow of execution in your AI applications.
## Next Steps ## Next Steps