[论文解读] Environment-Aware Code Generation: How far are We?
论文定义 Environment-Aware Code Generation (EACG) 并引入 VersiBCB,一个具有可执行、多库和版本感知任务的基准;评估三种推理时定制策略(数据、参数、缓存)并分析它们对可执行性、兼容性和组合性的影响。
Recent progress in large language models (LLMs) has improved code generation, but most evaluations still test isolated, small-scale code (e.g., a single function) under default or unspecified software environments. As a result, it is unclear whether LLMs can reliably generate executable code tailored to a user's specific environment. We present the first systematic study of Environment-Aware Code Generation (EACG), where generated code must be functionally correct and directly executable under arbitrary software configurations. To enable realistic evaluation, we introduce VersiBCB, a benchmark that is multi-package, execution-verified, and deprecation-aware, capturing complex and evolving environments that prior datasets often overlook. Using VersiBCB, we investigate three complementary adaptation axes: data, parameters, and cache, and develop representative strategies for each. Our results show that current LLMs struggle with environment-specific code generation, while our adaptations improve environment compatibility and executability. These findings highlight key challenges and opportunities for deploying LLMs in practical software engineering workflows.
研究动机与目标
- 将 Environment-Aware Code Generation (EACG) 正式化为在指定环境下生成在功能上正确且可执行的代码。
- 创建 VersiBCB,一个大规模、可执行性验证、支持多库的基准,反映真实世界的 Python 环境。
- 评估三种推理时适应策略(基于数据的检索增强生成 RAG、基于参数的专家混合 MoE、基于缓存的内存)以实现对环境的适应性。
- 评估可执行性、API 兼容性以及对未见库/版本配置的泛化能力。
- 为在实际软件工程工作流中部署大型语言模型提供见解和指导。
提出的方法
- 为 EACG 与 Environment-Aware Code Migration (EACM) 设定带环境规格 L、V 和功能需求 d 的任务表述。
- 通过在 BigCodeBench 上增加环境感知注释并在受控环境中执行代码来验证可行性,构建 VersiBCB。
- 使用 Pass@k 在 EACG 与 EACM 上评估最先进的 LLM,包括严格和宽松的 API 使用范式。
- 在推理时提出三种适应轴:基于数据的检索增强生成(RAG)、基于参数的版本感知路由的专家混合(MoE)、以及带有环境特定模式的缓存内存。

实验结果
研究问题
- RQ1模型是否能够在指定环境下生成可正确执行的代码(可执行性)?
- RQ2生成的 API 是否与给定环境的 API 集合兼容(兼容性)?
- RQ3模型是否对未见的库与版本组合具有泛化能力(可组合性)?
- RQ4环境适应策略在严格 API 遵循和实际可执行性方面的表现如何?
主要发现
| Model | Code Generation Pass@1 | Code Generation Pass@3 | Code Generation Pass@5 | Code Migration Pass@1 | Code Migration Pass@3 | Code Migration Pass@5 |
|---|---|---|---|---|---|---|
| DS-7B | 0.00 | 0.00 | 0.00 | 2.99 | 6.59 | 8.38 |
| CodeGemma-7B | 0.60 | 0.90 | 2.69 | 13.47 | 34.13 | 49.70 |
| CodeLlama-13B | 0.30 | 0.90 | 1.79 | 18.26 | 36.23 | 49.10 |
| StarCoder2-15B | 0.00 | 0.00 | 0.30 | 5.99 | 15.87 | 21.26 |
| LLaMA3-70B | 18.51 | 24.78 | 27.76 | 57.19 | 60.78 | 61.98 |
| GPT-4.1-mini | 27.76 | 32.24 | 33.43 | 53.29 | 59.28 | 61.68 |
| DeepSeek-v3 | 23.88 | 28.06 | 30.75 | 66.17 | 70.06 | 70.66 |
- 现有的 LLM 在环境感知代码生成方面存在挑战;较大模型表现更好,但仍落后于非环境无关的基准。
- 基于 MoE 的适应提升了严格 API 一致性和生成任务的部分正确性。
- 基于内存的适应在代码迁移中通过重用环境条件模式实现显著提升;然而,它也能容忍已弃用的 API。
- RAG 提供了保守的适应,收益中等且能解释环境信号。
- 所有策略在机器学习领域和未见库/版本组合上均表现出性能下降,凸显在版本敏感环境中的持续挑战。
- VersiBCB 实现对执行、兼容性和跨库演进的细粒度评价,揭示了标准基准未能捕捉到的差距。

更好的研究,从现在开始
从论文设计到论文写作,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。