mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-28 17:48:13 +00:00
Clean up docs
This commit is contained in:
@@ -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.
|
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
|
```python Code
|
||||||
import asyncio
|
|
||||||
|
|
||||||
from crewai.flow.flow import Flow, listen, start
|
from crewai.flow.flow import Flow, listen, start
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from litellm import completion
|
from litellm import completion
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
|
|
||||||
|
|
||||||
class ExampleFlow(Flow):
|
class ExampleFlow(Flow):
|
||||||
model = "gpt-4o-mini"
|
model = "gpt-4o-mini"
|
||||||
@@ -70,13 +67,11 @@ class ExampleFlow(Flow):
|
|||||||
return fun_fact
|
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.
|
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>
|
<CodeGroup>
|
||||||
```python Code
|
```python Code
|
||||||
import asyncio
|
|
||||||
from crewai.flow.flow import Flow, listen, start
|
from crewai.flow.flow import Flow, listen, start
|
||||||
|
|
||||||
class OutputExampleFlow(Flow):
|
class OutputExampleFlow(Flow):
|
||||||
@start()
|
@start()
|
||||||
def first_method(self):
|
def first_method(self):
|
||||||
return "Output from first_method"
|
return "Output from first_method"
|
||||||
|
|
||||||
@listen(first_method)
|
@listen(first_method)
|
||||||
def second_method(self, first_output):
|
def second_method(self, first_output):
|
||||||
return f"Second method received: {first_output}"
|
return f"Second method received: {first_output}"
|
||||||
|
|
||||||
async def main():
|
|
||||||
flow = OutputExampleFlow()
|
flow = OutputExampleFlow()
|
||||||
final_output = await flow.kickoff()
|
final_output = flow.kickoff()
|
||||||
|
|
||||||
print("---- Final Output ----")
|
print("---- Final Output ----")
|
||||||
print(final_output)
|
print(final_output)
|
||||||
|
|
||||||
asyncio.run(main())
|
|
||||||
|
|
||||||
````
|
````
|
||||||
|
|
||||||
``` text Output
|
``` text Output
|
||||||
@@ -165,7 +157,6 @@ Here's an example of how to update and access the state:
|
|||||||
<CodeGroup>
|
<CodeGroup>
|
||||||
|
|
||||||
```python Code
|
```python Code
|
||||||
import asyncio
|
|
||||||
from crewai.flow.flow import Flow, listen, start
|
from crewai.flow.flow import Flow, listen, start
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
@@ -186,14 +177,11 @@ class StateExampleFlow(Flow[ExampleState]):
|
|||||||
self.state.counter += 1
|
self.state.counter += 1
|
||||||
return self.state.message
|
return self.state.message
|
||||||
|
|
||||||
async def main():
|
flow = StateExampleFlow()
|
||||||
flow = StateExampleFlow()
|
final_output = flow.kickoff()
|
||||||
final_output = await flow.kickoff()
|
print(f"Final Output: {final_output}")
|
||||||
print(f"Final Output: {final_output}")
|
print("Final State:")
|
||||||
print("Final State:")
|
print(flow.state)
|
||||||
print(flow.state)
|
|
||||||
|
|
||||||
asyncio.run(main())
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```text Output
|
```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.
|
This approach offers flexibility, enabling developers to add or modify state attributes on the fly without defining a strict schema.
|
||||||
|
|
||||||
```python Code
|
```python Code
|
||||||
import asyncio
|
|
||||||
|
|
||||||
from crewai.flow.flow import Flow, listen, start
|
from crewai.flow.flow import Flow, listen, start
|
||||||
|
|
||||||
class UntructuredExampleFlow(Flow):
|
class UntructuredExampleFlow(Flow):
|
||||||
@@ -245,12 +231,8 @@ class UntructuredExampleFlow(Flow):
|
|||||||
print(f"State after third_method: {self.state}")
|
print(f"State after third_method: {self.state}")
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
flow = UntructuredExampleFlow()
|
||||||
flow = UntructuredExampleFlow()
|
flow.kickoff()
|
||||||
await flow.kickoff()
|
|
||||||
|
|
||||||
|
|
||||||
asyncio.run(main())
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Key Points:**
|
**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.
|
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
|
```python Code
|
||||||
import asyncio
|
|
||||||
|
|
||||||
from crewai.flow.flow import Flow, listen, start
|
from crewai.flow.flow import Flow, listen, start
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
@@ -294,12 +274,8 @@ class StructuredExampleFlow(Flow[ExampleState]):
|
|||||||
print(f"State after third_method: {self.state}")
|
print(f"State after third_method: {self.state}")
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
flow = StructuredExampleFlow()
|
||||||
flow = StructuredExampleFlow()
|
flow.kickoff()
|
||||||
await flow.kickoff()
|
|
||||||
|
|
||||||
|
|
||||||
asyncio.run(main())
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Key Points:**
|
**Key Points:**
|
||||||
@@ -332,7 +308,6 @@ The `or_` function in Flows allows you to listen to multiple methods and trigger
|
|||||||
<CodeGroup>
|
<CodeGroup>
|
||||||
|
|
||||||
```python Code
|
```python Code
|
||||||
import asyncio
|
|
||||||
from crewai.flow.flow import Flow, listen, or_, start
|
from crewai.flow.flow import Flow, listen, or_, start
|
||||||
|
|
||||||
class OrExampleFlow(Flow):
|
class OrExampleFlow(Flow):
|
||||||
@@ -350,12 +325,9 @@ class OrExampleFlow(Flow):
|
|||||||
print(f"Logger: {result}")
|
print(f"Logger: {result}")
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
flow = OrExampleFlow()
|
|
||||||
await flow.kickoff()
|
|
||||||
|
|
||||||
|
flow = OrExampleFlow()
|
||||||
asyncio.run(main())
|
flow.kickoff()
|
||||||
```
|
```
|
||||||
|
|
||||||
```text Output
|
```text Output
|
||||||
@@ -375,7 +347,6 @@ The `and_` function in Flows allows you to listen to multiple methods and trigge
|
|||||||
<CodeGroup>
|
<CodeGroup>
|
||||||
|
|
||||||
```python Code
|
```python Code
|
||||||
import asyncio
|
|
||||||
from crewai.flow.flow import Flow, and_, listen, start
|
from crewai.flow.flow import Flow, and_, listen, start
|
||||||
|
|
||||||
class AndExampleFlow(Flow):
|
class AndExampleFlow(Flow):
|
||||||
@@ -393,13 +364,8 @@ class AndExampleFlow(Flow):
|
|||||||
print("---- Logger ----")
|
print("---- Logger ----")
|
||||||
print(self.state)
|
print(self.state)
|
||||||
|
|
||||||
|
flow = AndExampleFlow()
|
||||||
async def main():
|
flow.kickoff()
|
||||||
flow = AndExampleFlow()
|
|
||||||
await flow.kickoff()
|
|
||||||
|
|
||||||
|
|
||||||
asyncio.run(main())
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```text Output
|
```text Output
|
||||||
@@ -420,7 +386,6 @@ You can specify different routes based on the output of the method, allowing you
|
|||||||
<CodeGroup>
|
<CodeGroup>
|
||||||
|
|
||||||
```python Code
|
```python Code
|
||||||
import asyncio
|
|
||||||
import random
|
import random
|
||||||
from crewai.flow.flow import Flow, listen, router, start
|
from crewai.flow.flow import Flow, listen, router, start
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@@ -452,12 +417,8 @@ class RouterFlow(Flow[ExampleState]):
|
|||||||
print("Fourth method running")
|
print("Fourth method running")
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
flow = RouterFlow()
|
||||||
flow = RouterFlow()
|
flow.kickoff()
|
||||||
await flow.kickoff()
|
|
||||||
|
|
||||||
|
|
||||||
asyncio.run(main())
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```text Output
|
```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
|
```python Code
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import asyncio
|
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
@@ -541,7 +501,6 @@ 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)
|
||||||
@@ -558,22 +517,17 @@ class PoemFlow(Flow[PoemState]):
|
|||||||
with open("poem.txt", "w") as f:
|
with open("poem.txt", "w") as f:
|
||||||
f.write(self.state.poem)
|
f.write(self.state.poem)
|
||||||
|
|
||||||
async def kickoff():
|
def kickoff():
|
||||||
poem_flow = PoemFlow()
|
poem_flow = PoemFlow()
|
||||||
await poem_flow.kickoff()
|
poem_flow.kickoff()
|
||||||
|
|
||||||
|
|
||||||
def plot():
|
def plot():
|
||||||
poem_flow = PoemFlow()
|
poem_flow = PoemFlow()
|
||||||
poem_flow.plot()
|
poem_flow.plot()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
asyncio.run(kickoff())
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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.
|
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.
|
||||||
|
|||||||
Reference in New Issue
Block a user