-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructured_flow.py
More file actions
52 lines (39 loc) · 1.65 KB
/
structured_flow.py
File metadata and controls
52 lines (39 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from crewai.flow.flow import Flow, start, listen
from pydantic import BaseModel
'''
This example demonstrates a structured flow using CrewAI's Flow framework.
The flow consists of two methods:
1. `method1`: This is the starting point of the flow. It initializes the flow state and performs some updates.
2. `method2`: This method listens to the output of `method1` and performs further updates to the flow state.
The flow state is defined using a Pydantic model, `ExampleState`, which contains two fields: `message` and `counter`.
The flow state is updated in both methods, and the updates are printed to the console.
'''
# Define the flow state using Pydantic model
class ExampleState(BaseModel):
message:str = 'Initial Message'
counter:int = 0
# Define the structured flow
class StructuredFlow(Flow[ExampleState]):
# Define the flow state
# state = ExampleState()
# start method to initialize the flow
@start()
def method1(self):
print("Starting flow...")
print(f'LOGGER :: Before updating State: {self.state}')
# Update the flow state
self.state.message += ' -- Update (1)'
self.state.counter = self.state.counter+1
print(f"LOGGER :: After updating State inside method1: {self.state}")
# listen to the output of method1
@listen(method1)
def method2(self):
print("Inside method2:")
# Update the flow state
self.state.message += ' -- Update (2)'
self.state.counter = self.state.counter+1
print(f"LOGGER :: After updating State inside method2: {self.state}")
# Create an instance of the structured flow and kickoff the flow
# (start the execution)
flow = StructuredFlow()
flow.kickoff()