-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathrun_stream_items_workflow.py
More file actions
71 lines (60 loc) · 2.32 KB
/
Copy pathrun_stream_items_workflow.py
File metadata and controls
71 lines (60 loc) · 2.32 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from __future__ import annotations
import asyncio
import uuid
from agents import ItemHelpers
from agents.items import TResponseStreamEvent
from temporalio.api.common.v1 import Payload
from temporalio.client import Client
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin
from temporalio.contrib.workflow_streams import WorkflowStreamClient
from openai_agents.streaming.shared import (
TASK_QUEUE,
TOPIC_DONE,
TOPIC_EVENTS,
race_with_workflow,
)
from openai_agents.streaming.workflows.stream_items_workflow import (
StreamItemsInput,
StreamItemsWorkflow,
)
async def main() -> None:
client = await Client.connect(
"localhost:7233",
plugins=[OpenAIAgentsPlugin()],
)
workflow_id = f"stream-items-{uuid.uuid4().hex[:8]}"
handle = await client.start_workflow(
StreamItemsWorkflow.run,
StreamItemsInput(),
id=workflow_id,
task_queue=TASK_QUEUE,
)
stream = WorkflowStreamClient.create(client, workflow_id)
converter = client.data_converter.payload_converter
async def render() -> None:
print("=== Run starting ===")
async for item in stream.subscribe([TOPIC_EVENTS, TOPIC_DONE]):
if item.topic == TOPIC_DONE:
return
assert isinstance(item.data, Payload)
event = converter.from_payload(item.data, TResponseStreamEvent)
if event.type == "raw_response_event":
continue
if event.type == "agent_updated_stream_event":
print(f"Agent updated: {event.new_agent.name}")
elif event.type == "run_item_stream_event":
if event.item.type == "tool_call_item":
name = getattr(event.item.raw_item, "name", "Unknown Tool")
print(f"-- Tool was called: {name}")
elif event.item.type == "tool_call_output_item":
print(f"-- Tool output: {event.item.output}")
elif event.item.type == "message_output_item":
print(
"-- Message output:\n "
f"{ItemHelpers.text_message_output(event.item)}"
)
result = await race_with_workflow(render(), handle)
print("=== Run complete ===")
print(result)
if __name__ == "__main__":
asyncio.run(main())