mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
made more progress updates on docs
This commit is contained in:
@@ -118,7 +118,7 @@ import asyncio
|
|||||||
|
|
||||||
from crewai.flow.flow import Flow, listen, start
|
from crewai.flow.flow import Flow, listen, start
|
||||||
|
|
||||||
class StructuredExampleFlow(Flow):
|
class UntructuredExampleFlow(Flow):
|
||||||
|
|
||||||
@start()
|
@start()
|
||||||
async def first_method(self):
|
async def first_method(self):
|
||||||
@@ -139,7 +139,7 @@ class StructuredExampleFlow(Flow):
|
|||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
flow = StructuredExampleFlow()
|
flow = UntructuredExampleFlow()
|
||||||
await flow.kickoff()
|
await flow.kickoff()
|
||||||
|
|
||||||
|
|
||||||
@@ -214,3 +214,96 @@ asyncio.run(main())
|
|||||||
- You want to leverage IDE features like auto-completion and type checking for better developer experience.
|
- You want to leverage IDE features like auto-completion and type checking for better developer experience.
|
||||||
|
|
||||||
By providing both unstructured and structured state management options, CrewAI Flows empowers developers to build AI workflows that are both flexible and robust, catering to a wide range of application requirements.
|
By providing both unstructured and structured state management options, CrewAI Flows empowers developers to build AI workflows that are both flexible and robust, catering to a wide range of application requirements.
|
||||||
|
|
||||||
|
## Flow Control
|
||||||
|
|
||||||
|
### Conditional Logic
|
||||||
|
|
||||||
|
#### or
|
||||||
|
|
||||||
|
The `or_` function in Flows allows you to listen to multiple methods and trigger the listener method when any of the specified methods emit an output.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import asyncio
|
||||||
|
from crewai.flow.flow import Flow, listen, or_, start
|
||||||
|
|
||||||
|
class OrExampleFlow(Flow):
|
||||||
|
|
||||||
|
@start()
|
||||||
|
def start_method(self):
|
||||||
|
return "Hello from the start method"
|
||||||
|
|
||||||
|
@listen(start_method)
|
||||||
|
def second_method(self):
|
||||||
|
return "Hello from the second method"
|
||||||
|
|
||||||
|
@listen(or_(start_method, second_method))
|
||||||
|
def logger(self, result):
|
||||||
|
print(f"Logger: {result}")
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
flow = OrExampleFlow()
|
||||||
|
await flow.kickoff()
|
||||||
|
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
|
```
|
||||||
|
|
||||||
|
When you run this Flow, the `logger` method will be triggered by the output of either the `start_method` or the `second_method`. The `or_` function is to listen to multiple methods and trigger the listener method when any of the specified methods emit an output.
|
||||||
|
|
||||||
|
The output of the Flow will be:
|
||||||
|
|
||||||
|
```
|
||||||
|
Logger: Hello from the start method
|
||||||
|
Logger: Hello from the second method
|
||||||
|
```
|
||||||
|
|
||||||
|
#### and
|
||||||
|
|
||||||
|
The `and_` function in Flows allows you to listen to multiple methods and trigger the listener method only when all the specified methods emit an output.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import asyncio
|
||||||
|
from crewai.flow.flow import Flow, and_, listen, start
|
||||||
|
|
||||||
|
class AndExampleFlow(Flow):
|
||||||
|
|
||||||
|
@start()
|
||||||
|
def start_method(self):
|
||||||
|
self.state["greeting"] = "Hello from the start method"
|
||||||
|
|
||||||
|
@listen(start_method)
|
||||||
|
def second_method(self):
|
||||||
|
self.state["joke"] = "What do computers eat? Microchips."
|
||||||
|
|
||||||
|
@listen(and_(start_method, second_method))
|
||||||
|
def logger(self):
|
||||||
|
print("---- Logger ----")
|
||||||
|
print(self.state)
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
flow = AndExampleFlow()
|
||||||
|
await flow.kickoff()
|
||||||
|
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
|
```
|
||||||
|
|
||||||
|
When you run this Flow, the `logger` method will be triggered only when both the `start_method` and the `second_method` emit an output. The `and_` function is used to listen to multiple methods and trigger the listener method only when all the specified methods emit an output.
|
||||||
|
|
||||||
|
The output of the Flow will be:
|
||||||
|
|
||||||
|
```
|
||||||
|
---- Logger ----
|
||||||
|
{'greeting': 'Hello from the start method', 'joke': 'What do computers eat? Microchips.'}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Router
|
||||||
|
|
||||||
|
## Adding Crews to Flows
|
||||||
|
|
||||||
|
- The easiest way to create flows with Crews is to use the `crewai create flow <name_of_flow>` command. This will create a new CrewAI project for you that includes a folders for your Crews.
|
||||||
|
|
||||||
|
##
|
||||||
|
|||||||
@@ -1,41 +1,26 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from crewai.flow.flow import Flow, listen, start
|
from crewai.flow.flow import Flow, and_, listen, start
|
||||||
from pydantic import BaseModel
|
|
||||||
|
|
||||||
|
|
||||||
class ExampleState(BaseModel):
|
class AndExampleFlow(Flow):
|
||||||
counter: int = 0
|
|
||||||
message: str = ""
|
|
||||||
|
|
||||||
|
|
||||||
class StructuredExampleFlow(Flow):
|
|
||||||
initial_state = ExampleState
|
|
||||||
|
|
||||||
@start()
|
@start()
|
||||||
async def start_method(self):
|
def start_method(self):
|
||||||
print("Starting the structured flow")
|
self.state["greeting"] = "Hello from the start method"
|
||||||
print(f"State in start_method: {self.state}")
|
|
||||||
self.state.message = "Hello from structured flow"
|
|
||||||
print(f"State after start_method: {self.state}")
|
|
||||||
return "Start result"
|
|
||||||
|
|
||||||
@listen(start_method)
|
@listen(start_method)
|
||||||
async def second_method(self):
|
def second_method(self):
|
||||||
print(f"State before increment: {self.state}")
|
self.state["joke"] = "What do computers eat? Microchips."
|
||||||
self.state.counter += 1
|
|
||||||
self.state.message += " - updated"
|
|
||||||
print(f"State after second_method: {self.state}")
|
|
||||||
return "Second result"
|
|
||||||
|
|
||||||
@listen(start_method and second_method)
|
@listen(and_(start_method, second_method))
|
||||||
async def logger(self):
|
def logger(self):
|
||||||
print("AND METHOD RUNNING")
|
print("---- Logger ----")
|
||||||
print("CURRENT STATE FROM OR: ", self.state)
|
print(self.state)
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
flow = StructuredExampleFlow()
|
flow = AndExampleFlow()
|
||||||
await flow.kickoff()
|
await flow.kickoff()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,45 +1,25 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from crewai.flow.flow import Flow, and_, listen, or_, start
|
from crewai.flow.flow import Flow, listen, or_, start
|
||||||
from pydantic import BaseModel
|
|
||||||
|
|
||||||
|
|
||||||
class ExampleState(BaseModel):
|
class OrExampleFlow(Flow):
|
||||||
counter: int = 0
|
|
||||||
message: str = ""
|
|
||||||
|
|
||||||
|
|
||||||
class StructuredExampleFlow(Flow[ExampleState]):
|
|
||||||
|
|
||||||
@start()
|
@start()
|
||||||
async def start_method(self):
|
def start_method(self):
|
||||||
print("Starting the structured flow")
|
return "Hello from the start method"
|
||||||
print(f"State in start_method: {self.state}")
|
|
||||||
self.state.message = "Hello from structured flow"
|
|
||||||
print(f"State after start_method: {self.state}")
|
|
||||||
return "Start result"
|
|
||||||
|
|
||||||
@listen(start_method)
|
@listen(start_method)
|
||||||
async def second_method(self):
|
def second_method(self):
|
||||||
print(f"State before increment: {self.state}")
|
return "Hello from the second method"
|
||||||
self.state.counter += 1
|
|
||||||
self.state.message += " - updated"
|
|
||||||
print(f"State after second_method: {self.state}")
|
|
||||||
return "Second result"
|
|
||||||
|
|
||||||
@listen(or_(start_method, second_method))
|
@listen(or_(start_method, second_method))
|
||||||
async def logger(self):
|
def logger(self, result):
|
||||||
print("LOGGER METHOD RUNNING")
|
print(f"Logger: {result}")
|
||||||
print("CURRENT STATE FROM LOGGER: ", self.state)
|
|
||||||
|
|
||||||
@listen(and_(start_method, second_method))
|
|
||||||
async def and_logger(self):
|
|
||||||
print("AND LOGGER METHOD RUNNING")
|
|
||||||
print("CURRENT STATE FROM AND LOGGER: ", self.state)
|
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
flow = StructuredExampleFlow()
|
flow = OrExampleFlow()
|
||||||
await flow.kickoff()
|
await flow.kickoff()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user