forked from microsoft/agent-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_with_shell.py
More file actions
67 lines (50 loc) · 2.36 KB
/
client_with_shell.py
File metadata and controls
67 lines (50 loc) · 2.36 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
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from agent_framework import Agent
from agent_framework.openai import OpenAIChatClient
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
"""
OpenAI Chat Client with Shell Tool Example
This sample demonstrates using get_shell_tool() with OpenAI Chat Client
for executing shell commands in a managed container environment hosted by OpenAI.
The shell tool allows the model to run commands like listing files, running scripts,
or performing system operations within a secure, sandboxed container.
Currently not all models support the shell tool. Refer to the OpenAI documentation
for the list of supported models: https://developers.openai.com/api/docs/models/
"""
async def main() -> None:
"""Example showing how to use the shell tool with OpenAI Chat."""
print("=== OpenAI Chat Client Agent with Shell Tool Example ===")
# Currently not all models support the shell tool. Refer to the OpenAI
# documentation for the list of supported models:
# https://developers.openai.com/api/docs/models/
client = OpenAIChatClient(model="gpt-5.4-nano")
# Create a hosted shell tool with the default auto container environment
shell_tool = client.get_shell_tool()
agent = Agent(
client=client,
instructions="You are a helpful assistant that can execute shell commands to answer questions.",
tools=shell_tool,
)
query = "Use a shell command to show the current date and time"
print(f"User: {query}")
result = await agent.run(query)
print(f"Result: {result}\n")
# Print shell-specific content details
for message in result.messages:
shell_calls = [c for c in message.contents if c.type == "shell_tool_call"]
shell_results = [c for c in message.contents if c.type == "shell_tool_result"]
if shell_calls:
print(f"Shell commands: {shell_calls[0].commands}")
if shell_results and shell_results[0].outputs:
for output in shell_results[0].outputs:
if output.stdout:
print(f"Stdout: {output.stdout}")
if output.stderr:
print(f"Stderr: {output.stderr}")
if output.exit_code is not None:
print(f"Exit code: {output.exit_code}")
if __name__ == "__main__":
asyncio.run(main())