Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use workflow init from samples #143

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions bedrock/entity/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@ class BedrockParams:

@workflow.defn
class EntityBedrockWorkflow:
def __init__(self) -> None:

@workflow.init
def __init__(self, params: BedrockParams) -> None:
# List to store prompt history
self.conversation_history: List[Tuple[str, str]] = []
self.prompt_queue: Deque[str] = deque()
self.conversation_summary: Optional[str] = None
self.continue_as_new_per_turns: int = 6
self.chat_ended: bool = False

@workflow.run
async def run(
self,
params: BedrockParams,
) -> str:

if params and params.conversation_summary:
self.conversation_history.append(
("conversation_summary", params.conversation_summary)
Expand All @@ -42,6 +37,12 @@ async def run(
if params and params.prompt_queue:
self.prompt_queue.extend(params.prompt_queue)


@workflow.run
async def run(
self,
params: BedrockParams,
) -> str:
while True:
workflow.logger.info("Waiting for prompts...")

Expand Down
2 changes: 1 addition & 1 deletion message_passing/introduction/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GreetingWorkflow:

# 👉 This Workflow does not use any async handlers and so cannot use any
# Activities. It only supports two languages, whose greetings are hardcoded
# in the Workflow definition. See GreetingWorkflowWithAsyncHandler below for
# in the Workflow definition. See ClusterManagerWorkflow for
# a Workflow that uses an async Update handler to call an Activity.

def __init__(self) -> None:
Expand Down
22 changes: 10 additions & 12 deletions message_passing/safe_message_handlers/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,20 @@ class ClusterManagerAssignNodesToJobResult:
# These updates must run atomically.
@workflow.defn
class ClusterManagerWorkflow:
def __init__(self) -> None:
self.state = ClusterManagerState()
@workflow.init
def __init__(self, input: ClusterManagerInput) -> None:
# Protects workflow state from interleaved access
self.nodes_lock = asyncio.Lock()

# If we're continuing as new, we want to start from where we left off.
self.state = input.state or ClusterManagerState()

# For testing, we want to be able to more frequently continue as new.
self.max_history_length: Optional[int] = None
self.sleep_interval_seconds: int = 600
if input.test_continue_as_new:
self.max_history_length = 120
self.sleep_interval_seconds = 1

@workflow.signal
async def start_cluster(self) -> None:
Expand Down Expand Up @@ -202,15 +210,6 @@ async def perform_health_checks(self) -> None:
f"Health check failed with error {type(e).__name__}:{e}"
)

# The cluster manager is a long-running "entity" workflow so we need to periodically checkpoint its state and
# continue-as-new.
def init(self, input: ClusterManagerInput) -> None:
if input.state:
self.state = input.state
if input.test_continue_as_new:
self.max_history_length = 120
self.sleep_interval_seconds = 1

def should_continue_as_new(self) -> bool:
if workflow.info().is_continue_as_new_suggested():
return True
Expand All @@ -224,7 +223,6 @@ def should_continue_as_new(self) -> bool:

@workflow.run
async def run(self, input: ClusterManagerInput) -> ClusterManagerResult:
self.init(input)
await workflow.wait_condition(lambda: self.state.cluster_started)
# Perform health checks at intervals.
while True:
Expand Down
Loading
Loading