-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathstream_tool_progress.py
More file actions
86 lines (64 loc) · 3 KB
/
stream_tool_progress.py
File metadata and controls
86 lines (64 loc) · 3 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""Example: tool progress via on_tool_progress hooks.
Demonstrates how tools can emit intermediate progress updates using
await ctx.send_progress(data), consumed via RunHooks.on_tool_progress.
"""
import asyncio
from agents import Agent, RunHooks, Runner, function_tool
from agents.tool import Tool
from agents.tool_context import ToolContext
@function_tool
async def analyze_data(ctx: ToolContext, query: str) -> str:
"""Simulate a long-running data analysis task with progress updates."""
await ctx.send_progress({"status": "starting", "query": query})
await asyncio.sleep(1)
await ctx.send_progress({"status": "fetching_data", "progress": 0.25})
await asyncio.sleep(1)
await ctx.send_progress({"status": "processing", "progress": 0.5})
await asyncio.sleep(1)
await ctx.send_progress({"status": "finalizing", "progress": 1.0})
await asyncio.sleep(0.5)
return f"Analysis complete for '{query}': found 42 results with 95% confidence."
@function_tool
async def quick_lookup(ctx: ToolContext, term: str) -> str:
"""A faster tool that also emits progress."""
await ctx.send_progress({"status": "searching", "term": term})
await asyncio.sleep(0.5)
return f"Found definition for '{term}': a common search term."
class ProgressHooks(RunHooks):
async def on_tool_progress(self, ctx, agent, tool: Tool, data):
print(f" [progress] {tool.name}: {data}")
async def main():
agent = Agent(
name="Analyst",
instructions=(
"You are a data analyst. Use the analyze_data tool for complex queries "
"and quick_lookup for simple lookups. Always use the tools when asked."
),
tools=[analyze_data, quick_lookup],
)
hooks = ProgressHooks()
print("Interactive tool progress example (hooks-based).")
print("Type a message to chat, or 'quit' to exit.\n")
while True:
user_input = input("You: ").strip()
if not user_input or user_input.lower() == "quit":
print("Goodbye!")
break
result = Runner.run_streamed(agent, input=user_input, hooks=hooks)
async for event in result.stream_events():
if event.type == "raw_response_event":
data = event.data
if getattr(data, "type", None) == "response.output_text.delta":
print(getattr(data, "delta", ""), end="", flush=True)
elif event.type == "agent_updated_stream_event":
print(f"Agent: {event.new_agent.name}")
elif event.type == "run_item_stream_event":
if event.item.type == "tool_call_item":
print(f"\n-- Tool called: {getattr(event.item.raw_item, 'name', '?')}")
elif event.item.type == "tool_call_output_item":
print(f"\n-- Tool output: {event.item.output}")
elif event.item.type == "message_output_item":
print() # newline after streamed tokens
print()
if __name__ == "__main__":
asyncio.run(main())