-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
140 lines (115 loc) · 5.1 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
import subprocess
import requests
from functools import reduce
import time
class DirectoryProcessor:
def __init__(self):
self.rootdir = os.getcwd()
self.api_key = os.getenv('OPENAI_API_KEY')
self.model_name = 'gpt-3.5-turbo'
self.session = requests.Session()
self.session.headers.update(
{
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"Authorization": f"Bearer {self.api_key}",
}
)
self.post_data_template = {
"model": 'gpt-3.5-turbo',
"messages": [
{
"role": "system",
"content": """
""",
},
{"role": "user", "content": ""},
],
"temperature": 0.01,
"max_tokens": 4000
}
def get_directory_structure(self):
"""
Creates a nested dictionary that represents the folder structure of rootdir
"""
dir = {}
start = self.rootdir.rfind(os.sep) + 1
for path, dirs, files in os.walk(self.rootdir):
folders = path[start:].split(os.sep)
subdir = dict.fromkeys(files)
parent = reduce(dict.get, folders[:-1], dir)
parent[folders[-1]] = subdir
return dir
def execute_commands(self, commands):
"""
Executes shell commands
"""
for command in commands:
subprocess.run(command, shell=True)
def process_directory(self):
# Get the directory structure
dir_structure = self.get_directory_structure()
# Send the directory structure to the GPT API
gpt_response = self.send_to_gpt_api(dir_structure)
# Get the commands from the GPT API response
commands = gpt_response.get('choices', [{}])[0].get('text', '').split('\n')
# Execute the commands
self.execute_commands(commands)
def print_directory_structure(self):
startpath = os.getcwd()
structure = ''
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
print('{}{}/'.format(indent, os.path.basename(root)))
subindent = ' ' * 4 * (level + 1)
for f in files:
structure += '{}{}'.format(subindent, f)
return structure
def get_commands(self, retries=5, delay=5):
directory_info = self.print_directory_structure()
post_data = self.post_data_template.copy()
# entity = meta_info.get('og:site_name', '') # Use the 'og:site_name' meta tag as the entity
post_data["messages"][1]["content"] = f"""
I need to organize my files and folders in my cwd, I'm going to give you the files/folder structure of my cwd, and you are to
analyze this, determine the optimal organization of the files and folders, and then provide me with the commands to execute to
organize the files and folders.
** Your output must simply be a one-line bash command or hyphenated list of bash commands sequence that organizes the files and folders as you've recommended **
Directory files and folders: {directory_info}
Example Output:
"mv file.txt /path/to/new/directory"
"""
for _ in range(retries):
try:
response = self.session.post("https://api.openai.com/v1/chat/completions", json=post_data, timeout=60)
response.raise_for_status()
response = response.json()
print(response['choices'][0]['message']['content'])
commands = response['choices'][0]['message']['content'].split(';')
return commands
# command = response["choices"][0]["message"]["content"].split('Bash command: ')[1]
# return command
except requests.HTTPError as e:
print(f"Request failed with {e}, retrying after {delay} seconds...")
time.sleep(delay)
except requests.Timeout:
print("Request to OpenAI API timed out after 60 seconds, retrying...")
time.sleep(delay)
except requests.exceptions.RequestException as e:
print(f"Request failed with {e}, retrying after {delay} seconds...")
time.sleep(delay)
def execute_bash_command(self, command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, error = process.communicate()
if process.returncode != 0:
print(f"Error executing '{command}': {error.decode('utf-8')}")
else:
print(f"Output of '{command}': {output.decode('utf-8')}")
if __name__ == "__main__":
processor = DirectoryProcessor()
commands = processor.get_commands()
for command in commands:
command = command.strip()
if command:
processor.execute_bash_command(command)