Automating Code Comments with GPTDoc: A Step-by-Step Guide

matiasvlevi

Matias Vazquez-Levi

Posted on June 6, 2023

Automating Code Comments with GPTDoc: A Step-by-Step Guide

As developers, we are always looking for ways to improve the efficiency of our workflow while maintaining the quality of our code. One essential aspect of any well-organized codebase is its documentation. Good documentation ensures that the intent and purpose of the code are well understood by both present team members and future maintainers of the codebase.

However, writing comprehensive and accurate documentation is a time-consuming process. What if we could automate the process of generating code comments for our functions? This is where GPTDoc comes in. GPTDoc is a code comment generator tool that leverages OpenAI models to generate documentation for your code automatically.

 

Requirements

  • Node >=18.0.0
  • npm >=8.0.0

Using

 

Getting Started with GPTDoc

Install gptdoc as a dev dependency in the project you want to document

 

Installing

npm i --save-dev gptdoc
Enter fullscreen mode Exit fullscreen mode

 

Configuring

Create a configuration file named .gptdoc in the root of your project

{
    "DEBUG": false,
    "framework": "JSDOC",
    "language": "JS",
    "files": {
        "src": "./src",
        "dest": "./gpt",
        "recursive": true
    },
    "openai": {
        "temperature": 0.7,
        "top_p": 1,
        "max_tokens": 256,
        "model": "text-davinci-003"
    },
    "prompt": "Add property tags with @prop"
}
Enter fullscreen mode Exit fullscreen mode

Change the src directory to your directory holding your project's source code.

 

OpenAI API Key

You need to have access to OpenAI's private beta API, and provide a token.

Create a file named .env in the root of your project

OPENAI_API_KEY="your-key-here"
Enter fullscreen mode Exit fullscreen mode

You can find your token here

Auto-Document

Every method or class with a /** @gpt */ code comment will be documented

/** @gpt */
function foo(x) {
    let sum = 0;
    for (let i = 0; i < x.length; i++) {
        sum += x[i];
    }
    return sum;
}
Enter fullscreen mode Exit fullscreen mode

Run the script to document

node node_modules/gptdoc
Enter fullscreen mode Exit fullscreen mode

If everything was configured properly, you should see a copy of your codebase in ./gpt which holds all the documentation comments

Here is an example of the output

/**
 * @autogenerated
 * @description Calculate the sum of all elements in an array.
 * @function foo
 * @param {number[]} x - An array of numbers to sum up.
 * @returns {number} The sum of all elements in the input array.
 */
function foo(x) {
    let sum = 0;
    for (let i = 0; i < x.length; i++) {
        sum += x[i];
    }
    return sum;
}
Enter fullscreen mode Exit fullscreen mode

 

We learned how to automate the generation of code comments using GPTDoc. By following these steps, you can save valuable time in maintaining your codebase and focus on other important tasks in your project. Don't forget to always review the autogenerated comments to ensure their accuracy and to maintain a high level of quality in your documentation. Keep a Human in the loop!

💖 💪 🙅 🚩
matiasvlevi
Matias Vazquez-Levi

Posted on June 6, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related