docugen/docugen-chatgpt.py

58 lines
1.9 KiB
Python
Raw Permalink Normal View History

2022-12-07 01:17:40 -08:00
#!/usr/bin/env python3
import json
2022-12-05 20:25:34 -08:00
import yaml
from yaml import CLoader, CDumper
from revChatGPT.revChatGPT import Chatbot
import os
from sys import argv
2022-12-07 01:17:40 -08:00
import fmtutil
from parse import py
2022-12-07 01:17:40 -08:00
from argparse import ArgumentParser
2022-12-05 20:25:34 -08:00
2022-12-07 01:17:40 -08:00
argparser = ArgumentParser(
# prog="docugen",
description="generate docs with le AI(tm)",
epilog="https://github.com/turtlebasket/docugen"
)
2022-12-05 20:25:34 -08:00
2022-12-07 01:17:40 -08:00
argparser.add_argument("filename")
argparser.add_argument("-o", dest="output directory", help="directory to write docs to")
argparser.add_argument("-m", dest="model", help="model to use to generate documentation", choices=["chatgpt"])
argparser.add_argument("-f", dest="format", help="formatting of output documentation", choices=["md"])
args = argparser.parse_args()
2022-12-05 20:25:34 -08:00
file_path = os.path.dirname(os.path.realpath(__file__))
with open(f"{file_path}/config.yaml", "r") as file:
config = yaml.load(file, Loader=CLoader)
2022-12-07 01:17:40 -08:00
infile = open(args.filename, "r")
2022-12-05 20:25:34 -08:00
2022-12-07 01:17:40 -08:00
# python & top level functions only for now
functions = py.find_toplevel_funcs(infile)
2022-12-05 20:25:34 -08:00
print(f"Found {len(functions)} functions.")
2022-12-07 01:17:40 -08:00
if len(functions) >= 3:
print(f"Grab a cup of coffee, this could take a while.")
2022-12-05 20:25:34 -08:00
doc_prompt_head = "Explain what this function does in one short sentence, then give example code that uses the function:\n"
# doc_prompt_example = "Return only example code using this function:\n"
bot = Chatbot({
'Authorization': config['Authorization'],
'session_token': config['session_token'],
}, conversation_id=None)
bot.refresh_session()
2022-12-07 01:17:40 -08:00
with open(f"{args.filename.split('.')[0]}-doc.md", "w") as outfile:
2022-12-05 20:25:34 -08:00
outfile.write(f"# Documentation for `{argv[1]}`\n\n")
for function in functions:
head_ask = doc_prompt_head + function["content"]
resp = bot.get_chat_response(head_ask, output="text")
print(f'Generated documentation for {function["head"]}.')
# append results to doc
2022-12-07 01:17:40 -08:00
output = f"### `{function['head']}`\n" + fmtutil.highlight_multiline_code_md(resp['message'], "python") + "\n\n"
2022-12-05 20:25:34 -08:00
outfile.write(output)