Article / 26 March 2023

A Dev's Journey Through the AI Landscape: How I use ChatGPT and Sphinx to write my Python documentation.

Introduction

ChatGPT has revolutionized productivity in various fields including game development. Its capabilities have made it possible to complete tasks much more efficiently, leaving developers with more time to focus on what they love – writing code (for now :D). However, I find that this increase in productivity has a downside for me: it has become difficult to know when to stop and take a break. 

The more and more I was writing this article, it began to feel like a journaling exercise for myself, attempting to put words to some of the thoughts and concerns I have about this new technology. I will try to set aside the anxiety-inducing aspects of the rapidly evolving AI landscape. With AI technologies bombarding us daily and progress appearing to be exponential, it raises significant questions about what this means for humans and our inherent need to feel useful. 

Instead, I'll share my personal experience with using ChatGPT and one of the ways I use it to write Python docstrings and document my code, as well as offer a glimpse into the challenges I've faced in finding the right balance between productivity and well-being in this new era of accelerated efficiency.

Efficient Python Documentation with ChatGPT 

One of the most time-consuming tasks for a developer is writing docstrings for their functions, or at least it was for me: it becomes monotonous which leads to boredom, it interrupts the flow of coding since I need to switch my focus from writing code to document it, and not just that but some functions may involve complex logic or algorithms that are difficult to explain concisely in a docstring especially if English is not your mother tongue. Struggling to find the right words or phrasing would make the process even less enjoyable and more time-consuming for me. 

Docstrings are essential for ensuring that the rest of the team understands the purpose and usage of each function. ChatGPT has simplified this process, allowing me to generate docstrings in Google's format with ease. This not only reduces the time spent on documentation but also ensures consistency and adherence to best practices if you write good prompts. 

The crazy thing is that ChatGPT has proven to be highly proficient and versatile, successfully generating accurate and informative docstrings not only for simple functions but also for complex decorator functions, it's been a game-changer in making my documentation process way more efficient.

Integrating ChatGPT, Python Annotations, and Sphinx for Seamless Documentation

After generating docstrings with ChatGPT (in a matter of seconds), I review, modify, and add them to their respective functions. To create a seamless documentation process, I also integrate Python annotations and Sphinx, a documentation generation tool. Here's what the workflow looks like:

  1. Python Annotations: While writing code, I add type hints using Python annotations to specify the expected input and output types for each function. This not only helps with code readability but also provides valuable information for generating more accurate and informative docstrings. 
    Example:
  2. ChatGPT Docstring Generation: Using a prompt that includes the function along with type annotations, I ask ChatGPT to generate a Google-style docstring for the function. This ensures that the generated docstring is consistent with the function's purpose and adheres to best practices. 
    Example prompt for ChatGPT: 
    Generate a Google-style docstring for the following Python function:
    
    def read_json(file_path: str) -> Union[dict, list, None]:
        if os.path.exists(file_path):
            with open(file_path, 'r') as file:
                file_content = file.read()
                if not file_content:
                    return None
                try:
                    data: Any = json.loads(file_content)
                except json.JSONDecodeError as e:
                    raise json.JSONDecodeError(f'Error decoding JSON file: {file_path}\n{str(e)}')
            return data
        else:
            raise FileNotFoundError(f'{file_path} not found')
  3. Sphinx Integration: I use Sphinx to generate well-structured and readable documentation based on the docstrings and annotations. Sphinx automatically extracts the information from the code, creating comprehensive documentation that includes function descriptions, input and output types, and any additional notes or examples.
  4. Automated Documentation Updates: To keep the documentation up-to-date, I set up a batch file or script that regenerates the documentation whenever changes are made to the code. This ensures that the documentation remains current and accurate, minimizing the need for manual updates.

By following this workflow, I can create high-quality and consistent documentation in a matter of minutes, leveraging the power of ChatGPT, Python annotations, and Sphinx to streamline the entire process.

The Challenge

ChatGPT has undoubtedly enhanced productivity in the software and game development realm, but it also brings forth a challenge: recognizing when to step back from work. The heightened efficiency sometimes makes it arduous for me to disengage from coding tasks and take much-needed breaks. As a technical artist, I constantly ponder the implications of this technology's advancements for our industry. Regardless of the outcomes, the influence of ChatGPT on productivity for those who adopt it is indisputable. Thus far, I've been able to achieve efficiency gains which have freed up more time for me to focus on creative pursuits and innovation in my projects.

I encourage you, to embrace ChatGPT and to try and integrate it into your development processes, exploring innovative and creative ways to incorporate this technology. Ignoring or fearing this technological wave is not the answer. As we collectively contemplate the implications of AI and the evolution of our jobs, it's vital to engage in open conversations, address emerging challenges, and seize opportunities for growth. 

Thanks for reading,  

Sergi

Resources

ChatGPT: https://chat.openai.com/

Sphinx: https://www.sphinx-doc.org/en/master/