-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
109 lines (88 loc) · 3.39 KB
/
main.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import subprocess
import json
import re
import openai
def extract_man_description(command):
try:
# Run man command and capture the output
man_output = subprocess.run(['man', command], capture_output=True, text=True, check=True).stdout
# Extract the DESCRIPTION section using regex
description_match = re.search(r'DESCRIPTION\n(-+\n)?(.*?)\n\n', man_output, re.DOTALL)
if description_match:
description = description_match.group(2).strip()
return description
else:
return f"No DESCRIPTION found for command '{command}'"
except subprocess.CalledProcessError:
return f"No man page found for command '{command}'"
def parse_man_options(command):
try:
# Run man command and capture the output
man_output = subprocess.run(['man', command], capture_output=True, text=True, check=True).stdout
# Extract options using regex
options = re.findall(r'\n\s*(-\w|--\w[\w-]*)\s+(.*?)(\n\s*-|\n\n)', man_output, re.DOTALL)
# Format options into a dictionary
options_dict = {}
for option in options:
flag, description, _ = option
options_dict[flag.strip()] = description.strip()
return options_dict
except subprocess.CalledProcessError:
return {}
def build_tool_definition_from_man(command):
description = extract_man_description(command)
options = parse_man_options(command)
# Generate a tool definition based on the description and options
tool_definition = {
"type": "function",
"name": f"{command}_command",
"description": description,
"parameters": {
"type": "object",
"properties": {},
"required": []
},
}
for option, desc in options.items():
tool_definition["parameters"]["properties"][option] = {
"type": "boolean" if desc.lower() in ["enable", "disable"] else "string",
"description": desc
}
return tool_definition
def execute_command(command, args):
cmd = [command]
for key, value in args.items():
if value is True or value == 'true':
cmd.append(key)
else:
cmd.append(f"{key}={value}")
print(cmd)
result = subprocess.run(cmd, capture_output=True, text=True)
return result.stdout
def main():
tool_definition = build_tool_definition_from_man("ls")
client = openai.Client(api_key=os.getenv('OPENAI_API_KEY'))
# Request ChatGPT to use the 'ls' command and get the current directory
response = client.chat.completions.create(
model='gpt-4o',
messages=[
{"role": "user", "content": "list files sorted by name with human-readable file sizes"}
],
functions=[tool_definition]
)
response_message = response.choices[0].message
content = response_message.content or ""
tool_calls = [response_message.function_call]
if tool_calls:
for tool_call in tool_calls:
function_name = tool_call.name
arguments = json.loads(tool_call.arguments)
command = function_name.replace("_command", "")
if function_name == f"{command}_command":
output = execute_command(command, arguments)
print(f"Output of '{command}':\n{output}")
else:
print(content)
if __name__ == "__main__":
main()