TL;DR
This paper introduces CGCode, a novel detector for identifying ChatGPT-generated code assignments. It leverages the observation that LLM-generated code exhibits more consistent perplexity and is less sensitive to minor perturbations than human-written code, achieving significantly better detection rates than existing methods.
What Problem Does This Paper Address?
The rise of powerful large language models (LLMs) like ChatGPT presents a significant challenge to academic integrity in programming education. Students can now generate code assignments with relative ease, potentially undermining the learning process. While detectors for LLM-generated text exist, a reliable method for detecting LLM-generated code has been lacking. This paper aims to fill that gap by developing a specialized detector for code assignments.
Methods
The core of the CGCode detector lies in a two-pronged approach: perturbation and scoring.
- Perturbation: Instead of randomly masking tokens like in text detectors, CGCode uses a more targeted strategy. It first calculates the perplexity (PPL) of each line of code. Lines with higher perplexity are then selected for masking. This is because high-perplexity lines are often more complex or less predictable, making them good candidates for revealing subtle differences. After masking, a fine-tuned CodeBERT model is used to fill these masks, creating slightly modified versions of the original code. This "mask-filling" task is performed using Nucleus Sampling to ensure a diverse set of modified samples.
- Scoring: The detector then evaluates the original and perturbed code samples using a scoring mechanism that combines three metrics:
- Overall Perplexity: The average perplexity of the code.
- Perplexity Variation: The standard deviation of perplexity across lines of code.
- Burstiness: A measure of how "bursty" or clustered the perplexity is.
The hypothesis is that LLM-generated code, due to its inherent coherence, will have consistently low perplexity and low variation. Perturbing such code is unlikely to result in a significantly lower score. Conversely, human-written code, being more varied and potentially less predictable, might yield a lower score after perturbation, indicating it's more likely to be human-authored.
Datasets
The authors created the CGCode dataset for this research. It's based on IBM's CodeNet and aims to simulate student programming submissions. The dataset includes code in six programming languages: C, C++, C#, Java, JavaScript, and Python. After cleaning and removing duplicates/comments, 80% of the data was used for fine-tuning CodeBERT. The remaining 20% was split for validation and testing. To further enhance the test set, the authors used OpenAI's text-davinci-003 to generate code for 400 problems and perform code translations. The final test set contains 5,214 samples.
Key Insights and Ideas
- Perplexity as a Code Fingerprint: The paper highlights that perplexity, a measure of how well a language model predicts a sequence, can serve as a valuable indicator of code origin. LLM-generated code tends to have a more predictable and consistent perplexity profile.
- Targeted Perturbation is Key: Random masking is less effective for code. By focusing on high-perplexity lines, the detector can more efficiently reveal the underlying differences between human and LLM-generated code.
- LLM Code is "Smoother": The core idea is that LLM-generated code is inherently "smoother" in terms of perplexity and less prone to dramatic score changes after minor modifications compared to human code. This makes it harder for LLM-generated code to achieve a lower score after perturbation, while human code might.
Key Results
The CGCode detector significantly outperforms existing detectors. When tested against models like GPT2-Detector, DetectGPT, and GPTZero, CGCode achieved a substantial improvement in the Area Under the Curve (AUC) metric. For instance, against OpenAI's text-davinci-003, the average AUC rose from 0.56 (GPTZero baseline) to 0.87. The paper also reports low False Positive Rates (FPR) and False Negative Rates (FNR) across all tested programming languages, indicating high accuracy and reliability.
Limitations and Future Work
The paper acknowledges that the current work focuses on specific LLMs and code generation tasks. Future work could involve extending the detector to be more robust against a wider range of LLMs and code generation techniques. Additionally, exploring other perturbation strategies or scoring metrics could further enhance the detector's performance. The authors also suggest that the flexibility of their method allows for extension to other code generation models beyond ChatGPT.
Summary updated