forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagents_as_tools_streaming.py
More file actions
59 lines (46 loc) · 2.14 KB
/
agents_as_tools_streaming.py
File metadata and controls
59 lines (46 loc) · 2.14 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
import asyncio
from agents import Agent, AgentToolStreamEvent, ModelSettings, Runner, function_tool, trace
@function_tool(
name_override="billing_status_checker",
description_override="Answer questions about customer billing status.",
)
def billing_status_checker(customer_id: str | None = None, question: str = "") -> str:
"""Return a canned billing answer or a fallback when the question is unrelated."""
normalized = question.lower()
if "bill" in normalized or "billing" in normalized:
return f"This customer (ID: {customer_id})'s bill is $100"
return "I can only answer questions about billing."
def handle_stream(event: AgentToolStreamEvent) -> None:
"""Print streaming events emitted by the nested billing agent."""
stream = event["event"]
tool_call = event.get("tool_call")
tool_call_info = tool_call.call_id if tool_call is not None else "unknown"
print(f"[stream] agent={event['agent'].name} call={tool_call_info} type={stream.type} {stream}")
async def main() -> None:
with trace("Agents as tools streaming example"):
billing_agent = Agent(
name="Billing Agent",
instructions="You are a billing agent that answers billing questions.",
model_settings=ModelSettings(tool_choice="required"),
tools=[billing_status_checker],
)
billing_agent_tool = billing_agent.as_tool(
tool_name="billing_agent",
tool_description="You are a billing agent that answers billing questions.",
on_stream=handle_stream,
)
main_agent = Agent(
name="Customer Support Agent",
instructions=(
"You are a customer support agent. Always call the billing agent to answer billing "
"questions and return the billing agent response to the user."
),
tools=[billing_agent_tool],
)
result = await Runner.run(
main_agent,
"Hello, my customer ID is ABC123. How much is my bill for this month?",
)
print(f"\nFinal response:\n{result.final_output}")
if __name__ == "__main__":
asyncio.run(main())