Skip to main content
QUICK REVIEW

[Paper Review] AI-assisted coding: Experiments with GPT-4

Russell A. Poldrack, Thomas Lu|arXiv (Cornell University)|Apr 25, 2023
Machine Learning and Data ClassificationComputer Science32 citations
TL;DR

GPT-4 can generate usable code and improve readability through refactoring, but human validation remains essential for correctness and reliability of tests. Tests generated by GPT-4 often fail and require debugging.

ABSTRACT

Artificial intelligence (AI) tools based on large language models have acheived human-level performance on some computer programming tasks. We report several experiments using GPT-4 to generate computer code. These experiments demonstrate that AI code generation using the current generation of tools, while powerful, requires substantial human validation to ensure accurate performance. We also demonstrate that GPT-4 refactoring of existing code can significantly improve that code along several established metrics for code quality, and we show that GPT-4 can generate tests with substantial coverage, but that many of the tests fail when applied to the associated code. These findings suggest that while AI coding tools are very powerful, they still require humans in the loop to ensure validity and accuracy of the results.

Motivation & Objective

  • Assess how GPT-4 performs in interactive data-science coding tasks with minimal prompt engineering.
  • Evaluate GPT-4's ability to refactor and improve the quality of existing Python code.
  • Examine GPT-4's capability to generate tests for its own code and the resulting test coverage.
  • Assess overall utility and limitations of AI coding assistants for scientific researchers.

Proposed method

  • Interactive code generation with GPT-4 via ChatGPT on data science problems; multiple prompts used to fix and complete code; success judged within ~5 minutes per problem.
  • Refactoring of existing Python code using GPT-4, using a dataset of 274 candidate files from GitHub filtered for quality and relevance; code quality assessed with static analysis and maintainability metrics.
  • Automated test generation: GPT-4 creates prompts to generate code plus tests for multiple domains; executed scripts to assess runability and test coverage using Coverage.py and pytest.
  • Quantitative comparison of original vs refactored code across metrics (readability, maintainability, complexity, and style) using flake8 and radon packages; statistical tests (paired t-tests with FDR correction) report differences.
  • Assessment of limitations: analysis of incorrect or hallucinated outputs, the need for human validation, and discussion of prompting techniques (e.g., chain-of-thought) as future work.
Figure 1: Proportion of successful code outcomes as a function of number of prompts. NS: not successful.
Figure 1: Proportion of successful code outcomes as a function of number of prompts. NS: not successful.

Experimental results

Research questions

  • RQ1Can GPT-4 produce working Python code for data science problems with minimal prompt engineering and human guidance?
  • RQ2Does GPT-4 refactor existing code to improve readability and maintainability without changing behavior?
  • RQ3Can GPT-4 generate tests for its own code with substantial coverage, and how often do these tests fail or require debugging?
  • RQ4What is the overall utility and limitation of GPT-4 as an AI coding assistant for researchers?

Key findings

  • 72% of attempts (23/32) produced successful code outcomes with GPT-4, with 37.5% (12/32) successful on the first prompt.
  • Refactored code had substantially fewer flake8 messages per line (0.23 vs 0.09), indicating improved standards compliance and readability (Cohen’s d = 0.50).
  • Code quality metrics showed improvements after refactoring: maintainability index, Halstead metrics, and cyclomatic complexity improved on average (small-to-medium effect sizes, p < .05 after FDR).
  • GPT-4 generated tests for its code with high coverage, but only 45 of 100 tests passed after integration, with many tests failing due to incorrect assumptions or missing context.
  • Most scripts (97/100) executed without failure, but substantial debugging was needed to address failures or misalignments between tests and code.
  • The study emphasizes that human involvement remains essential for correctness, especially for mathematical and domain-specific aspects, even with GPT-4.
Figure 2: Number of Flake8 messages (per line of code) for original github files and refactored files.
Figure 2: Number of Flake8 messages (per line of code) for original github files and refactored files.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.