TruncProof: 一种用于在令牌长度限制下基于LLM的JSON生成的防护机制
TruncProof: A Guardrail for LLM-based JSON Generation under Token-Length Constraints
AI总结:
本文提出TruncProof,一种基于文法约束的生成方法,使LLM在限定令牌数下生成语法正确的JSON。实验表明其在严格令牌限制下仍能生成语法正确且语义准确的输出。
摘要:
基于LLM的机器可读输出(如JSON)的生成已引起关注,以集成到外部系统中。然而,现有方法无法严格限制生成的最大令牌数,导致无限生成或截断输出,从而引发系统故障。为解决此限制,我们提出了TruncProof,一种新颖的文法约束生成方法,使LLM能够在预定义的令牌限制下生成语法正确的JSON。通过利用LL(1)解析器的性质,TruncProof在每一步解码过程中高效地近似完成语法正确输出所需的最小令牌数。在文本到JSON指令任务上的实验表明,TruncProof在严格令牌限制下仍能生成语法正确的输出。此外,我们展示了TruncProof可以有效地与高级解码策略结合,产生不仅语法正确而且语义准确的输出。
英文摘要:
The LLM-based generation of machine-readable outputs such as JSON has attracted significant attention for integration with external systems. However, existing approaches cannot strictly enforce the maximum number of tokens to be generated, leading to infinite generation or truncated outputs that cause a system malfunction. To address this limitation, we propose TruncProof, a novel grammar-constrained generation method that enables LLMs to produce grammatically valid JSONs while adhering to a predefined token limit. By leveraging the properties of LL(1) parsers, TruncProof efficiently approximates the minimum number of tokens required to complete a grammatically valid output at each decoding step. Experiments on the Text-to-JSON instruction tasks demonstrate that TruncProof successfully generates syntactically correct outputs even under strict token constraints. Furthermore, we show that TruncProof can be effectively combined with advanced decoding strategies, resulting in outputs that are not only grammatically valid but also semantically accurate. The source code is public at https://github.com/Yosshi999/TruncProof
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。