Repository navigation
Expand file tree
/
Copy pathrun_workflow.py
More file actions
54 lines (43 loc) · 1.69 KB
/
Copy pathrun_workflow.py
File metadata and controls
54 lines (43 loc) · 1.69 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
53
54
"""Start the streaming workflow and print the model's updates as they arrive."""
# @@@SNIPSTART python-microsoft-agent-framework-streaming-client
import asyncio
import os
from datetime import timedelta
from agent_framework import ChatResponseUpdate
from temporalio.client import Client
from temporalio.contrib.workflow_streams import WorkflowStreamClient
from microsoft_agent_framework_plugin.streaming.workflow import (
STREAMING_TOPIC,
StreamingWorkflow,
)
async def main() -> None:
client = await Client.connect(os.environ.get("TEMPORAL_ADDRESS", "localhost:7233"))
workflow_id = "microsoft-agent-framework-streaming"
handle = await client.start_workflow(
StreamingWorkflow.run,
"Count from 1 to 5, one number per sentence.",
id=workflow_id,
task_queue="microsoft-agent-framework-streaming",
)
async def consume() -> None:
# Each item is a ChatResponseUpdate in its dict form.
stream = WorkflowStreamClient.create(client, workflow_id)
async for item in stream.subscribe(
[STREAMING_TOPIC],
from_offset=0,
result_type=dict,
poll_cooldown=timedelta(milliseconds=50),
):
update = ChatResponseUpdate.from_dict(item.data)
if update.text:
print(update.text, end="", flush=True)
consume_task = asyncio.create_task(consume())
result = await handle.result()
# Once the Workflow is complete, subscribe reads the rest of the stream
# and then ends, so this returns with every update printed.
await consume_task
print()
print(f"Final result: {result}")
if __name__ == "__main__":
asyncio.run(main())
# @@@SNIPEND