Clean up docs

This commit is contained in:
Brandon Hancock
2024-10-21 17:22:48 -04:00
parent 056a8e734b
commit eb13dbb223

View File

@@ -23,14 +23,11 @@ Flows allow you to create structured, event-driven workflows. They provide a sea
Let's create a simple Flow where you will use OpenAI to generate a random city in one task and then use that city to generate a fun fact in another task.
```python Code
import asyncio
from crewai.flow.flow import Flow, listen, start
from dotenv import load_dotenv
from litellm import completion
load_dotenv()
class ExampleFlow(Flow):
model = "gpt-4o-mini"
@@ -70,13 +67,11 @@ class ExampleFlow(Flow):
return fun_fact
async def main():
flow = ExampleFlow()
result = await flow.kickoff()
print(f"Generated fun fact: {result}")
flow = ExampleFlow()
result = flow.kickoff()
asyncio.run(main())
print(f"Generated fun fact: {result}")
```
In the above example, we have created a simple Flow that generates a random city using OpenAI and then generates a fun fact about that city. The Flow consists of two tasks: `generate_city` and `generate_fun_fact`. The `generate_city` task is the starting point of the Flow, and the `generate_fun_fact` task listens for the output of the `generate_city` task.
@@ -124,26 +119,23 @@ Here's how you can access the final output:
<CodeGroup>
```python Code
import asyncio
from crewai.flow.flow import Flow, listen, start
class OutputExampleFlow(Flow):
@start()
def first_method(self):
return "Output from first_method"
@start()
def first_method(self):
return "Output from first_method"
@listen(first_method)
def second_method(self, first_output):
return f"Second method received: {first_output}"
async def main():
flow = OutputExampleFlow()
final_output = await flow.kickoff()
final_output = flow.kickoff()
print("---- Final Output ----")
print(final_output)
asyncio.run(main())
````
``` text Output
@@ -165,7 +157,6 @@ Here's an example of how to update and access the state:
<CodeGroup>
```python Code
import asyncio
from crewai.flow.flow import Flow, listen, start
from pydantic import BaseModel
@@ -186,14 +177,11 @@ class StateExampleFlow(Flow[ExampleState]):
self.state.counter += 1
return self.state.message
async def main():
flow = StateExampleFlow()
final_output = await flow.kickoff()
print(f"Final Output: {final_output}")
print("Final State:")
print(flow.state)
asyncio.run(main())
flow = StateExampleFlow()
final_output = flow.kickoff()
print(f"Final Output: {final_output}")
print("Final State:")
print(flow.state)
```
```text Output
@@ -221,8 +209,6 @@ In unstructured state management, all state is stored in the `state` attribute o
This approach offers flexibility, enabling developers to add or modify state attributes on the fly without defining a strict schema.
```python Code
import asyncio
from crewai.flow.flow import Flow, listen, start
class UntructuredExampleFlow(Flow):
@@ -245,12 +231,8 @@ class UntructuredExampleFlow(Flow):
print(f"State after third_method: {self.state}")
async def main():
flow = UntructuredExampleFlow()
await flow.kickoff()
asyncio.run(main())
flow = UntructuredExampleFlow()
flow.kickoff()
```
**Key Points:**
@@ -264,8 +246,6 @@ Structured state management leverages predefined schemas to ensure consistency a
By using models like Pydantic's `BaseModel`, developers can define the exact shape of the state, enabling better validation and auto-completion in development environments.
```python Code
import asyncio
from crewai.flow.flow import Flow, listen, start
from pydantic import BaseModel
@@ -294,12 +274,8 @@ class StructuredExampleFlow(Flow[ExampleState]):
print(f"State after third_method: {self.state}")
async def main():
flow = StructuredExampleFlow()
await flow.kickoff()
asyncio.run(main())
flow = StructuredExampleFlow()
flow.kickoff()
```
**Key Points:**
@@ -332,7 +308,6 @@ The `or_` function in Flows allows you to listen to multiple methods and trigger
<CodeGroup>
```python Code
import asyncio
from crewai.flow.flow import Flow, listen, or_, start
class OrExampleFlow(Flow):
@@ -350,12 +325,9 @@ class OrExampleFlow(Flow):
print(f"Logger: {result}")
async def main():
flow = OrExampleFlow()
await flow.kickoff()
asyncio.run(main())
flow = OrExampleFlow()
flow.kickoff()
```
```text Output
@@ -375,7 +347,6 @@ The `and_` function in Flows allows you to listen to multiple methods and trigge
<CodeGroup>
```python Code
import asyncio
from crewai.flow.flow import Flow, and_, listen, start
class AndExampleFlow(Flow):
@@ -393,13 +364,8 @@ class AndExampleFlow(Flow):
print("---- Logger ----")
print(self.state)
async def main():
flow = AndExampleFlow()
await flow.kickoff()
asyncio.run(main())
flow = AndExampleFlow()
flow.kickoff()
```
```text Output
@@ -420,7 +386,6 @@ You can specify different routes based on the output of the method, allowing you
<CodeGroup>
```python Code
import asyncio
import random
from crewai.flow.flow import Flow, listen, router, start
from pydantic import BaseModel
@@ -452,12 +417,8 @@ class RouterFlow(Flow[ExampleState]):
print("Fourth method running")
async def main():
flow = RouterFlow()
await flow.kickoff()
asyncio.run(main())
flow = RouterFlow()
flow.kickoff()
```
```text Output
@@ -525,7 +486,6 @@ Here's an example of how you can connect the `poem_crew` in the `main.py` file:
```python Code
#!/usr/bin/env python
import asyncio
from random import randint
from pydantic import BaseModel
@@ -541,7 +501,6 @@ class PoemFlow(Flow[PoemState]):
@start()
def generate_sentence_count(self):
print("Generating sentence count")
# Generate a number between 1 and 5
self.state.sentence_count = randint(1, 5)
@listen(generate_sentence_count)
@@ -558,22 +517,17 @@ class PoemFlow(Flow[PoemState]):
with open("poem.txt", "w") as f:
f.write(self.state.poem)
async def kickoff():
def kickoff():
poem_flow = PoemFlow()
await poem_flow.kickoff()
poem_flow.kickoff()
def plot():
poem_flow = PoemFlow()
poem_flow.plot()
def main():
asyncio.run(kickoff())
if __name__ == "__main__":
main()
kickoff()
```
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.