GPT4Readability — Never Write a README Again
Last Updated on July 15, 2023 by Editorial Team
Author(s): Denny Loevlie
Originally published on Towards AI.
One Python package to optimize and document your code repository
Navigating a complex Python codebase can be a challenging task, especially when the project lacks sufficient documentation. This is far too often the case in the life of a programmer. Fortunately, thanks to the recent breakthroughs in NLP and Deep Learning, there’s a tool designed to help: GPT4Readability.
GitHub – loevlie/GPT4Readability: A powerful tool designed to automatically generate a README.md…
A powerful tool designed to automatically generate a README.md file and suggest code improvements using LLMs. – GitHub…
github.com
GPT4Readability: Your AI-Powered Coding Assistant
GPT4Readability is a powerful package that leverages large language models (LLMs) and a vector database to generate a README.md file and suggest code improvements for your Python code repositories. It’s like having a personal assistant who not only understands and documents your code but also helps you make it better.
But Why Can’t I just Use ChatGPT?
ChatGPT is a powerful tool. However, it has a limitation when it comes to handling large volumes of text or code. You can only input a certain amount of text or code into ChatGPT at a time, which can be restrictive when dealing with extensive codebases.
In contrast, GPT4Readability is designed to handle larger volumes of code. While it may still struggle with extremely large codebases, it can comfortably handle most normal-sized ones. This means by simply pointing it to the correct folder, it can parse your entire project, analyze the full context, and generate a comprehensive README or provide detailed code improvement suggestions. This is something that ChatGPT cannot do!
Getting Started with GPT4Readability
I’ll start by walking you through the setup. First, you’ll need to install the package:
pip install GPT4Readability==0.0.3
Next, you’ll need to create an account with OpenAI. This is because GPT4Readability uses the OpenAI API to access GPT-3.5 and GPT-4. Eventually, the hope is to also integrate open-source models from HuggingFace (stay tuned to the GitHub repository for updates). Once you’ve created an account on the OpenAI platform, proceed to add a payment method and generating an API key.
Now, you’ll need to set an environment variable named OPENAI_API_KEY
equal to your newly minted API key. This allows GPT4Readability to authenticate with the OpenAI API. If you don’t do this, you will be prompted to add the key when running the GPT4Readability.
export OPEN_API_KEY="YOUR KEY"
Using OpenAI’s GPT-3 and GPT-4 LLMs through the API isn’t expensive. The entire development of this tool costs just $1.50, which seems quite cheap compared to the $20/month ChatGPT plus users pay! However, you can always keep an eye on your usage of the OpenAI website to ensure you’re not overspending.
Introducing Your Personal AI Documentation Assistant
Now that you’ve got GPT4Readability installed and have an OpenAI API key generated, you have access to the CLI. This article is focused on the CLI but you can also try the no-code web application on HuggingFace spaces at: https://huggingface.co/spaces/JohanDL/GPT4Readability.
Let’s say you have a Python project in need of a README, but you don’t even remember what the code was supposed to do. Instead of spending hours trying to figure out the connections in the codebase, let GPT4Readability do the heavy lifting:
gpt4readability /path/to/your/project --function readme --output_readme MyAwesomeREADME.md --model gpt-3.5-turbo
Just replace /path/to/your/project
with the root directory of your Python project. GPT4Readability will generate a comprehensive README.md file named MyAwesomeREADME.md
using the "gpt-3.5-turbo" model from OpenAI. Currently, there are two available models you can use with GPT4Readability:
- gpt-3.5-turbo
- gpt-4
Code Improvements: A Learning Tool for All
GPT4Readability isn’t just for updating codebases that lack the documentation they deserve. It’s also an incredible learning tool for developers at all levels. To generate suggestions on the functions in your codebase, use the suggestions tool as shown below:
gpt4readability /path/to/your/project --function suggestions --output_suggestions CodeImprovements.md --model gpt-3.5-turbo
Example Suggestion from Running GPT4Readability on my First Python Project:
Original function:
def Get_props(file):
""" This function retreives the information from the property cell of the notebook"""
no_prop = True
nb = nbformat.read(file,as_version=4)
for i in nb['cells']:
if i['cell_type'] == 'code':
if i['source'].startswith('%%properties'):
Metal_A = i['source'].split('\n')[1].split()[-1]
Metal_B = i['source'].split('\n')[2].split()[-1]
Max_H = float(i['source'].split('\n')[3].split()[-1])
result = {'Metal_A':Metal_A,'Metal_B':Metal_B,'Max_H':Max_H}
no_prop = False
if no_prop:
result = None
return result
Suggestion:
def Get_props(file):
""" This function retrieves the information from the property cell of the notebook"""
nb = nbformat.read(file, as_version=4)
for i in nb['cells']:
if i['cell_type'] == 'code' and i['source'].startswith('%%properties'):
Metal_A, Metal_B, Max_H = i['source'].split('\n')[1:4]
result = {'Metal_A': Metal_A.split()[-1], 'Metal_B': Metal_B.split()[-1], 'Max_H': float(Max_H.split()[-1])}
return result
return None
Explanation:
- Removed the unnecessary variable
no_prop
and its associated flag. Instead, we can directly returnNone
if no properties are found. - Combined the assignment statements for
Metal_A
,Metal_B
, andMax_H
into a single line for better readability. - Removed the
no_prop
check after the loop since we can directly returnNone
if no properties are found. - Simplified the dictionary creation by directly assigning the values to the keys.
These changes improve the readability of the code by reducing unnecessary variables and simplifying the logic. There is no significant impact on performance or space complexity.
Just replace /path/to/your/project
with the root directory of your Python project. GPT4Readability will analyze your code and generate a file named CodeImprovements.md
with suggestions for enhancements. The suggestions are always formatted the same way:
- Path to the file
- Function name
- Original Function
- Suggested change
- Explanation of why the change was suggested
It’s important to add an explanation to the suggested code adjustments because it can increase transparency and even be a learning opportunity. By reviewing these suggestions, you’re not just improving your code — you could also be improving as a developer.
Wrapping Up
GPT4Readability is a highly useful tool for understanding and improving your Python projects!
If GPT4Readability saves you time or you have any questions, please feel free to reach out to me (you can use any of the links in the section below). Also, please consider giving the GitHub repository a star!! If you find a bug or have a suggestion for improving GPT4Readability, don’t hesitate to open an issue on the GitHub Issues page.
Connect with Me!
I’m an aspiring deep-learning researcher currently working as a Computer Vision Engineer at KEF Robotics in Pittsburgh! Connect with me, and feel free to reach out to chat about anything ML related!
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.
Published via Towards AI