-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgenerate.py
114 lines (94 loc) · 3.45 KB
/
generate.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
import re
import openai
import os
import json
import glob
import time
openai.api_key = os.getenv("OPENAI_API_KEY")
def get_bpf_summary(bpf_code):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content":
f"""
Given this BPF code:
{bpf_code}
Provide a concise and clear summary in one or two sentences.
Frame the explanation as a user's request to a developer to write a BPF code.
Avoid beginning with phrases like 'This BPF code is...' Instead,
start with action-oriented phrases such as 'Write a BPF code that...'
"""
}
],
max_tokens=1500
)
output = response.choices[0].message['content']
return output
def get_all_bpf_in_dir(directory):
all_files = glob.glob(directory + '/*.bt')
all_bpf = []
for file in all_files:
print("opening file: " + file)
# sleep for a short time to avoid hitting the rate limit
# Sleep for 5 seconds
time.sleep(3)
with open(file, 'r') as f:
bpf_code = f.read()
summary = get_bpf_summary(bpf_code)
all_bpf.append({
"request": summary,
"bpf": bpf_code
})
print(bpf_code)
print(summary)
return all_bpf
def write_example_to_json():
directory = './'
all_bpf = get_all_bpf_in_dir(directory)
with open('output.json', 'w') as outfile:
json.dump(all_bpf, outfile)
def remove_multiline_comments(lines):
"""
Remove multiline comments from a list of lines.
This function takes a list of lines as input and removes multiline comments
that start with '/*' and end with '*/'. The function returns the cleaned
content without the multiline comments.
"""
inside_comment = False
cleaned_lines = []
for line in lines:
if not inside_comment:
start_index = line.find('/*')
end_index = line.find('*/', start_index + 2)
if start_index != -1 and end_index != -1:
inside_comment = False
cleaned_line = line[:start_index] + line[end_index + 2:]
cleaned_lines.append(cleaned_line)
elif start_index != -1:
inside_comment = True
cleaned_line = line[:start_index]
cleaned_lines.append(cleaned_line)
else:
cleaned_lines.append(line)
else:
end_index = line.find('*/')
if end_index != -1:
inside_comment = False
cleaned_line = line[end_index + 2:]
cleaned_lines.append(cleaned_line)
cleaned_content = ''.join(cleaned_lines)
return cleaned_content
def reformat():
rearranged_data = {"data": []}
with open("./tools/output.json", mode='r', encoding='utf-8') as file:
contents = json.load(file)
for content in contents:
cleaned_content = remove_multiline_comments(re.split(r'(\n)', content['bpf']))
example = f"example: {content['request']}\n\n```\n{cleaned_content}\n```\n"
info = {"content": example}
rearranged_data["data"].append(info)
with open("./tools/examples.json", 'w', encoding='utf-8') as file:
json.dump(rearranged_data, file)
if __name__ == "__main__":
reformat()