Compositional API Recommendation for Library-Oriented Code Generation

📄 arXiv: 2402.19431v1 📥 PDF

作者: Zexiong Ma, Shengnan An, Bing Xie, Zeqi Lin

分类: cs.SE, cs.AI, cs.CL

发布日期: 2024-02-29

期刊: 32nd IEEE/ACM International Conference on Program Comprehension (ICPC 2024), Apr 2024, Lisboa, Portugal

DOI: 10.1145/3643916.3644403


💡 一句话要点

提出CAPIR以解决库导向代码生成中的API推荐问题

🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)

关键词: API推荐 代码生成 大型语言模型 任务分解 嵌入检索 重排序技术 软件开发 智能编程

📋 核心要点

  1. 现有API推荐方法在处理粗粒度开发需求时存在不足,难以有效组合多个细粒度API。
  2. CAPIR通过分解粗粒度任务为多个子任务,结合检索和重排序技术,提供更精准的API推荐。
  3. 在RAPID和LOCG基准测试中,CAPIR显著提升了API推荐的召回率和精确率,验证了其有效性。

📝 摘要(中文)

大型语言模型(LLMs)在代码生成方面表现出色,但在生成库导向代码时,尤其是对于未在训练数据中出现的库,其性能仍不理想。现有方法利用API推荐技术帮助LLMs使用库,但由于开发需求通常是粗粒度的,需结合多个细粒度API,导致API推荐面临挑战。为此,本文提出CAPIR(组合API推荐),采用“分而治之”策略,首先通过LLM基础的分解器将粗粒度任务描述分解为多个详细子任务,然后利用嵌入基础的检索器识别与每个子任务相关的API,最后通过LLM基础的重排序器过滤冗余API并提供最终推荐。此外,本文还提出了两个具有挑战性的基准测试,RAPID和LOCG,以评估API推荐方法在粗粒度需求上的表现。实验结果表明,CAPIR在各基准测试中相较于现有基线表现出色。

🔬 方法详解

问题定义:本文旨在解决库导向代码生成中的API推荐问题,现有方法在处理粗粒度需求时难以有效组合细粒度API,导致推荐效果不佳。

核心思路:CAPIR采用“分而治之”的策略,通过将粗粒度任务分解为多个细粒度子任务,进而为每个子任务推荐相关API,以提高推荐的准确性和相关性。

技术框架:CAPIR的整体架构包括三个主要模块:LLM基础的分解器用于任务分解,嵌入基础的检索器用于API检索,LLM基础的重排序器用于过滤冗余API并生成最终推荐。

关键创新:CAPIR的核心创新在于其分解和组合的策略,能够有效处理粗粒度需求,显著提升API推荐的质量,与传统方法相比具有本质区别。

关键设计:在设计中,使用了LLM进行任务分解和重排序,检索器则基于嵌入技术,确保了推荐的相关性和准确性。

🖼️ 关键图片

fig_0
fig_1
fig_2

📊 实验亮点

在RAPID的Torchdata-AR数据集上,CAPIR的召回率@5从18.7%提升至43.2%,精确率@5从15.5%提升至37.1%。在LOCG的Torchdata-Code数据集上,与没有API推荐的代码生成相比,CAPIR的通过率@100从16.0%提升至28.0%。

🎯 应用场景

CAPIR的研究成果在软件开发、自动化编程助手和智能IDE等领域具有广泛的应用潜力。通过提高API推荐的准确性,开发者可以更高效地利用库,减少开发时间和错误率,推动智能编程工具的发展。

📄 摘要(原文)

Large language models (LLMs) have achieved exceptional performance in code generation. However, the performance remains unsatisfactory in generating library-oriented code, especially for the libraries not present in the training data of LLMs. Previous work utilizes API recommendation technology to help LLMs use libraries: it retrieves APIs related to the user requirements, then leverages them as context to prompt LLMs. However, developmental requirements can be coarse-grained, requiring a combination of multiple fine-grained APIs. This granularity inconsistency makes API recommendation a challenging task. To address this, we propose CAPIR (Compositional API Recommendation), which adopts a "divide-and-conquer" strategy to recommend APIs for coarse-grained requirements. Specifically, CAPIR employs an LLM-based Decomposer to break down a coarse-grained task description into several detailed subtasks. Then, CAPIR applies an embedding-based Retriever to identify relevant APIs corresponding to each subtask. Moreover, CAPIR leverages an LLM-based Reranker to filter out redundant APIs and provides the final recommendation. To facilitate the evaluation of API recommendation methods on coarse-grained requirements, we present two challenging benchmarks, RAPID (Recommend APIs based on Documentation) and LOCG (Library-Oriented Code Generation). Experimental results on these benchmarks, demonstrate the effectiveness of CAPIR in comparison to existing baselines. Specifically, on RAPID's Torchdata-AR dataset, compared to the state-of-the-art API recommendation approach, CAPIR improves recall@5 from 18.7% to 43.2% and precision@5 from 15.5% to 37.1%. On LOCG's Torchdata-Code dataset, compared to code generation without API recommendation, CAPIR improves pass@100 from 16.0% to 28.0%.