DEV Community

Cover image for Effortless Documentation of your Python Code with Github Actions and GPT3
Dhanush Reddy
Dhanush Reddy

Posted on

Effortless Documentation of your Python Code with Github Actions and GPT3

What I built

I built a Github Actions workflow that automates the process of adding docstrings to Python functions using GPT-3. The workflow loops over all .py files and functions in each one of them, sends the function code to the GPT-3 API for analysis, and inserts the suggested docstring for the function if it does not already have one.

This solution streamlines the process of documenting Python code and saves developers time and effort by automating the task of adding docstrings to functions. By using Github Actions and GPT-3, my solution helps developers to focus on other aspects of their work while maintaining high-quality code documentation.

Category Submission:

Maintainer Must-Haves

App Link

github.com/dhanushreddy291/docstring-generator

Screenshots

An Image showing the demo of Github Action

Another Image showing the demo of Github Action

Description

My project is a Github Actions workflow that automates the process of adding docstrings to Python functions using GPT-3. The solution is designed to streamline the process of documenting Python code by automatically generating docstrings for functions that do not have one.

The workflow is triggered whenever changes are pushed to the repository, and it loops over all .py files to find functions without a docstring. For each such function, the code is sent to the GPT-3 API to analyze and suggest a corresponding docstring. The functions having a docstring are ignored during the workflow. Finally the modified code is automatically commited to the Github Repo.

By automating the task of adding docstrings to Python functions, my project saves developers time and effort, allowing them to focus on other aspects of their work. The solution also helps maintain high-quality code documentation, which is essential for the long-term maintainability and scalability of any software project.

Of course, I am just scratching the surface with what is possible with Github Actions and GPT3. One can use GPT4-32K Token Model API to even build a full Markdown powered documentation by analyzing the code. At the moment, my workflow works only for Python but can be easily extended to other languages.

Link to Source Code

github.com/dhanushreddy291/docstring-generator

Permissive License

MIT

Background (What made you decide to build this particular app? What inspired you?)

As a software developer, I have often found the process of writing code documentation to be time-consuming. I knew that there had to be a better way to document code that did not require manual effort and could produce consistent and reliable results.

That's when I came across GPT-3, a powerful language model developed by OpenAI, which has been trained on vast amounts of text and code data, that can generate humanlike responses to natural language inputs. I realized that it could be leveraged to automate the process of adding docstrings to Python functions. Because why not? If Github Copilot is being used to write 46% of code, then why not do the same for code documentation.

That's why I decided to build a Github Actions workflow that uses GPT-3 to generate docstrings for Python functions automatically. My goal was to create a solution that would save developers time and effort, while also improving the quality of code documentation.

Overall, I was inspired by the idea of using GPT3 to automate a task that had traditionally been done manually. I believe that my project has the potential to transform the way that developers approach code documentation, making it easier, faster, and more reliable than ever before. Offcourse the documentation will keep on improving once we use better LLM's such as GPT4 and beyond.

How I built it (How did you utilize GitHub Actions or GitHub Codespaces? Did you learn something new along the way? Pick up a new skill?)

For this project, I utilized GitHub Actions to automate the process of adding docstrings to Python files in my repository. The workflow is triggered on a push event to the main branch of the repository.

It has the following steps:

  • Check out repository step is used to checkout the latest version of the repository.
  • Set up Python and install dependencies step is used to set up the Python environment with version 3.10 and installed the dependencies required.
  • Run add_docstring script step is used to execute the add_docstring.py script with the path to the file that needs to be updated.
  • Check for changes step is used to check if any changes have been made to the repository. If there are changes, it sets the output has_changes to true.
  • Commit and push changes step is used to commit and push the changes back to the main branch.

The add_docstring.py script utilizes the OpenAI GPT-3 language model to generate docstrings for the Python functions in the repository. The script is also responsible for formatting the Python files using the black and autoflake libraries.

During this project, I learned how to use GitHub Actions and integrate them into my workflow. I also learned how to use the RedBaron library to parse and manipulate Python code.

I do admit I faced a few challenges. The main problem was working with the OpenAI API. To avoid rate limiting, I had to add a time delay of 20 seconds after every request (as free trial accounts have a hard cap of 3 req/min). This was necessary to ensure that the API would not block the requests, resulting in failed workflow.

Another challenge was integrating the script with GitHub Actions. I had to learn how to create a workflow file and configure the necessary settings to ensure that the script would run automatically whenever changes were pushed to the main branch. I also had to debug some issues related to permissions for commiting back to the repo again.

Overall, the project was a great learning experience that helped me improve my skills in working with APIs and automating tasks using GitHub Actions.

Additional Resources/Info

You can find the entire source code used here

Top comments (4)

Collapse
 
maxprilutskiy profile image
Max Prilutskiy

Awesome stuff!

I've done the other way around, and extracted meaning from the code. From the commit messages to be exact: I've open-sourced my AI-powered GitHub Action that posts change logs directly into Slack every Monday morning

Having JSDoc here would be really cool btw! 😎

Collapse
 
dhanushreddy29 profile image
Dhanush Reddy

Thanks @maxprilutskiy

Your GitHub Action sounds really interesting too, will definitely check it out. And yeah, I would add JSDoc hopefully soon 😅

Collapse
 
zirkelc profile image
Chris Cook

Very cool idea! Did you consider extending the Action to work with other languages/formats as well? JSDoc for JavaScript might be a good extension.

Collapse
 
dhanushreddy29 profile image
Dhanush Reddy

Sure @zirkelc

I will do checkout JSDoc and maybe add it into the current workflow. 😀