NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Improving ChatGPT Prompt for Code Generation

Chao Liu, Xuanlin Bao, Hongyu Zhang, Neng Zhang, Haibo Hu, Xiaohong Zhang, Meng YanarXiv 2023 · DOI

While Large Language Models (LLMs) like ChatGPT have demonstrated remarkable general-purpose capabilities, their performance on specialized tasks such as software engineering can be inconsistent. Basic requests often yield buggy, overly verbose, or non-idiomatic code. This research explores how systematic "prompt engineering"—specifically utilizing Chain-of-Thought (CoT) strategies and multi-step optimizations—can bridge the gap between a generalist chatbot and a high-performing code generator.

The Challenge of Generalist Models in Coding

The primary hurdle identified is that while specialized models (e.g., CodeBERT or CodeT5) are fine-tuned on massive code datasets to master syntax and structure, ChatGPT remains a generalist. Developers frequently encounter outputs that ignore project conventions, include unnecessary comments, or misuse APIs. To make ChatGPT a viable tool for professional development, a systematic framework for prompt design is required to guide the model toward high-quality, functional code without the need for retraining.

Methodology: Multi-Step Prompt Optimization

The researchers employed a Chain-of-Thought (CoT) strategy combined with multi-step optimizations. The core of their approach involved categorizing prompts into five distinct types (P1 through P5) to provide structured guidance to the model.

Table showing the specific prompt templates (P1-P5) used for Text-to-Code and Code-to-Code tasks.

As shown in the table above, the prompts range from basic task descriptions (P1) to complex behavioral and context-aware instructions (P2, P5). By providing the model with the surrounding "environment"—such as class names and existing member variables—and explicit processing instructions (e.g., "remove comments"), the researchers were able to significantly constrain the model's output to match desired specifications.

Experimental Results

The study utilized the CodeXGlue benchmark, focusing on two primary tasks: CONCODE (Text-to-Code), generating Java from natural language, and C2C (Code-to-Code), translating C# to Java. Performance was measured using CodeBLEU, a metric that evaluates grammatical and logical correctness alongside text similarity.

Table illustrating the cumulative performance gains (BLEU and CodeBLEU) as different prompt types are combined.

The results demonstrate a dramatic improvement when using optimized prompts. For the Text-to-Code (T2C) task, a basic prompt (P1) achieved a CodeBLEU score of 22.76. By layering context, behavior, and processing instructions (P2+P5+P3), the score surged to 46.78—a massive improvement over the baseline. In many instances, the optimized ChatGPT outperformed specialized, fine-tuned models like CodeGPT-adapted and PLBART.

Key Insights

Several critical findings emerged from the optimization process:

  • The Power of "Concise": Simply adding the keyword "concise" to a prompt forced the model to eliminate "fluff" and focus on logic that closely matched ground-truth data.
  • Context is Essential: Providing the model with the surrounding class architecture (P2) was one of the single largest drivers of accuracy.
  • Reduced Randomness: As prompts became more specific and structured, the inherent randomness of the LLM's output decreased, leading to more stable and predictable code generation.
  • Session Memory: For translation tasks, maintaining a single continuous chat session allowed the model to "learn" from previous snippets, outperforming fresh sessions.

Conclusion and Future Work

While prompt engineering significantly boosts ChatGPT's coding performance, it still faces challenges in complex Code-to-Code translations compared to models like CodeT5, which possess a deeper holistic understanding of language-specific libraries. Future research will focus on automated prompt construction and the development of more nuanced evaluation metrics capable of detecting subtle "code smells" and design flaws that functional metrics like CodeBLEU might miss.

Summary updated

BibTeX
@misc{liu2023improving,
  author = {Chao Liu and Xuanlin Bao and Hongyu Zhang and Neng Zhang and Haibo Hu and Xiaohong Zhang and Meng Yan},
  title = {{Improving ChatGPT Prompt for Code Generation}},
  year = {2023},
  eprint = {2305.08360},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2305.08360},
}
Figure