arXiv:2508.16131cs.SEcs.AI2025-08被引 4

不同编程语言下大模型代码生成信心差异显著,可指导开发选型。

The Fools are Certain; the Wise are Doubtful: Exploring LLM Confidence in Code Completion

  • 用困惑度衡量大模型在代码生成中的自信程度,跨语言、模型和数据集验证。
  • 强类型语言如Java困惑度低,脚本语言如Shell困惑度高,排名稳定。
  • 代码注释影响小,适合开发者根据语言和模型选择生成方案。

代码补全任务是在上下文基础上生成缺失的代码片段,能提升开发效率并作为代码发现工具。近期该任务多采用在代码数据上微调的大语言模型(代码LLM)。其性能可通过下游任务指标或内在指标评估。下游指标虽反映实际应用价值,但计算复杂且依赖领域知识;而内在指标如困惑度、熵和互信息,可衡量模型置信度或不确定性,简单通用,被提议作为生成代码功能正确性和幻觉风险的代理指标。为此,我们通过测量多种大模型在881个GitHub项目中2254个文件上的代码困惑度,系统评估了代码生成中的模型信心。结果表明:强类型语言困惑度低于动态类型语言,脚本语言困惑度较高,Shell普遍困惑度高,而Java则普遍较低。模型选择显著影响困惑度,同一模型下语言排名在不同数据集间保持稳定。代码注释虽略微增加困惑度,但不改变语言间的整体排名。研究结论可供研究人员、开发者和用户依据语言、模型及代码特征判断大模型代码补全的适用性。

原文摘要 · Abstract (English)

Code completion entails the task of providing missing tokens given a surrounding context. It can boost developer productivity and serve as a code discovery tool. Code completion has recently been approached with Large Language Models (LLMs) fine-tuned on code (code LLMs). The performance of code LLMs can be assessed with downstream and intrinsic metrics. Downstream metrics are usually employed to evaluate the practical utility of a model, but can be unreliable and require complex calculations and domain-specific knowledge. In contrast, intrinsic metrics such as perplexity, entropy, and mutual information, which measure model confidence or uncertainty, are simple, versatile, and universal across LLMs and tasks, and have been proposed as proxies for functional correctness and hallucination risk in LLM-generated code. Motivated by this, we evaluate the confidence of LLMs when generating code by measuring code perplexity across programming languages, models, and datasets using various LLMs, and a sample of 2254 files from 881 GitHub projects. We find that strongly-typed languages exhibit lower perplexity than dynamically typed languages. Scripting languages also demonstrate higher perplexity. Shell appears universally high in perplexity, whereas Java appears low. Code perplexity depends on the employed LLM; under a fixed model, relative language-level rankings are largely stable across evaluation corpora. Although code comments modestly increase perplexity, the language ranking based on perplexity is barely affected by their presence. LLM researchers, developers, and users can use our findings to assess the suitability of LLM-based code completion in specific software projects based on how language, model choice, and code characteristics impact model confidence.

代码生成大模型困惑度编程语言

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。