Source-linked AI summary

AI-assisted coding: Experiments with GPT-4

Russell A Poldrack, Thomas Lu, Gašper Beguš

arXiv:2304.13187v1cs.AIcs.SE

TL;DR

This paper examines GPT-4’s usefulness for practical coding, refactoring, and test generation. Across these experiments, GPT-4 showed strong coding abilities, but errors and unreliable tests made human validation essential.

  • Problem

    The paper asks how useful GPT-4 is for practical data-science coding and whether it can improve existing research code, whose quality matters for reproducibility, transparency, reuse, and maintainability.

    Source: §1 · p. 1, §2.1 · p. 3
  • Method

    The authors tested interactive GPT-4 coding and debugging, code refactoring, and automatic code and test generation.

    Source: §1 · p. 1, §2 · p. 2, §2.1 · p. 3
  • Results

    GPT-4 showed strong Python code-generation ability, improved common code-quality metrics through refactoring without assessing accuracy, and generated high-coverage tests that failed a majority of the time.

    Source: §2.3 · p. 6
  • Takeaways & Limitations

    Human involvement remains essential for code validity and accuracy, especially for mathematical programming, while tests intended to assess behavior should be designed by people with domain expertise.

    Source: §2.3 · p. 7

    Mathematical processing remained challenging, and recognizing an incorrect hurdle-model implementation required sophisticated domain knowledge.

    Source: §2 · pp. 2–3

Abstract

from arXiv · show

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.

1 Introduction

The paper examines practical implications of GPT-4-assisted coding beyond benchmark assessments, including interactive coding, code refactoring, and test generation. It uses these experiments to assess the tool’s utility for researchers.

1 Introduction

  • The paper explores GPT-4-assisted coding more qualitatively than prior benchmarking assessments, focusing on its implications for research coding.The experiments examine interactive coding and debugging, refactoring existing code, and generating tests for code.Source: §1 · pp. 1–2
  • Interactive coding tests how much effort a researcher with minimal prompt-engineering expertise needs to solve data-science problems.The paper uses the ChatGPT interface to GPT-4 for this experiment.Source: §1 · pp. 1–2
  • The other experiments assess GPT-4’s ability to improve existing code quality and write tests for its own code across scientific domains.The paper concludes with an overall assessment of AI coding assistants for scientific researchers.Source: §1 · p. 2

2 Coding with GPT-4

Across interactive coding, refactoring, and test generation, GPT-4 showed useful capabilities, but its outputs still required human validation for accuracy. Refactoring improved measured code quality, while generated tests often failed and needed debugging.

2 Coding with GPT-4

  • 72% (23/32) of interactive coding attempts succeeded relatively quickly, including 37.5% (12/32) on the first prompt.Common issues with additional prompting included outdated library functions or datasets; causes of failures varied, including incompatible library versions and incorrect API labels.Source: §2 · p. 2

2.1 Refactoring code using GPT4

  • Refactored code had fewer Flake8 messages per line than the original, while other measured quality metrics changed with small-to-medium effect sizes.Flake8 messages fell from a median of 0.23 per line for original code to 0.09 for refactored code (Cohen’s d = 0.50); other metrics had Cohen’s d values from 0.13 to 0.33, with differences significant after false discovery rate correction.Source: §2.1 · p. 3, Figure 2 · p. 4, Figure 4 · p. 5

2.2 Automatically generated code and tests

  • Of 100 generated scripts, 97 executed successfully, but only 45 of 100 generated test sets completed successfully after minor code modifications.Most files had 100% test coverage, and 94% had at least 50% coverage; value assertions were the most common test failure, and debugging was often needed to determine whether code or tests were wrong.Source: §2.2 · p. 6, Figure 5 · p. 7

2.3 Conclusions

  • GPT-4 showed strong Python code-generation abilities, but errors mean humans must remain involved to ensure code accuracy.A relatively novice prompt engineer solved most coding problems within a small number of prompts, though a sizeable minority would have required substantial debugging; improved outcomes from restarting in a new context remain an open question.Source: §2.3 · pp. 6–7
  • Refactoring improved measured code quality and standards compliance, but did not establish code accuracy or eliminate all standards issues.The authors note that combining GPT-4 refactoring with tools such as black would likely yield further improvements.Source: §2.3 · p. 6
  • Generated tests had high coverage but failed a majority of the time, requiring human effort to diagnose whether code, tests, or both were inaccurate.The authors conclude that specific test examples should be designed and implemented by a human with domain expertise.Source: §2.3 · pp. 6–7
  • The authors identify mathematical coding as a particular setting where human validation remains essential, citing a generated EZ-diffusion formula that bore little resemblance to the original.The experiment also showed a hurdle-model implementation that differed from the reference approach despite fitting the test data nearly as well.Source: §2 · pp. 2–3, §2.3 · p. 7
  • The authors caution that their prompts may underestimate model performance and describe the results as a lower bound.They cite chain-of-thought prompting as a possible way to improve performance on complex coding problems, while noting that further study is needed.Source: §2.3 · p. 7

3 Appendix 1: Prompts used for interactive coding with GPT-4

Appendix 1 lists interactive GPT-4 coding prompts spanning statistical analyses, algorithms, data processing, and visualization. Bracketed numbers indicate prompts needed for judged success, while NS marks problems abandoned before success.

3 Appendix 1: Prompts used for interactive coding with GPT-4

  • The prompts cover statistical modeling and analysis, including regression statistics, drift-diffusion estimation, mixed-effects models, reinforcement-learning analysis, and cross-validation.They also ask for simulations of overfitting and randomization-based hypothesis testing.Source: §3 · pp. 8–10
  • Data and text tasks range from PubMed search and topic modeling to sentiment analysis, weather retrieval, and scraping researchers’ email addresses.A separate prompt requests downloading recent Python files from GitHub.Source: §3 · pp. 8–9
  • Some tasks explicitly request tests or test data, including test data for an earlier prompt, graph-separation comparison, and automatically generated data files.The appendix legend says bracketed values count prompts needed for judged success and NS denotes abandonment before success.Source: §3 · pp. 8–9
  • Other prompts ask for text-file animation, ASCII-art rendering, Markdown-to-PDF conversion, image smoothing, and distribution visualizations.Several prompts specify plots or figures as part of the requested output.Source: §3 · pp. 8–9
  • Algorithm and modeling tasks include implementing transformers, graph separation, causal inference, greedy equivalence search, and factor analysis.Other prompts request principal component analysis from scratch and a logistic-map plot.Source: §3 · p. 9
Loading 2304.13187v1…