arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2511.12288cs.SE
收藏

通过语义三角化减少LLM生成代码中的幻觉

Reducing Hallucinations in LLM-Generated Code via Semantic Triangulation

Key Lab of HCST (PKU), MOE(北京大学计算科学研究所;教育部高速计算机技术重点实验室);SCS, Peking University

Yihan Dai, Sijie Liang, Haotian Xu, Peichu Xie, Sergey Mechtaev

AI总结:

本文提出语义三角化框架,通过将问题转换为需要不同算法的变体,提高代码正确性。实验显示,该方法在多个基准测试中提升了代码选择的正确率和F1分数。

摘要:

大型语言模型(LLM)能够从自然语言描述生成可执行代码,但生成的程序常因幻觉而存在错误。在缺乏正式规范的情况下,现有方法试图通过LLM生成的代理(如测试或自动形式化规范)来评估正确性。然而,这些代理由同一不完美模型生成,因此往往验证而非捕捉错误,尤其是在模型出现相关错误时。我们引入语义三角化,一种基于理论的框架,通过将原始问题转换为需要根本不同算法的变体,并检查独立采样解决方案之间的一致性。我们识别了该框架的理论要求,并证明在LLM幻觉的正式模型下,这些属性能提高程序正确性的信心。我们通过四个具体三角化方法实现了该框架,基于问题倒置、分解和解集枚举。在LiveCodeBench和CodeElo基准测试上,针对GPT-4o、DeepSeek-V3和Gemini 2.5 Flash,我们的工具在基线(测试生成、元测试和自动形式化规范)上将正确程序选择的概率提高了24%,并在选择或退避场景中实现了26%更高的F1分数,同时是唯一能一致处理允许多个有效解的不精确问题的方法。

英文摘要:

Large language models (LLMs) can generate executable code from natural language descriptions, but the resulting programs frequently contain bugs due to hallucinations. In the absence of formal specifications, existing approaches attempt to assess correctness using LLM-generated proxies such as tests or auto-formalized specifications. However, these proxies are produced by the same imperfect models and thus often corroborate rather than catch errors, especially when the model exhibits correlated errors. We introduce semantic triangulation, a theory-grounded framework that decorrelates model errors by transforming the original problem into a dissociative variant - one likely requiring a fundamentally different algorithm - and checks consistency between independently sampled solutions to both problems. We identify theoretical requirements for this framework, and we prove that under a formal model of LLM hallucinations, these properties confer higher confidence in program correctness. We instantiate the framework through four concrete triangulation methods based on problem inversion, decomposition, and solution enumeration. Evaluated on LiveCodeBench and CodeElo across GPT-4o, DeepSeek-V3, and Gemini 2.5 Flash, our tool increases the probability of selecting a correct program by 16% over baselines (test generation, metamorphic testing, and auto-formalized specifications) and achieves 7% higher reliability and 7% higher F1 score in selection-or-abstention scenarios, while being the only method that consistently handles inexact problems admitting multiple valid solutions.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑