arXiv:2607.05936cs.SEcs.LG2026-07

用检索增强和约束解码提升大模型调用API的准确性

Mitigating Errors in LLM-Generated Web API Invocations via Retrieval-Augmented Generation and Constrained Decoding

论文配图:Mitigating Errors in LLM-Generated Web API Invocations via Retrieval-Augmented Generation and Constrained Decoding
图 1 · 摘自论文原文
  • 用OpenAPI文档检索接口片段,注入提示词辅助生成
  • 自动将API规范转为正则约束,生成时强制合规
  • 对真实代码库有效,适合开发者提升API调用可靠性

Web API集成是现代软件系统的核心,但编写正确调用代码仍因规格复杂多变而困难。尽管大模型用于代码生成日益普及,但其生成正确API调用的能力有限。本文系统评估了检索增强生成(RAG)与约束解码(CD)两种互补方法。RAG通过处理OpenAPI规范,检索紧凑的端点表示并注入提示;CD将OpenAPI规范自动转换为基于正则的约束,在生成时强制执行。在WAPIIBench的合成数据集及从GitHub仓库构建的新真实数据集上评估显示:RAG在完整生成时减少幻觉、提升正确性,但在已知端点时反而引入多余参数;而CD能可靠防止非法URL、HTTP方法和参数,显著提升两类起始代码的整体正确性。

原文摘要 · Abstract (English)

Integration of web APIs is a cornerstone of modern software systems, yet writing correct web API invocation code remains challenging due to complex and evolving API specifications. Although LLMs are increasingly used for code generation, previous work has empirically shown that their ability to generate correct web API integrations is limited. At the same time, mitigation techniques and their effectiveness for this setting remain insufficiently understood. In this paper, we propose and systematically evaluate retrieval-augmented generation (RAG) and constrained decoding (CD) as two complementary approaches to improving LLM-generated web API invocation code. For RAG, we design a retriever that processes OpenAPI specifications and retrieves compact endpoint representations to inject into model prompts. For CD, we introduce an automatic translation from OpenAPI specifications to regex-based constraints enforced during generation. We evaluate both approaches on WAPIIBench's existing synthetic dataset and on a new real-world dataset derived from GitHub repositories. Our results show that RAG reduces hallucinations and improves correctness when generating full API invocations but reduces it when the endpoint is already provided as it encourages the generation of unnecessary parameters. In contrast, CD reliably prevents illegal URLs, HTTP methods, and arguments and substantially improves overall correctness for both starter codes.

API生成大模型RAG约束解码

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