2023-02-25 13:37:33 +00:00
|
|
|
import ast
|
2023-05-13 08:36:17 +00:00
|
|
|
import os
|
2023-02-22 17:19:13 +00:00
|
|
|
from pathlib import Path
|
2023-05-13 08:36:17 +00:00
|
|
|
|
|
|
|
import tiktoken
|
2023-02-22 17:19:13 +00:00
|
|
|
from langchain.llms import OpenAI
|
|
|
|
from langchain.prompts import PromptTemplate
|
|
|
|
|
2023-05-12 10:02:25 +00:00
|
|
|
|
2023-02-25 13:37:33 +00:00
|
|
|
def find_files(directory):
|
|
|
|
files_list = []
|
|
|
|
for root, dirs, files in os.walk(directory):
|
|
|
|
for file in files:
|
|
|
|
if file.endswith('.py'):
|
|
|
|
files_list.append(os.path.join(root, file))
|
|
|
|
return files_list
|
|
|
|
|
2023-05-12 10:02:25 +00:00
|
|
|
|
2023-02-25 13:37:33 +00:00
|
|
|
def extract_functions(file_path):
|
|
|
|
with open(file_path, 'r') as file:
|
|
|
|
source_code = file.read()
|
|
|
|
functions = {}
|
|
|
|
tree = ast.parse(source_code)
|
|
|
|
for node in ast.walk(tree):
|
|
|
|
if isinstance(node, ast.FunctionDef):
|
|
|
|
func_name = node.name
|
|
|
|
func_def = ast.get_source_segment(source_code, node)
|
|
|
|
functions[func_name] = func_def
|
2023-02-22 17:19:13 +00:00
|
|
|
return functions
|
|
|
|
|
2023-05-12 10:02:25 +00:00
|
|
|
|
2023-02-25 13:37:33 +00:00
|
|
|
def extract_classes(file_path):
|
|
|
|
with open(file_path, 'r') as file:
|
|
|
|
source_code = file.read()
|
|
|
|
classes = {}
|
|
|
|
tree = ast.parse(source_code)
|
|
|
|
for node in ast.walk(tree):
|
|
|
|
if isinstance(node, ast.ClassDef):
|
|
|
|
class_name = node.name
|
|
|
|
function_names = []
|
|
|
|
for subnode in ast.walk(node):
|
|
|
|
if isinstance(subnode, ast.FunctionDef):
|
|
|
|
function_names.append(subnode.name)
|
|
|
|
classes[class_name] = ", ".join(function_names)
|
2023-02-22 17:19:13 +00:00
|
|
|
return classes
|
|
|
|
|
2023-05-12 10:02:25 +00:00
|
|
|
|
2023-02-25 13:37:33 +00:00
|
|
|
def extract_functions_and_classes(directory):
|
|
|
|
files = find_files(directory)
|
|
|
|
functions_dict = {}
|
|
|
|
classes_dict = {}
|
|
|
|
for file in files:
|
|
|
|
functions = extract_functions(file)
|
|
|
|
if functions:
|
|
|
|
functions_dict[file] = functions
|
|
|
|
classes = extract_classes(file)
|
|
|
|
if classes:
|
|
|
|
classes_dict[file] = classes
|
|
|
|
return functions_dict, classes_dict
|
|
|
|
|
2023-05-12 10:02:25 +00:00
|
|
|
|
2023-02-25 13:37:33 +00:00
|
|
|
def parse_functions(functions_dict, formats, dir):
|
2023-02-22 17:19:13 +00:00
|
|
|
c1 = len(functions_dict)
|
2023-02-25 13:37:33 +00:00
|
|
|
for i, (source, functions) in enumerate(functions_dict.items(), start=1):
|
|
|
|
print(f"Processing file {i}/{c1}")
|
2023-05-12 10:02:25 +00:00
|
|
|
source_w = source.replace(dir + "/", "").replace("." + formats, ".md")
|
2023-02-25 13:37:33 +00:00
|
|
|
subfolders = "/".join(source_w.split("/")[:-1])
|
|
|
|
Path(f"outputs/{subfolders}").mkdir(parents=True, exist_ok=True)
|
|
|
|
for j, (name, function) in enumerate(functions.items(), start=1):
|
|
|
|
print(f"Processing function {j}/{len(functions)}")
|
2023-02-22 17:19:13 +00:00
|
|
|
prompt = PromptTemplate(
|
|
|
|
input_variables=["code"],
|
|
|
|
template="Code: \n{code}, \nDocumentation: ",
|
|
|
|
)
|
|
|
|
llm = OpenAI(temperature=0)
|
|
|
|
response = llm(prompt.format(code=function))
|
2023-02-25 13:37:33 +00:00
|
|
|
mode = "a" if Path(f"outputs/{source_w}").exists() else "w"
|
|
|
|
with open(f"outputs/{source_w}", mode) as f:
|
2023-05-12 10:02:25 +00:00
|
|
|
f.write(
|
|
|
|
f"\n\n# Function name: {name} \n\nFunction: \n```\n{function}\n```, \nDocumentation: \n{response}")
|
2023-02-22 17:19:13 +00:00
|
|
|
|
|
|
|
|
2023-02-25 13:37:33 +00:00
|
|
|
def parse_classes(classes_dict, formats, dir):
|
2023-02-22 17:19:13 +00:00
|
|
|
c1 = len(classes_dict)
|
2023-02-25 13:37:33 +00:00
|
|
|
for i, (source, classes) in enumerate(classes_dict.items()):
|
2023-05-12 10:02:25 +00:00
|
|
|
print(f"Processing file {i + 1}/{c1}")
|
|
|
|
source_w = source.replace(dir + "/", "").replace("." + formats, ".md")
|
2023-02-25 13:37:33 +00:00
|
|
|
subfolders = "/".join(source_w.split("/")[:-1])
|
|
|
|
Path(f"outputs/{subfolders}").mkdir(parents=True, exist_ok=True)
|
2023-02-22 17:19:13 +00:00
|
|
|
for name, function_names in classes.items():
|
2023-05-12 10:02:25 +00:00
|
|
|
print(f"Processing Class {i + 1}/{c1}")
|
2023-02-22 17:19:13 +00:00
|
|
|
prompt = PromptTemplate(
|
|
|
|
input_variables=["class_name", "functions_names"],
|
|
|
|
template="Class name: {class_name} \nFunctions: {functions_names}, \nDocumentation: ",
|
|
|
|
)
|
|
|
|
llm = OpenAI(temperature=0)
|
|
|
|
response = llm(prompt.format(class_name=name, functions_names=function_names))
|
|
|
|
|
2023-02-25 13:37:33 +00:00
|
|
|
with open(f"outputs/{source_w}", "a" if Path(f"outputs/{source_w}").exists() else "w") as f:
|
|
|
|
f.write(f"\n\n# Class name: {name} \n\nFunctions: \n{function_names}, \nDocumentation: \n{response}")
|
2023-02-22 17:19:13 +00:00
|
|
|
|
2023-05-12 10:02:25 +00:00
|
|
|
|
2023-02-25 13:37:33 +00:00
|
|
|
def transform_to_docs(functions_dict, classes_dict, formats, dir):
|
|
|
|
docs_content = ''.join([str(key) + str(value) for key, value in functions_dict.items()])
|
|
|
|
docs_content += ''.join([str(key) + str(value) for key, value in classes_dict.items()])
|
2023-02-22 17:19:13 +00:00
|
|
|
|
2023-02-25 13:37:33 +00:00
|
|
|
num_tokens = len(tiktoken.get_encoding("cl100k_base").encode(docs_content))
|
2023-02-22 17:19:13 +00:00
|
|
|
total_price = ((num_tokens / 1000) * 0.02)
|
|
|
|
|
2023-02-25 13:37:33 +00:00
|
|
|
print(f"Number of Tokens = {num_tokens:,d}")
|
|
|
|
print(f"Approx Cost = ${total_price:,.2f}")
|
|
|
|
|
|
|
|
user_input = input("Price Okay? (Y/N)\n").lower()
|
|
|
|
if user_input == "y" or user_input == "":
|
2023-02-22 17:19:13 +00:00
|
|
|
if not Path("outputs").exists():
|
|
|
|
Path("outputs").mkdir()
|
2023-02-25 13:37:33 +00:00
|
|
|
parse_functions(functions_dict, formats, dir)
|
|
|
|
parse_classes(classes_dict, formats, dir)
|
2023-02-22 17:19:13 +00:00
|
|
|
print("All done!")
|
|
|
|
else:
|
2023-05-12 10:02:25 +00:00
|
|
|
print("The API was not called. No money was spent.")
|