Skip to content

Commit 3b643ff

Browse files
committed
Update OpenAI example to show tools being called
1 parent 122220c commit 3b643ff

1 file changed

Lines changed: 49 additions & 5 deletions

File tree

examples/openai_agent/main_streamable.py

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,44 @@
66
from agents import Agent, Runner, trace
77
from agents.mcp import create_static_tool_filter
88
from agents.mcp.server import MCPServerStreamableHttp
9+
from agents.stream_events import RawResponsesStreamEvent, RunItemStreamEvent
10+
from openai.types.responses import ResponseCompletedEvent, ResponseOutputMessage
911

1012

11-
async def main():
13+
def print_tool_call(tool_name, params, color="yellow", show_params=True):
14+
# Define color codes for different colors
15+
# we could use a library like colorama but this avoids adding a dependency
16+
color_codes = {
17+
"grey": "\033[37m",
18+
"yellow": "\033[93m",
19+
}
20+
color_code_reset = "\033[0m"
21+
22+
color_code = color_codes.get(color, color_codes["yellow"])
23+
msg = f"Calling the tool {tool_name}"
24+
if show_params:
25+
msg += f" with params {params}"
26+
print(f"{color_code}# {msg}{color_code_reset}")
27+
28+
29+
def handle_event_printing(event, show_tools_calls=True):
30+
if type(event) == RunItemStreamEvent and show_tools_calls:
31+
if event.name == "tool_called":
32+
print_tool_call(
33+
event.item.raw_item.name,
34+
event.item.raw_item.arguments,
35+
color="grey",
36+
show_params=True,
37+
)
38+
39+
if type(event) == RawResponsesStreamEvent:
40+
if type(event.data) == ResponseCompletedEvent:
41+
for output in event.data.response.output:
42+
if type(output) == ResponseOutputMessage:
43+
print(output.content[0].text)
44+
45+
46+
async def main(inspect_events_tools_calls=False):
1247
prod_environment_id = os.environ.get("DBT_PROD_ENV_ID", os.getenv("DBT_ENV_ID"))
1348
token = os.environ.get("DBT_TOKEN")
1449
host = os.environ.get("DBT_HOST", "cloud.getdbt.com")
@@ -30,13 +65,15 @@ async def main():
3065
"get_dimensions",
3166
"get_entities",
3267
"query_metrics",
68+
"get_metrics_compiled_sql",
3369
],
3470
),
3571
) as server:
3672
agent = Agent(
3773
name="Assistant",
38-
instructions="Use the tools to answer the user's questions",
74+
instructions="Use the tools to answer the user's questions. Do not invent data or sample data.",
3975
mcp_servers=[server],
76+
model="gpt-5",
4077
)
4178
with trace(workflow_name="Conversation"):
4279
conversation = []
@@ -45,12 +82,19 @@ async def main():
4582
if result:
4683
conversation = result.to_input_list()
4784
conversation.append({"role": "user", "content": input("User > ")})
48-
result = await Runner.run(agent, conversation)
49-
print(result.final_output)
85+
86+
if inspect_events_tools_calls:
87+
async for event in Runner.run_streamed(
88+
agent, conversation
89+
).stream_events():
90+
handle_event_printing(event, show_tools_calls=True)
91+
else:
92+
result = await Runner.run(agent, conversation)
93+
print(result.final_output)
5094

5195

5296
if __name__ == "__main__":
5397
try:
54-
asyncio.run(main())
98+
asyncio.run(main(inspect_events_tools_calls=True))
5599
except KeyboardInterrupt:
56100
print("\nExiting.")

0 commit comments

Comments
 (0)