arXiv:2506.11021cs.SEcs.AI2025-06被引 8

通过函数聚类消除大模型代码生成中的幻觉错误,提升安全性。

Eliminating Hallucination-Induced Errors in LLM Code Generation with Functional Clustering

  • 对生成代码进行输入输出行为聚类,以最大簇质量作为置信度
  • 在LiveCodeBench上将错误率从65%降至2%,保守阈值下为0%
  • 适用于闭源接口,可调节可靠性与覆盖率,适合生产级部署

现代代码生成大模型虽能解决大量编程问题,但仍会生成隐蔽错误,导致输出不安全。本文提出函数聚类——一种黑盒封装方法,几乎完全消除幻觉引发的错误,并提供可调置信度评分。该方法对候选代码进行采样,在自动生成的测试集上执行,将具有相同输入输出行为的代码归为一类;最大簇的实证质量即为精确置信度估计。用户仅需设定单一标量阈值,即可在覆盖范围与可靠性间权衡,获得指数级保障。在LiveCodeBench上,验证器在可解任务上保持基线pass@1性能,但将返回答案的错误率从约65%降至2%,在保守阈值下进一步降为0%,同时仍能响应15.6%的提示。人工审计显示残余错误源于提示理解偏差,而非随机生成噪声,指明未来改进方向为规范需求描述。由于方法仅依赖采样与沙箱执行,可直接应用于闭源API及未来模型,为实现可靠自主代码生成提供可行路径。代码已开源(https://github.com/20ChaituR/functional-clustering)。

原文摘要 · Abstract (English)

Modern code-generation LLMs can already solve a large fraction of programming problems, yet they still hallucinate subtle bugs that make their outputs unsafe for autonomous deployment. We present functional clustering, a black-box wrapper that eliminates nearly all hallucination-induced errors while providing a tunable confidence score. The wrapper samples many candidate programs, executes each on a self-generated test suite, and clusters candidates whose I/O behavior is identical; the empirical mass of the largest cluster serves as an exact confidence estimate. A single scalar threshold on this estimate lets users trade coverage for reliability with exponential guarantees. On LiveCodeBench our verifier preserves baseline pass@1 on solvable tasks yet slashes the error rate of returned answers from ~65% to 2%, and drives it to 0% at a conservative threshold while still answering 15.6% of prompts. Manual audits show that the few residual mistakes stem from prompt misinterpretation, not random generation noise, narrowing future work to specification clarity. Because the method requires only sampling and sandbox execution, it applies unchanged to closed-source APIs and future models, offering a practical path toward dependable, autonomous code generation. Our code is available on Github (https://github.com/20ChaituR/functional-clustering).

代码生成幻觉检测置信度评估自动验证

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