Self-Explained Keywords Empower Large Language Models for Code Generation

Lishui Fan, Mouxiang Chen, Zhongxin Liu · DOI

Abstract

Large Language Models (LLMs) often struggle with code generation tasks involving rare or problem-specific terminology. This paper introduces Self-Explained Keywords (SEK), a novel prompting framework that enables LLMs to identify, define, and rank crucial keywords before generating code. By bridging the gap between general linguistic knowledge and specific code implementation, SEK achieves significant performance gains across major benchmarks, including a jump from 85.4% to 93.3% accuracy on HumanEval for DeepSeek-Coder-V2.

The Long-Tail Challenge in Code Generation

While LLMs are proficient in common programming patterns, they suffer from a "long-tail distribution" problem. Real-world coding problems often use specific, rare terminology that was underrepresented in the model's training data.

For example, a model might perfectly understand "even numbers," but if a prompt asks for "even digits" (0, 2, 4, 6, 8), the model might misinterpret it as "even numbers" because the specific phrase "even digits" rarely appeared in its code-centric training. This gap between general linguistic understanding and specific code implementation leads to bugs. The authors argue that the model often possesses the knowledge to define these terms from its general pre-training but needs a structured way to "bridge" that knowledge into the coding task.

Figure 3: Comparison between a standard LLM failure on "even digits" and the correct solution achieved by analyzing key terms.

The SEK Framework

The researchers propose a three-step pipeline called SEK (Self-Explained Keywords) that mimics a pragmatic developer defining requirements before coding:

  1. KeyExtract & Explain: Using a few-shot prompt, the LLM analyzes the problem description to identify crucial keywords (e.g., "nonagonal number" or "1-based indexing"). It then generates a formalized explanation for each, ensuring definitions are consistent with provided test cases.
  2. KeyRank: Not all keywords are equally difficult. The authors use a TF-IDF (Term Frequency-Inverse Document Frequency) ranking algorithm to prioritize keywords that appear rarely in general code corpora. These "difficult" terms are moved to the front of the list to combat "position bias," where LLMs pay more attention to the beginning of a prompt.
  3. PromptEnrich: The original problem description is appended with these ranked, self-generated explanations. This "enriched" prompt is then fed back into the LLM to generate the final code solution.
Figure 6: The SEK workflow, illustrating the KeyExtract & Explain, KeyRank, and PromptEnrich stages.

Experimental Evaluation and Results

SEK was evaluated across major benchmarks including HumanEval, MBPP, and the more challenging APPS dataset. The framework provided consistent, substantial gains across five representative models (Llama-3.1-70B, Mixtral-8x22B, DeepSeek-Coder-V2, GPT-3.5-turbo, and GPT-4o-mini):

  • Performance Jumps: DeepSeek-Coder-V2-Instruct saw a massive jump on HumanEval, moving from 85.4% to 93.3% (Pass@1).
  • Complexity Scaling: Gains were most pronounced on complex tasks (like the APPS Competition level), suggesting SEK is most useful when the cognitive load of the problem is high.
  • Outperforming Baselines: SEK consistently beat other popular prompting methods like Chain-of-Thought (CoT), largely because CoT often focuses on the logic of steps rather than the precise definitions of requirements.

Key Insights

  • LLMs as Their Own Tutors: LLMs often possess the semantic knowledge to define a term even if they haven't seen enough examples of how to code it. Forcing the model to explain the term first activates this latent knowledge.
  • Attention Shifting: Through attention visualization, the authors proved that SEK changes the model's internal behavior. Without SEK, models often ignore rare keywords; with SEK, the model's attention shifts toward the rare terms and their high-frequency semantic counterparts.

Limitations and Future Work

While effective, SEK requires a two-stage invocation, which doubles inference cost and latency. Additionally, the quality of the final code is heavily dependent on the accuracy of the initial explanation; if the LLM hallucinates a wrong definition, the resulting code will likely fail. Future work may explore compressing this process into a single pass or making the keyword ranking dynamic for specialized domains.

Summary updated

BibTeX
@misc{fan2024self,
  author = {Lishui Fan and Mouxiang Chen and Zhongxin Liu},
  title = {{Self-Explained Keywords Empower Large Language Models for Code Generation}},
  year = {2024},
  eprint = {2410.15966},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.15966},
}
Figure