-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathrun_workflow.py
More file actions
51 lines (41 loc) · 1.59 KB
/
Copy pathrun_workflow.py
File metadata and controls
51 lines (41 loc) · 1.59 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
"""Start the streaming workflow and consume model events live."""
# @@@SNIPSTART python-strands-streaming-client
import asyncio
import os
from datetime import timedelta
from strands.types.streaming import StreamEvent
from temporalio.client import Client
from temporalio.contrib.workflow_streams import WorkflowStreamClient
from strands_plugin.streaming.workflow import StreamingWorkflow
async def main() -> None:
client = await Client.connect(os.environ.get("TEMPORAL_ADDRESS", "localhost:7233"))
workflow_id = "strands-streaming"
handle = await client.start_workflow(
StreamingWorkflow.run,
"Count from 1 to 5, one number per sentence.",
id=workflow_id,
task_queue="strands-streaming",
)
async def consume() -> None:
stream = WorkflowStreamClient.create(client, workflow_id)
async for item in stream.subscribe(
["events"],
from_offset=0,
result_type=StreamEvent,
poll_cooldown=timedelta(milliseconds=50),
):
event: StreamEvent = item.data
if "contentBlockDelta" in event:
delta = event["contentBlockDelta"].get("delta", {})
if "text" in delta:
print(delta["text"], end="", flush=True)
elif "messageStop" in event:
print()
return
consume_task = asyncio.create_task(consume())
result = await handle.result()
await asyncio.wait_for(consume_task, timeout=10.0)
print(f"Final result: {result}")
if __name__ == "__main__":
asyncio.run(main())
# @@@SNIPEND