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
|
from collections import defaultdict
import os
import pandas as pd
import openai
import tiktoken
from openai.embeddings_utils import get_embedding, cosine_similarity
from tree_sitter import Language, Parser
SOURCE_DIR = './'
openai.api_key = os.getenv('END_OF_WORLD')
def ts_query(lang, tree, sexp):
query = lang.query(sexp)
return query.captures(tree.root_node)
def ts_get_all_code_blocks(lang, code_blocks, file_path, tree, code):
"""Use treesitter to get all code blocks"""
# TODO need way to switch between declaration and definition ..
# e.g. golang does not have function definitions according to treesitter
results = ts_query(lang, tree, """(function_declaration) @function""")
results += ts_query(lang, tree, """(method_declaration) @method""")
# TODO something like list comprehension here?
for r in results:
return_dict = {
'code_type': r[1],
'source': code[r[0].start_byte:r[0].end_byte].decode('utf-8'),
'start_line': r[0].start_point[0],
'end_line': r[0].end_point[0],
'chars': r[0].end_byte - r[0].start_byte,
'file_path': file_path
}
code_blocks.append(return_dict)
def parse_file(file_path):
"""take source code file and return pd dataframe"""
# read file
with open(file_path, 'r') as f:
code = f.read()
# Tree-Sitter
parser = Parser()
lang = Language("./tree-go.so", "go")
parser.set_language(lang)
tree = parser.parse(bytes(code, "utf8"))
code_blocks = []
ts_get_all_code_blocks(lang, code_blocks, file_path, tree, bytes(code, "utf8"))
#TODO
# collate imports, assign
collate_types = ['import', 'assign']
tempblock = None
finblocks = []
for block in code_blocks:
if block['code_type'] in collate_types:
if tempblock is None:
tempblock = {k:v for k,v in block.items()}
elif tempblock['code_type'] == block['code_type']:
tempblock['source'] += f"\n{block['source']}"
tempblock['start_line'] = min(tempblock['start_line'], block['start_line'])
tempblock['end_line'] = max(tempblock['start_line'], block['end_line'])
tempblock['chars'] += (block['chars'] + 1)
else:
finblocks.append(tempblock)
tempblock = {k:v for k,v in block.items()}
else:
if tempblock is not None:
finblocks.append(tempblock)
tempblock = None
finblocks.append(block)
df = pd.DataFrame(finblocks)
return df
def get_files_to_parse(root_path, files_extensions_to_parse=['go'], dirs_to_ignore=['tests', 'vendor', 'unix']) -> list:
"""get all source file paths as list."""
files_to_parse = []
for root, dirs, files in os.walk(root_path):
# there is probably a better way to do this
# https://stackoverflow.com/questions/13454164/os-walk-without-hidden-folders
files = [f for f in files if not f[0] == '.']
dirs[:] = [d for d in dirs if (not d[0] == '.') and (set(d.split()).isdisjoint(dirs_to_ignore))]
for name in files:
#if (dirfix(root).rsplit("/", 1)[-1] in dirs_to_ignore) or (name in dirs_to_ignore) or (name.rsplit('.')[-1] not in files_extensions_to_parse):
if (name.rsplit('.')[-1] not in files_extensions_to_parse):
continue
temp_path = os.path.join(root, name)
files_to_parse.append(temp_path)
return files_to_parse
def generate_summary(prompt):
enc = tiktoken.encoding_for_model("text-davinci-003")
if (len(enc.encode(prompt)) > 2500):
return "too long to summarize."
prompt = prompt + '\nSummarize the above code: '
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.7,
max_tokens=1024,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=["\"\"\""]
)
return response["choices"][0]["text"]
# nate function to create blob. the blob just contains the file path and the source code.
def blobify(pandaSeries):
return f"file path: {pandaSeries['file_path']}\n {pandaSeries['source']}"
### doing stuff!!
code_df = pd.DataFrame()
for file in get_files_to_parse("../../dirserver/src/dirserver/"):
code_df = pd.concat([code_df, parse_file(file)])
code_df["blob"] = code_df.apply(lambda x: blobify(x),axis=1)
print(type(code_df))
print(code_df)
code_df.to_csv('test_with_blob.csv')
print('startng to generate summary')
code_df["summary"] = code_df.blob.apply(lambda x: generate_summary(x))
print('done with generate summary')
print('generating embeddings')
embedding_model = "text-embedding-ada-002"
code_df["embedding_summary"] = code_df.summary.apply([lambda x: get_embedding(x, engine=embedding_model)])
print('done with generating embeddings')
code_df.to_csv('test_with_summary_and_embeddings.csv')
|