forked from peass-ng/PEASS-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAICoder.py
208 lines (167 loc) · 6.73 KB
/
AICoder.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import argparse
import os
import sys
import string
import random
from typing import List
import openai
import json
import subprocess
import tiktoken
import requests
from github import Github
#########################
#### OPENAI FUNCTIONS ###
#########################
def reportTokens(prompt, model="gpt-4"):
encoding = tiktoken.encoding_for_model(model)
print("\033[37m" + str(len(encoding.encode(prompt))) + " tokens\033[0m" + " in prompt: " + "\033[92m" + prompt[:50] + "\033[0m" + ("..." if len(prompt) > 50 else ""))
def write_file(file_path: str, content: str):
"""Write content to a file creating the needed directories first"""
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, "w") as file:
file.write(content)
def delete_file(file_path: str):
"""Delete a file if it exists"""
if os.path.isfile(file_path):
os.remove(file_path)
openai_available_functions = {
"write_file": write_file, "delete_file": delete_file
}
openai_functions = [
{
"name": "write_file",
"description": "Write a file giving the path and the content",
"parameters": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "Path to the file to write",
},
"content": {
"type": "string",
"description": "Content to write in the file",
},
},
"required": ["file_path", "content"],
},
},
{
"name": "delete_file",
"description": "Delete a file",
"parameters": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "Path to the file to write",
}
},
"required": ["file_path"],
},
}
]
#########################
#### GIT FUNCTIONS ######
#########################
def create_pull_request(branch_name, commit_message, github_token):
github = Github(github_token)
repo = github.get_repo(os.environ["GITHUB_REPOSITORY"])
# Create a new branch
base_branch = repo.get_branch(repo.default_branch)
repo.create_git_ref(ref=f"refs/heads/{branch_name}", sha=base_branch.commit.sha)
# Commit changes to the new branch
subprocess.run(["git", "checkout", branch_name])
subprocess.run(["git", "add", "."])
subprocess.run(["git", "commit", "-m", commit_message])
subprocess.run(["git", "push", "origin", branch_name])
# Create a pull request
pr = repo.create_pull(
title=commit_message,
body="Generated by OpenAI Github Action",
head=branch_name,
base=repo.default_branch
)
return pr.html_url
#########################
#### FILE PROCESSING ####
#########################
def process_file(prompt: str, api_key: str, file_path: str, model: str="gpt-4") -> str:
with open(file_path, "r") as file:
file_content = file.read()
messages = [
{"role": "system", "content": f"You are a developer and your goal is to generate code. The user will ask you to improve and modify some code. Your response must be a valid JSON with the path of each file to write as keys and the content of the files as values. Several files can be written at the same time."},
{"role": "user", "content": prompt},
{"role": "user", "content": f"This is the code from the file '{file_path}':\n\n{file_content}"}
]
openai.api_key = api_key
reportTokens(f"This is the code from the file '{file_path}':\n\n{file_content}")
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0
)
response_message = response["choices"][0]["message"]
# Step 2: check if GPT wanted to call a function
if response_message.get("function_call"):
function_name = response_message["function_call"]["name"]
fuction_to_call = openai_available_functions[function_name]
function_args = json.loads(response_message["function_call"]["arguments"])
fuction_to_call(**function_args)
def process_folder(prompt: str, api_key: str, folder_path: str, model: str="gpt-4") -> List[str]:
responses = []
for root, _, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
response = process_file(prompt, api_key, file_path, model)
responses.append(response)
#########################
#### MAIN FUNCTION ######
#########################
def get_random_string(length):
# With combination of lower and upper case
letters = string.ascii_letters
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str
def main(prompt: str, api_key: str, file_path: str, github_token: str, model: str="gpt-4"):
if os.path.isfile(file_path):
process_file(prompt, api_key, file_path, model)
elif os.path.isdir(file_path):
process_folder(prompt, api_key, file_path, model)
else:
print("Error: Invalid file path.")
sys.exit(1)
try:
create_pull_request(get_random_string(5), f"Modified {file_path}", github_token)
except Exception as e:
print(f"Error: Failed to create pull request. {e}")
sys.exit(1)
if __name__ == "__main__":
# Setup the argument parser
parser = argparse.ArgumentParser()
# Add arguments for prompt, api_key, file_path and github_token
parser.add_argument('--prompt', default=None, type=str, help='Input prompt')
parser.add_argument('--api-key', default=None, type=str, help='Input API key')
parser.add_argument('--path', default=None, type=str, help='Input file/folder path')
parser.add_argument('--github-token', default=None, type=str, help='Github token')
parser.add_argument('--model', default="gpt-4", type=str, help='Model to use')
# Parse the arguments
args = parser.parse_args()
prompt = os.environ.get("INPUT_PROMPT", args.prompt)
api_key = os.environ.get("INPUT_API_KEY", args.api_key)
file_path = os.environ.get("INPUT_FILE_PATH", args.path)
github_token = os.environ.get("GITHUB_TOKEN", args.github_token)
model = os.environ.get("INPUT_MODEL", args.model)
if not prompt or not api_key or not file_path:
print("Error: Missing required inputs.")
sys.exit(1)
#if not github_token:
# print("Error: Missing github token.")
# sys.exit(1)
if os.path.exists(prompt):
with open(prompt, "r") as file:
prompt = file.read()
if prompt.startswith("http"):
prompt = requests.get(prompt).text
main(prompt, api_key, file_path, github_token, model)