[论文解读] ToolCoder: Teach Code Generation Models to use API search tools
ToolCoder 将 API 搜索工具集成到预训练的代码生成模型中,使在代码生成过程中能够自动使用工具,在公共和私有库基准上获得持续的收益。它使用基于 ChatGPT 的数据标注和 LoRA 基于的微调来实现高效的工具启用解码。
Automatically generating source code from natural language descriptions has been a growing field of research in recent years. However, current large-scale code generation models often encounter difficulties when selecting appropriate APIs for specific contexts. These models may generate APIs that do not meet requirements or refer to non-existent APIs in third-party libraries, especially for lesser-known or private libraries. Inspired by the process of human developers using tools to search APIs, we propose ToolCoder, a novel approach that integrates API search tools with existing models to assist in code generation and API selection. To teach our model to use tools, we introduce an automated data annotation method using ChatGPT to add tool usage information into the source code data and fine-tune code generation models. During inference, we integrate API search tools into the generation process so that our model can automatically use the search tool to get suggestions when selecting an API. Our experimental results demonstrate that ToolCoder exhibits excellent performance and generalization across five public and private library code generation benchmarks, with at least 6.21\% improvement on average pass@1 metrics and 9.64\% improvement on average pass@10 metrics compared to state-of-the-art methods. Furthermore, we show that our relatively small ToolCoder model is comparable to one of the current best models, GPT-3.5, highlighting the potential of incorporating programming tools into the code generation process.
研究动机与目标
- 激发并解决代码生成模型中由于庞大且私有的库造成的 API 选择挑战。
- 引入一个工具增强的数据管道,教导模型使用 API 搜索工具。
- 提出一种高效的微调方法,使解码过程中能够使用工具。
- 展示 ToolCoder 在公开与私有库基准上的泛化能力。
提出的方法
- 使用 ChatGPT 自动标注一个工具增强的数据集,在源代码数据中插入工具使用调用。
- 使用参数高效微调(LoRA)针对 API 搜索工具使用,对预训练的代码生成模型进行微调。
- 在推理阶段将 API 搜索工具整合到解码过程中,当模型输出工具调用标记时触发工具调用。
- 使用两个搜索工具来源:在线的 DuckDuckGo + 私有库的文档内基于 BM25 的检索,将其统一为带有特殊标记的 API 搜索调用。
- 在五个基准上评估 ToolCoder(三个公开:NumpyEval、PandasEval、TorchDataEval;两个私有:MonkeyEval、BeatNumEval),使用 pass@k 指标(k=1、10)。
- 与通用代码生成基线和面向 API 的基线进行比较,其中 GPT-3.5 作为强参考。
实验结果
研究问题
- RQ1RQ1:ToolCoder 在公开库基准上相较于最新状态基线的表现如何?
- RQ2RQ2:ToolCoder 在私有库基准上的表现如何?
- RQ3RQ3:数据集、训练和推理组件(消融)的贡献是什么?
- RQ4RQ4:ToolCoder 生成解的代码质量和失败模式如何?
主要发现
| 模型 | 参数量 | NumpyEval 的 pass@1 | NumpyEval 的 pass@10 | PandasEval 的 pass@1 | PandasEval 的 pass@10 | TorchDataEval 的 pass@1 | TorchDataEval 的 pass@10 |
|---|---|---|---|---|---|---|---|
| CodeT5 | 220M | 0 | 0.1 | 0 | 0 | 0 | 0 |
| PyCodeGPT | 110M | 18.04 | 38.61 | 12.75 | 37.62 | 3.80 | 14.00 |
| CodeGen350M | 350M | 18.51 | 43.56 | 16.73 | 29.70 | 4.60 | 14.00 |
| CodeGen2B | 2B | 29.10 | 53.46 | 30.69 | 42.57 | 7.00 | 18.00 |
| GPT3.5 | - | 58.41 | 66.21 | 30.09 | 33.16 | 6.00 | 24.00 |
| CERT-numpy | 220M | 31.47 | 46.42 | 16.03 | 27.72 | 2.20 | 14.00 |
| CERT-pandas | 220M | 18.81 | 33.66 | 28.42 | 48.04 | 2.80 | 6.00 |
| CodeGenAPI | 350M | 16.55 | 29.48 | 13.58 | 34.95 | 7.19 | 16.93 |
| CodeGenAPI-retrieval | 475M | 12.67 | 27.32 | 11.25 | 28.61 | 10.41 | 23.50 |
| CodeGen-retrieval | 475M | 18.30 | 35.12 | 9.54 | 29.02 | 7.52 | 16.36 |
| ToolCoder-OnlineTool | 350M | 35.64 | 50.50 | 22.77 | 37.62 | 7.40 | 20.00 |
| ToolCoder-OnlineTool-2B | 2B | 41.58 | 55.44 | 31.68 | 47.52 | 11.80 | 24.00 |
- ToolCoder 在公开基准上相对于最先进基线在 pass@1 平均提升不少于 6.21%,在 pass@10 平均提升 9.64%。
- ToolCoder-OnlineTool 具备在线工具的变体取得强结果并减少 API 选择错误。
- 在私有库上,结合文档搜索或私有文档工具的 ToolCoder 展现稳定提升和良好泛化。
- 在五个基准上,ToolCoder 的平均 pass@1 提升根据设置在 6.21%–19.00% 之间,在某些公开基准接近或达到大模型。
- 基于 LoRA 的微调实现参数高效学习,分别仅训练 CodeGen-350M 的 0.18% 和 CodeGen-2B 的 0.09% 参数。
- 一项消融研究证实数据标注、训练和推理设计选择的有效性。
更好的研究,从现在开始
从论文设计到论文写作,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。